help with getdate()

  • What is the best way to strip out the seconds of the current date returned by getdate()?

    so far I have..

    PRINT CONVERT(DATETIME,CONVERT(VARCHAR(10),GETDATE(),101),101)

    Is there a better way?

    Thanks in advance,

    Billy

  • Think I would use DATEPART:

    print datepart(ss,GETDATE())

    Gregory Larsen, DBA

    If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples

    Gregory A. Larsen, MVP

  • thanks..

    however I am looking for something that will remove the seconds (as well as the hours and minutes) from getdate()...

    for example, if I assign getdate() to a variable, it will display:

    Dec 23 2002 11:46AM

    However, I only want to store in the variable as:

    Dec 23 2002 12:00AM

    Thanks

    Billy

  • This works. Although you end up with a varhcar. Note: GetDate returns

    a data type of int, and if you convert this back to datetime you're going

    to have the time again (12:00).

    print convert(varchar(2),datepart(mm,GETDATE())) + '/' +

    convert(varchar(2),datepart(dd,GETDATE())) + '/' +

    convert(varchar(4),datepart(yyyy,GETDATE()))



    Everett Wilson
    ewilson10@yahoo.com

  • Here is another way:

    select convert(datetime, convert(varchar(12), getdate(), 101))

    Andy

    http://www.sqlservercentral.com/columnists/awarren/

  • Thanks for everyone's responses..

    ewilson: I think you mean "datepart" returns an integer.

    all: *sigh* if only there was some other more elegant way of getting just the current date instead of having to convert it to varchar and then back to datetime (something like getcurrentdate())...

  • Overall Andy's is the easiest and does exactly what you are stating. Date values are implicit within SQL anyway as long as the format is right, you do not have to convert back. The advantage that ewilson10's gives you is the fact that CONVERT does 1 extra read against the DB but I am not sure how the overall memory footprint between the two compare.

Viewing 7 posts - 1 through 6 (of 6 total)

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