all groups > sql server msde > june 2004 >
You're in the

sql server msde

group:

Sql date select expressions?


Sql date select expressions? John Carnahan
6/30/2004 4:01:21 PM
sql server msde:
Having a problem understanding datetime field query. When I do the
following, I don't get the correct data.
What am I doing wrong?

this gives me everything in the table regardless of the query expression..
SELECT MyField FROM MyTable WHERE MyDateTimeField >= '6/1/2004' AND
MyDateTimeField < '7/1/2004'

For some reason, if I do the following, I get 6/1/2004 records as well as
records dated after 6/1/2004... Should just get 6/2/2004 and above
SELECT MyField FROM MyTable WHERE MyDateTimeField > '6/1/2004'

Do I need to do a cast or some other function in the select?

Re: Sql date select expressions? Aaron [SQL Server MVP]
6/30/2004 10:40:05 PM
Can you show your table structure, sample data, and desired results? Are
you sure that MyDateTimeField is a DATETIME or SMALLDATETIME column? Have
you tried using a standard date format, e.g.

WHERE MyDateTimeField >= '20040601' AND MyDateTimeField < '20040701'

--
http://www.aspfaq.com/
(Reverse address to reply.)




[quoted text, click to view]

Re: Sql date select expressions? Aaron [SQL Server MVP]
6/30/2004 10:45:22 PM
[quoted text, click to view]

No, that is incorrect. DATETIME contains time information too. 6/1/2004
4:00 PM is > 6/1/2004 at midnight, which is what your > is comparing to. If
you only want rows from 2004-06-02 and later, then use >= '20040602'

--
http://www.aspfaq.com/
(Reverse address to reply.)

Re: Sql date select expressions? John Carnahan
7/2/2004 8:26:53 AM

[quoted text, click to view]

Wouldn't 6/1/2004
4:00 PM is LESS THAN 6/1/2004 at midnight?

.....anyway
the "accessdate" field is DATETIME not short
I tried the following...

select * from visitorhead where accessdate >= '20040525' and accessdate <=
'20040614' order by accessdate

.....result
ACCESSDATE
5/25/2004 12:18:25 PM
5/31/2004 3:38:14 PM
6/12/2004 3:40:31 PM
6/13/2004 1:59:06 PM

didn't pick these up...
6/14/2004 4:25:46 AM
6/14/2004 11:06:57 AM
6/14/2004 11:26:02 PM

Formating the date string to '20040525' rather than '5/25/2004' does solve
the first critereia, but not the second.

Re: Sql date select expressions? John Carnahan
7/2/2004 12:19:07 PM
I didn't realize the TIME must be part of the date portion of the query, but
that makes sense.
Thanks
John

[quoted text, click to view]

Re: Sql date select expressions? Andrea Montanari
7/2/2004 5:53:14 PM
hi John,
"John Carnahan" <carnahan_j@msn.com> ha scritto nel messaggio
news:%23gmrDkEYEHA.2344@TK2MSFTNGP11.phx.gbl...
[quoted text, click to view]

as Aaron already pointed out DATETIME type include ttime information too, so
6/1/2004 4:00 PM is LESS THAN 6/1/2004 23:59
you query must include data till '20040614 23:59....' , so, for short, it
must be less then '20040615', in order to include all rows from 20040614
00:00:001 to 20040614 23:59....
--
Andrea Montanari (Microsoft MVP - SQL Server)
http://www.asql.biz/DbaMgr.shtm http://italy.mvps.org
DbaMgr2k ver 0.8.0 - DbaMgr ver 0.54.0
(my vb6+sql-dmo little try to provide MS MSDE 1.0 and MSDE 2000 a visual
interface)
--------- remove DMO to reply
AddThis Social Bookmark Button