all groups > sql server programming > july 2006 >
You're in the

sql server programming

group:

Date Query Problem



Re: Date Query Problem Arnie Rowland
7/19/2006 3:02:56 PM
sql server programming: The convert() function fails because some of the rows have data that will
not convert to datetime.

I suggest that instead of 'invalid_date_text' you set the value to NULL.

--
Arnie Rowland
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous


[quoted text, click to view]

Date Query Problem Ben Rum
7/19/2006 9:23:10 PM
I am importing text files into a staging table, then doing a number of
checks on the data to flag potential erroneous rows.

I am getting stuck when trying to check if Date_A is a later date then
Date_B, which business rules don't allow. However, if some of the other rows
contain data which is not a vliad date, the whole check fails - even if I
unclude a where clause to ignore these rows...

I've tried moving the where columns around, but no luck.

Any advice appreciated.



create table #Temp (
row_id int null ,
date_a varchar(30) null ,
date_b varchar(30) null ,
error_flag bit null default 0 )

insert #temp ( row_id, date_a, date_b ) select 1, '12-jul-2006',
'19-jul-2006'
insert #temp ( row_id, date_a, date_b ) select 2, '10-jul-2006',
'19-jul-2006'
insert #temp ( row_id, date_a, date_b ) select 3, '20-jul-2006',
'19-jul-2006'
insert #temp ( row_id, date_a, date_b ) select 4, 'invalid_date_text',
'more_invalid_text'

-- I want to flag row_id 3 as error_flag = 1

update #temp
set error_flag = 1
where convert(datetime, date_a) > convert(datetime, date_b)
where IsDate(date_a) = 1
and IsDate(date_b) = 1

I also tried:

update #temp
set error_flag = 1
where convert(datetime, date_a) > convert(datetime, date_b)
and row_id in ( select row_id from #temp where isdate(date_a) = 1 and
isdate(date_b) = 1)

Re: Date Query Problem Erland Sommarskog
7/19/2006 10:24:56 PM
Ben Rum (bundyrum75@yahoo.com) writes:
[quoted text, click to view]

UPDATE #temp
SET error_flag = 1
WHERE Isdate(date_a) = 0
OR isdate(date_b) = 0


UPDATE #temp
SET error_flag = 1
WHERE CASE error_flag = 0 THEN convert(datetime, date_a) END >
CASE error_flag = 0 THEN convert(datetime, date_b) END

A CASE expression is the only safe way to avoid the unwanted evaluations
of expressions occur.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Re: Date Query Problem Steve G
7/27/2006 6:39:02 AM
You can also use the Case it in the same Update statement like this:

update #temp
set error_flag = 1
where
Case
when IsDate(date_a) = 1
then convert(datetime, date_a)
else convert(datetime,'31 dec 9999')
End
[quoted text, click to view]
Case
when IsDate(date_b) = 1
then convert(datetime, date_b)
else convert(datetime,'1 jan 1900')
End

This will also treat the case when one of the 2 dates are invalid and not
just the 2 dates are invalid at the same time. I think this is the more
efficient way to do that job.

Hope it helps
Steve
[quoted text, click to view]
AddThis Social Bookmark Button