Help me with the Select Statement for date formatting

  • Select '"' + CAST( GETDATE() as varchar(100) ) + '"' as Obs_dt_1

    I get this output---> "Apr 6 2015 4:07PM"

    But what I really need is for it to show in this format---> "2015-04-06 16:08:05.317"

    How do I do the select ?

  • convert has an optional 3rd parameter:

    --2015-04-06 16:13:21.187

    SELECT CONVERT(VARCHAR,getdate(),121)

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Thx, it worked.

    However when I paste the results in EXCEL the entire string got converted to some numbers ...

    Hmm that is outside of this forum but a reply would be appreciated.

  • mw112009 (4/6/2015)


    Thx, it worked.

    However when I paste the results in EXCEL the entire string got converted to some numbers ...

    Hmm that is outside of this forum but a reply would be appreciated.

    The short answer is that excel sucks and likes to try autoformat data, usually in very bad ways.

    What you can do is change all the cells in your excel sheet to text that should stop excel from trashing the data.

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

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