Datetime field

  • HI. I'm trying to pull out just the time from a datetime field. I'm using this:CONVERT(varchar(20), dbo.MYDATE, 8) and it works, but gives me the time in this format:19:44:15 but I need it to be like this:7:44:15 PM.

    I know its something easy, but I can't remember and I'm stuck. Does anyone have any ideas?

    Thanks!!

    Thanks!

  • Hello Anita,

    You need to use the statement like this

    Convert(Varchar(20), dbo.MyDate, 108)

    Hope this helps you.

    Thanks

     


    Lucky

  • lucky,

    here is how i have formatted some timedate information.  you can modify an necessary.

                    LEFT(

                        CONVERT(VARCHAR,

                            GETDATE(),101) + --DD/MM/YYYY

                            LEFT(RIGHT(CONVERT(VARCHAR, --HH:MM:SS

                                GETDATE(),109),15),9) + SPACE(1) +

                            RIGHT(CONVERT(VARCHAR,

                                GETDATE(),109),2),

                        22) + SPACE(1) -- AM/PM

  • Thanks! I can work with this and get what I need.

    Thanks!

  • or,

    SELECT Stuff(Right(CONVERT(varchar(30), GetDate(), 9), 14), 9, 4, ' ')

  • Also useful is knowing where in Help to find information like this. Type F1 in Enterprise manager or query analyzer and search on "CAST and CONVERT".

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

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