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
 SQL Server Development (2000)
 Simple Date Query

Author  Topic 

D Martin
Starting Member

2 Posts

Posted - 2002-11-27 : 12:18:02
I'm trying to write a simple query with a specific date criteria.
EX. Select * From Logs where date='11/26/02'
I've like using Like, IN, and others but nothing seems to work other than using relational operators and Between. Is there a problem with datatype DateTime? Is there a way to do this query?

VyasKN
SQL Server MVP & SQLTeam MVY

313 Posts

Posted - 2002-11-27 : 12:23:19
You need to include time also in your WHERE clause. For example:

Select * From Logs where date between '11/26/02 00:00' and '11/26/02 23:59'

--
HTH,
Vyas
http://vyaskn.tripod.com
Go to Top of Page

D Martin
Starting Member

2 Posts

Posted - 2002-11-27 : 12:25:18
So the only way to query for a specific date is using between?

Go to Top of Page

Arnold Fribble
Yak-finder General

1961 Posts

Posted - 2002-11-27 : 12:28:33
The only way to query for a specific date is not using between.

DECLARE @dt datetime
SET @dt = '20021126'
SELECT * FROM Logs WHERE date >= @dt AND date < DATEADD(d, 1, @dt)


Go to Top of Page

ValterBorges
Master Smack Fu Yak Hacker

1429 Posts

Posted - 2002-11-28 : 00:44:58
Arnold are you saying that the between method does not work.
If so why does the between not work?

Also would your other method be faster?

SELECT * FROM Logs WHERE dateadd(dd,datediff(dd,0,date),0) = @dt


Edited by - ValterBorges on 11/28/2002 00:49:50
Go to Top of Page

mdanwerali
Starting Member

30 Posts

Posted - 2002-11-28 : 02:49:25
Hi

Instead of Between and Dateadd we can use Convert function which is very easy to use. what do u say.

Select * From Logs
where date = convert(char,'01/03/03')

Md Anwer Ali

Go to Top of Page

mr_mist
Grunnio

1870 Posts

Posted - 2002-11-28 : 03:36:43
quote:

Hi

Instead of Between and Dateadd we can use Convert function which is very easy to use. what do u say.




Not wanting to preempt him, but I'm guessing he'll say that the other way is faster.

-------
Moo.
Go to Top of Page

simondeutsch
Aged Yak Warrior

547 Posts

Posted - 2002-11-28 : 15:35:02
Syntax for Convert:
CONVERT (data_type[(length)], expression [, style])
so it would be CONVERT(char(10),date,101) for '01/01/2003'
But I have a recollection of someone posting that CONVERT is slower than using BETWEEN, or s/t like that. It's slower than some other solution, which I don't remember what that is.

Sarah Berger MCSD
Go to Top of Page

simondeutsch
Aged Yak Warrior

547 Posts

Posted - 2002-11-28 : 15:40:39
Syntax for Convert:
CONVERT (data_type[(length)], expression [, style])
so it would be CONVERT(char(10),date,101) for '01/01/2003'
But I have a recollection of someone posting that CONVERT is slower than using BETWEEN, or s/t like that. It's slower than some other solution, which I don't remember what that is.

Sarah Berger MCSD
Go to Top of Page
   

- Advertisement -