SQL Server, Convert Military to Standard Time

Posted on 2004-11-22
Last Modified: 2008-01-09
Here is my current SQL:

SELECT (startDate + ' From ' + startTime+ ' To ' + endTime) AS FullTime, dateTimeKey
FROM DatesTimes
WHERE DatesTimes.reservationKey = 79

startTime and endTime are being stored in the database as Strings.  The hold time values in hh:mm military format.  I would like to pull these values from the database and store them in FullTime but having a standard time format rather than military  (with AM or PM showing).

I tried this, but it didn't work:

SELECT convert(startTime, 108) AS Starting, convert(endTime, 108) AS Ending,
(startDate + ' From ' + Starting + ' To ' + Ending) AS FullTime, dateTimeKey
FROM DatesTimes
WHERE DatesTimes.reservationKey = 79

Any help would be most appreciated.
Question by:rreister
    LVL 16

    Accepted Solution

    The first line will pull the start and end times out as genuine date time values. You can't then references these directly though, so the second line might be all you really want. Haven't tested it, so if it's not quite right let me know and I'll tweak.

    SELECT CONVERT(datetime, startDate + ' ' + startTime) AS Starting, CONVERT(datetime, startDate + ' ' + endTime) AS Ending,
    startDate + ' From ' + RIGHT(CONVERT(varchar(20), CONVERT(datetime, startTime), 0), 7) + ' To ' + RIGHT(CONVERT(varchar(20), CONVERT(datetime, endTime), 0), 7) AS FullTime, dateTimeKey
    FROM DatesTimes
    WHERE DatesTimes.reservationKey = 79

    Author Comment

    It worked perfectly!  Thanks so much for quick and easy reply!
    LVL 16

    Expert Comment

    You're welcome

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Suggested Solutions

    Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
    CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now