Datetime Problem

  • Hey professionals,

    I have a Date column "dateadded" that stores date down to the minute. so it stores a date like say 2009-10-19 15:58:00. I want to select some columns and group by only want year, month and day. I do not want the time. How do I use the datepart function?

    For example if I only wanted to group by year, I would do this

    select dateadded, max(column1) from Mytable

    group by datepart(y, dateadded)

    How do I tweak the code to group by year, month and day without the minutes and hours

    Thanks

  • The most efficient in terms of CPU cycles is probably the following:

    dateadd(day,datediff(day, 0,getdate()),0)

    This strips hours, minutes, seconds and milliseconds from the date. Just replace GETDATE() with the name of your date column.

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

  • Bob,

    I really wish you'd quit beating me to answers tonight!:-D

  • Even a blind squirrel finds some nuts, Jack. 😀

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

  • Bob Hovious 24601 (10/19/2009)


    Even a blind squirrel finds some nuts, Jack. 😀

    Hmmm, I could take that a couple of different ways.:-P

  • You've been watching Johnny Depp in "Charlie and the Chocolate Factory" haven't you? 😛

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

  • Thanks for the help guys!

  • Hey guys, i noticed that the query gave me some 1905 dates?? Is there a reason why?

  • Not to sound sarcastic, but they show because there are 1905 dates in the table. They are probably typos, but you'll have to determine that.

  • i :-D, I also thought for a minute that there were 1905 records on the table but that was not the case. There are 3 tables that I am putting all the records into 1. The good news is that the code was not responsible for the 1905 records. So my apologies for that.

    However something really strange is happening. Each record on the 3 tables earliest date is 2003. When i join them all together into 1 table, then I see 1905 records from no where. Very strange. Has this ever happened to anybody?

  • Could we see some code and sample data?

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

  • False alarm never mind. Sorry about that and thanks for all the help guys !!!

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply