Thursday, March 22, 2012

Comparing Dates

Does anyone know of a quick and easy to compare 2 datetime values based only on the month and year.

For example,

FromDate = 11/30/2004
ToDate = 12/30/2004

I just need to compare the 11/2004 to 12/2004 using function like <=, =, >=, etc.

Any suggestions?

ThanksI'd use:IF Convert(CHAR(7), FromDate, 121) = Convert(CHAR(7), ToDate, 121)-PatP|||...or:

CONVERT(CHAR(7), [YourDate], 120) + '/01'

...will implicitly convert your date value to the first day of the month.|||...or:

CONVERT(CHAR(7), [YourDate], 120) + '/01'

...will implicitly convert your date value to the first day of the month.My machine doesn't like the mixed separators... It does Ok with:CONVERT(CHAR(7), [YourDate], 120) + '-01'-PatP|||My bad. I meant "-01"...|||I'd create a computed column. This way you can index it and avoid table/clustered index scan overhead.sqlsql

No comments:

Post a Comment