Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Interview with me about the chapters I wrote for the SQL Server Deep Dives bookHow to script all stored procedures in a database »
    comments

    This is a quick post about a question that had to do with time formatting. This question was asked in our SQL Server programming forum today

    I don't see a format of hh:mm AM/PM only

    Do we need to do it using datepart or how to get only hour:minute am/pm format ?

    The question can be found here: Formatting time only. SQL Server has a bunch of formats for converting datetime values, a list of all of them can be found in Books On Line here: CAST and CONVERT (Transact-SQL)


    So let's say for example that you have this datetime '2010-03-24 16:20:01.800' and you want to show 4:20 PM, how can you do that?

    Let's take a look, if you are on SQL Server 2008 you can convert to time

    1. DECLARE @t TIME = '2010-03-24 16:20:01.800'
    2. SELECT @t AS TIME

    -----------------
    16:20:01.8000000



    Okay that gives me 16:20:01.8000000, not what the original poster wanted

    You can do this

    1. DECLARE @t TIME = '2010-03-24 16:20:01.800'
    2. SELECT CONVERT(VARCHAR(30),@t,100)

    --------------------
    4:20PM



    That gives me 4:20PM. But how can I add a space between 0 and PM? Say hello to my little friend STUFF. In order to add a space you need to start at position 5 and add a space and don't do any replacement.

    Take a look at what I mean

    1. SELECT STUFF('4:20PM',5,0,' ')

    ---------------
    4:20 PM



    Now let's use the code from before and wrap the STUFF function around it

    1. DECLARE @t TIME = '2010-03-24 16:20:01.800'
    2. SELECT STUFF(RIGHT(' ' + CONVERT(VARCHAR(30),@t,100),7),6,0,' ') AS FormattedTime

    --------------
    4:20 PM



    Okay but what if you are not on SQL Server 2008? No problem really, it is almost the same

    1. DECLARE @d datetime
    2. SET @d = '2010-03-24 16:20:01.800'
    3. SELECT STUFF(RIGHT(' ' + CONVERT(VARCHAR(30),@d,100),7),6,0,' ')

    --------------
    4:20 PM




    *** Remember, if you have a SQL related question, try our Microsoft SQL Server Programming forum or our Microsoft SQL Server Admin forum

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    1970 views
    Instapaper

    5 comments

    Comment from: bradschu [Member] Email
    bradschu I'm afraid the approach will only work for times up to 9:59:59 AM/PM. Once you get into 2-digit hours (10, 11 or 12), then the function will break, I'm afraid. (For a time of 10:00:00, it will show '0:00 AM', cutting off the left-most digit).

    Instead of RIGHT(,6), I would instead to RIGHT(,7) and then LTRIM() it at the end. You'll also have to change the location of the STUFF(). Here's what I suggest:

    SELECT LTRIM(STUFF(RIGHT(CONVERT(VARCHAR(30),@d,100),7),6,0,' '))

    --Brad
    03/24/10 @ 17:09
    Comment from: SQLDenis [Member] Email
    SQLDenis Thanks Brad, I will modify the code...I could have sworn I tested with that :-(
    03/24/10 @ 17:12
    Comment from: SQLDenis [Member] Email
    SQLDenis Brad, I solved it buy using RIGHT(' ' + ...)

    example


    DECLARE @t TIME = '2010-03-24 22:20:01.800'
    select STUFF(RIGHT(' ' + CONVERT(VARCHAR(30),@t,100),7),6,0,' ')


    DECLARE @d DATETIME = '2010-03-24 22:20:01.800'
    select STUFF(RIGHT(' ' + CONVERT(VARCHAR(30),@d,100),7),6,0,' ')
    GO

    DECLARE @t TIME = '2010-03-24 02:20:01.800'
    select STUFF(RIGHT(' ' + CONVERT(VARCHAR(30),@t,100),7),6,0,' ')


    DECLARE @d DATETIME = '2010-03-24 02:20:01.800'
    select STUFF(RIGHT(' ' + CONVERT(VARCHAR(30),@d,100),7),6,0,' ')
    03/24/10 @ 17:22
    Comment from: ca8msm [Member] Email
    ca8msm "Okay but what if you are not on SQL Server 2008? No problem really, it is almost the same"

    Denis, you'll need to amend your first line to not set the value "inline" as well otherwise you'll get the "cannot assign a default value to a local variable" error.
    03/25/10 @ 05:06
    Comment from: SQLDenis [Member] Email
    SQLDenis Yes, you are right, can you tell I only have 2008 instances :-)

    I have fixed it
    03/25/10 @ 05:48

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)