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
 General SQL Server Forums
 New to SQL Server Programming
 Hlp with datetime field

Author  Topic 

vohyndra
Starting Member

7 Posts

Posted - 2006-12-20 : 00:56:09
hello there.
i have a databse with a datetime Filed and i want to find all records of the month on the datetie filed.
how can i do it.
i was using this but it does not work

SELECT * FROM laconexion.lc_members WHERE (baja = MONTH(GETDATE()))

thanks a lot

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2006-12-20 : 01:05:14
SELECT * FROM laconexion.lc_members WHERE (YEAR(baja) = YEAR(GETDATE()) AND MONTH(baja) = MONTH(GETDATE()))


Duane.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-12-20 : 03:39:48
<<
SELECT * FROM laconexion.lc_members WHERE (baja = MONTH(GETDATE()))
>>

1 The datecolumn doesnt have only month value
2 Also refer www.sql-server-performance.com/fk_datetime.asp

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-12-20 : 04:06:52
SELECT * FROM laconexion.lc_members WHERE DATEDIFF(month, baja, GETDATE()) = 0

SELECT * FROM laconexion.lc_members
WHERE baja >= DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0) and baja < DATEADD(month, DATEDIFF(month, 0, GETDATE()), 31)


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-12-20 : 05:21:39
I prefer using second method as it makes use of index if any

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -