SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Other SQL Server 2008 Topics
 Datetime comparison
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

chwanhun
Starting Member

22 Posts

Posted - 03/01/2012 :  13:17:38  Show Profile  Reply with Quote
Hello!
Why does this not return dates in 2012?
ISNULL(CONVERT(VARCHAR(10),closeddate,101), '12/31/9999') >= '12/31/2011'

However these conditions return the correct results...
ISNULL(CONVERT(VARCHAR(10),Alloc.ct_closeddate,101), '12/31/9999') <> '12/31/9999'

ISNULL(CONVERT(VARCHAR(10),Alloc.ct_closeddate,110), '12319999') = '12/31/2011'

ISNULL(Alloc.ct_closeddate,'12/31/9999') >= '12/31/2011'

Why?

Thanks

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 03/01/2012 :  13:56:20  Show Profile  Reply with Quote
why are you making dates as varchar before comparison?

why not use

closeddate>='2011-12-31'
?

whenever you make it varchar it will do string comparison which can cause output to come incorrectly

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

chwanhun
Starting Member

22 Posts

Posted - 03/01/2012 :  14:33:11  Show Profile  Reply with Quote
Thanks.
I was doing that because I had other comparison problems when I didn't use CONVERT. The SQL would not return records with dates of 12/31/2011 04:00:00:000 if I were using
ISNULL(closeddate,'12/31/9999') <= '12/31/2011' because of the hour. That's why I decided to only compare using the date portion of the datetime.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
29910 Posts

Posted - 03/01/2012 :  14:44:48  Show Profile  Visit SwePeso's Homepage  Reply with Quote
ISNULL(ClosedDate, '99991231') < '20120101'



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 03/01/2012 :  23:11:15  Show Profile  Reply with Quote
quote:
Originally posted by chwanhun

Thanks.
I was doing that because I had other comparison problems when I didn't use CONVERT. The SQL would not return records with dates of 12/31/2011 04:00:00:000 if I were using
ISNULL(closeddate,'12/31/9999') <= '12/31/2011' because of the hour. That's why I decided to only compare using the date portion of the datetime.


it will return if you're using a interval using >= and <

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.12 seconds. Powered By: Snitz Forums 2000