all groups > sql server (alternate) > march 2006 >
You're in the

sql server (alternate)

group:

looking to collect distinct date part out of datetime field


looking to collect distinct date part out of datetime field sdowney717 NO[at]SPAM msn.com
3/11/2006 5:02:03 AM
sql server (alternate):
from this, circdate being a datetime field:
SQLQuery = "select distinct circdate from circdata order by circdate"

I need the distinct date portion excluding the time part.

this has come about when I discovered
I am inserting and updating some datetime values with the same value,
but for some reason, the values are always off by a few seconds. I set
a variable called SetNow assigned to NOW and then set the datetime
fields to this SetNow variable. Then when I collect the distinct date
time I am assuming they will have the same values recorded in
circdate, but no, they are off by several seconds. Makes no sense to me
at all. I tried renaming the variable several times but it makes no
difference at all.
any help appreciated, thanks.
Re: looking to collect distinct date part out of datetime field sdowney717 NO[at]SPAM msn.com
3/11/2006 5:27:54 AM
SQLQuery = "SELECT distinct CONVERT(char,circdate,1) from circdata"

I think I solved it
any one see a problem with this?
thanks
how does the '1' parameter affect the output as I know there are
several choices
Re: looking to collect distinct date part out of datetime field Erland Sommarskog
3/11/2006 6:07:59 PM
sdowney717@msn.com (sdowney717@msn.com) writes:
[quoted text, click to view]

1 is a format parameter that controls how the datetime value is formatted.
You can read about these in the topic CAST and CONVERT in Books Online.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Re: looking to collect distinct date part out of datetime field sdowney717 NO[at]SPAM msn.com
3/11/2006 7:59:37 PM
http://www.aspfaq.com/show.asp?id=2464
shows all the outputs
Was wondering though if it wont use an index on a convert.
Re: looking to collect distinct date part out of datetime field Erland Sommarskog
3/12/2006 12:00:00 AM
sdowney717@msn.com (sdowney717@msn.com) writes:
[quoted text, click to view]

For the query you gave,

SELECT distinct CONVERT(char,circdate,1) from circdata

this is not an issue. If there is an index on cricdate, SQL Server will
use that index in the most effective, that is to scan the index, because
that is what the query calls for, with or without the convert().

On the other hand

SELECT col1, col2, col3 FROM circdata
WHERE CONVERT(char, circdate, 1) = @val

will probably not use the index, and in any case the query will not seek
the index, that is lookup the value through the B-tree. This is because
the index is sorted on the datetime value, not on a character value.

To list all rows for a given date you can do:

SELECT col1, col2, col3 FROM circdata
WHERE circdate >= @val AND circdate < dateadd(DAY, @val, 1)


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Re: looking to collect distinct date part out of datetime field Madhivanan
3/14/2006 5:17:28 AM
Also refer this to know how to query on dates
http://www.karaszi.com/SQLServer/info_datetime.asp

Madhivanan
AddThis Social Bookmark Button