Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Date compare help

Author  Topic 

qman
Constraint Violating Yak Guru

442 Posts

Posted - 2010-10-13 : 12:54:00
Could someone help me out with the following SQL Logic?

Specifically, I am trying to revreive records where a date value is >= first day of the previous month
AND
a date value is < first day of current month

Any ideas?

select * from tablename
where
TRANSACTION_CREATE_TS >= first day of previous month
and
TRANSACTION_CREATE_TS < first day of current month

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2010-10-13 : 13:13:32
[code]
TRANSACTION_CREATE_TS >= DateAdd(month, DateDiff(month, 0, getdate())-1, 0)
and
TRANSACTION_CREATE_TS < DateAdd(month, DateDiff(month, 0, getdate()), 0)[/code]

or
[code]
TRANSACTION_CREATE_TS BETWEEN
DateAdd(month, DateDiff(month, 0, getdate())-1, 0)
AND
DateAdd(month, DateDiff(month, 0, getdate()), 0)-1[/code]
Go to Top of Page

qman
Constraint Violating Yak Guru

442 Posts

Posted - 2010-10-13 : 13:20:22
Sweet, you the man Russell!
Go to Top of Page
   

- Advertisement -