GETDATE() Manipulation

  • 1. Here is the code I'm using:

    declare @dt as datetime

    set @dt=CONVERT(varchar(8), GETDATE(), 112)

    print @dt

    The result is :

    Dec 23 2008 12:00AM

    How do I get only Dec 23 2008?

    2. When I set a column's default value to GETDATE()

    I get the date and time.

    How do i get rid of the seconds at the end?

  • declare @dt as varchar(11)

    set @dt=left(convert(varchar,getdate()),11)

    print @dt

    there is no convert option to display mon dd yyyy



    Pradeep Singh

  • Hi Franco

    I have always hated working with date formatting so here is my attempt to help you and make it a better experience for you! 🙂

    Franco_1 (12/23/2008)


    1. Here is the code I'm using:

    declare @dt as datetime

    set @dt=CONVERT(varchar(8), GETDATE(), 112)

    print @dt

    The result is :

    Dec 23 2008 12:00AM

    How do I get only Dec 23 2008?

    Since the variable @dt is of type datetime it will invariably return the time part if you dont explicity convert it when printing.

    For example:

    print CONVERT(varchar, GETDATE(), 107)

    print CONVERT(varchar(8), GETDATE(), 112)

    2. When I set a column's default value to GETDATE()

    I get the date and time.

    How do i get rid of the seconds at the end?

    If the column is a datetime i believe you are stuck with the time part.

    When you insert a date without specifying the time, it's still there (00:00:00.000). I like to see the datetime as a string with a mask always applied to it.

    create table #DateTable(

    SomeField int,

    MyDate datetime default getdate()

    )

    insert into #DateTable(SomeField) values (1)

    insert into #DateTable(SomeField,MyDate) values (2,'20081223')

    select * from #DateTable

    drop table #DateTable

    Let me know if this helps and if anyone has better solution please share!

  • Thank you, Thank you!

    Both suggestions work.

  • Franco_1 (12/23/2008)


    1. Here is the code I'm using:

    declare @dt as datetime

    set @dt=CONVERT(varchar(8), GETDATE(), 112)

    print @dt

    The result is :

    Dec 23 2008 12:00AM

    How do I get only Dec 23 2008?

    2. When I set a column's default value to GETDATE()

    I get the date and time.

    How do i get rid of the seconds at the end?

    Or If you use front end application, use format function there


    Madhivanan

    Failing to plan is Planning to fail

Viewing 5 posts - 1 through 4 (of 4 total)

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