SQL Server - Datediff function on the integer values

Asked By Bharti Moorjani on 16-Jun-04 02:39 PM
Please check the problem description below
SELECT   Item.File, Item.Code,
SUBSTRING(CONVERT(varchar(8), Search.OrdDate), 5, 2) + '/' + SUBSTRING(CONVERT(varchar(8), Search.OrdDate), 7, 2) + '/' + SUBSTRING(CONVERT(varchar(8), Search.OrdDate),1,4)	as orderdate	,
SUBSTRING(CONVERT(varchar(8), Items.CompleteDt), 5, 2) + '/' + SUBSTRING(CONVERT(varchar(8), Items.CompleteDt), 7, 2) + '/' + SUBSTRING(CONVERT(varchar(8), Items.CompleteDt),1,4) as 	completedate,
	datediff(DAY,     	
SUBSTRING(CONVERT(varchar(8), Search.OrdDate), 5, 2) + '/' + SUBSTRING(CONVERT(varchar(8), Search.OrdDate), 7, 2) + '/' + SUBSTRING(CONVERT(varchar(8), Search.OrdDate),1,4)	,
SUBSTRING(CONVERT(varchar(8), Items.CompleteDt), 5, 2) + '/' + SUBSTRING(CONVERT(varchar(8), Items.CompleteDt), 7, 2) + '/' + SUBSTRING(CONVERT(varchar(8), Items.CompleteDt),1,4)
		 )	+ 1 AS [NumOfDays]
FROM    Items INNER JOIN
              			Search ON
				Item.File = Search.FirmFile
				where Items.Code like	 'Policy'
Problem Description
The above query works well, if there is a valid date in CompleteDt.
However, if there is a 0 then it is displays an error
	Syntax error converting datetime from character string.
How should I check for 0’s in the CompleteDt field and cancel the date diff computation.
Also could I possible set the two the orderdate and complete date in a variable .. and use the variables to compute the datediff..??

is your field a varchar or datetime?

Asked By Mike Prince on 16-Jun-04 02:46 PM
Is Search.OrdDate a datetime field? If so, try convert(varchar(12), Search.OrdDate, 101) to trim off the time if that's what you're trying to do.  You wouldn't have to work so hard to form a date plus you wouldn't get the error you're referring to.  
Adding "isdate(Search.OrdDate) = 1" to your where clause might get you around your issue, but it would then exclude the rows in question.  good luck.

Nope my field is an int

Asked By Bharti Moorjani on 16-Jun-04 05:27 PM
see if that helps...

I'll read the subject next time

Asked By Mike Prince on 17-Jun-04 11:36 AM
Can you just filter out where Search.OrdDate <> 0?
int?
Asked By Mike Prince on 17-Jun-04 11:37 AM
any chance of changing it to a date field?  Life will be easier...
Another dilemma
Asked By Bharti Moorjani on 23-Jun-04 12:30 PM
I ended up checking for <> 0.. 
I now need to subtract 90 days..
 select
(SUBSTRING(CAST(Search.OrdDate AS CHAR(8)),5,2) + '/' + 
	RIGHT(CAST(Search.OrdDate AS CHAR(8)),2) + '/' + 
	LEFT(CAST(Search.OrdDate AS CHAR(8)),4))as orderdate
from Search
where  OrdDate <= convert(varchar(12),DATEADD(day,-90,GETDATE()),101)
under all normal circumstances the 
convert(varchar(12),DATEADD(day,-90,GETDATE()),101)
will subtract 90 days from today 
how do i convert the orderdate to a valid date.. or vice versa how do i change the where clause to an int??
I still don't understand
Asked By Mike Prince on 28-Jun-04 08:16 AM
why the date is stored as an int.  If you can't change the field to a datetime field, then maybe you can create another field in the same table that is the translation of your int to a datetime and then just use that field for all of your date related queries.  In other words, on insert into this table, add both the int and datetime version of the value.