Converting Datetime Field To Text

  • I have a datetime field that I want to convert to text in another field and also have the full name of the month displayed along with dropping any preceeding zeroes in the date that it returns.

    If I run the following:

    Update membership_table

    set membership_period = convert(varchar, effective_date, 107)

    Note: membership_period is varchar

    It returns the following results in the field in the two records I'm testing:

    Jan 01, 2010

    Nov 01, 2009

    In the examples above (and any others that I'll need to run it against) I want the months spelled out (Jan - January, Feb-February, Nov-November, etc) and I'd also like the actual dates not to be preceeded by a 0. So, Jan 01, 2010 would actually need to be January 1, 2010.

    Can someone point me in the right direction?

    Thank you.

    Roger

  • The DatePart and DateName functions should handle what you need to do.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • I agree with Wayne and I'll go a bit further. Study ALL of the functions in SQL Server. At least learn what they do so you know what is possible... especially the date functions.

    To get you out of the woods, here's an example...

    SELECT DATENAME(mm,GETDATE())+' '+DATENAME(dd,GETDATE())+', '+DATENAME(yy,GETDATE())

    The thing I'd really like to know is, considering that date formatting is normally considered to be a form of "Death by SQL", why do you need to format the dates? Where will the formatted dates be used?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Thanks everyone for pointing me in the right direction. I did figure it out. I am setting it up to use in the body of an email where I need the dates printed in text format... Like January 1, 2010.

    Rog

  • Roger Abram (10/3/2009)


    I did figure it out.

    Would you mind posting what your final code is? Thanks.

    I am setting it up to use in the body of an email where I need the dates printed in text format... Like January 1, 2010.

    Thanks for the feedback on that. I always wonder...

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

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