Thursday, January 1, 2009

Date comparison in SQL

Date comparison in SQL

I've observed many code where date is compared like below

if  convert(varchar(50), '1/4/2009',101) <= convert(varchar(50), '12/29/2008',101)
begin
print 'true'
end
else
print 'false'

If above is true then what about below code???


if  cast('1/4/2009' as datetime) <= cast('12/29/2008' as datetime)
begin
print 'true'
end
else
print 'false'

When comparing dates in SQL we must cast it to datetime...

No comments:

Post a Comment