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 2000 Forums
 Transact-SQL (2000)
 Search on date

Author  Topic 

nemohm
Yak Posting Veteran

69 Posts

Posted - 2003-12-04 : 13:56:24

Hi,

What could be the correct query to search on date with LIKE?

There are results I have:

SELECT COUNT(*)
FROM VIEWEVENT
WHERE DateTime like '%2003%'

24264


SELECT COUNT(*)
FROM VIEWEVENT
WHERE DateTime like '%2003-12-03%'

0

select count(*) from viewEVENT
where DateTime between '2003-12-02 23:59:59' AND '2003-12-03 23:59:59'

1879

Please help!

Thx,

Dobby

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-12-04 : 14:00:38
Use DATEPART instead of LIKE.

SELECT COUNT(*)
FROM VIEWEVENT
WHERE DATEPART(yy, DateTime) = 2003

SELECT COUNT(*)
FROM VIEWEVENT
WHERE DATEPART(yy, DateTime) = 2003 AND DATEPART(m, DateTime) = 12 AND DATEPART(d, DateTime) = 3

Tara
Go to Top of Page

nemohm
Yak Posting Veteran

69 Posts

Posted - 2003-12-04 : 14:14:50
Tara,

Something as DATEPART(yyyy, mm, dd, datetime)?
(2003-12-03)

Dobby
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-12-04 : 14:17:08
You can not pass more than one thing to DATEPART. Please look up DATEPART in BOL. My second query shows what to do for year, month, and day.

Tara
Go to Top of Page

nemohm
Yak Posting Veteran

69 Posts

Posted - 2003-12-04 : 14:31:04
Disregard my last question, please.

Thank you very much!

Dobby
Go to Top of Page
   

- Advertisement -