Datetime field using military time - need time only in standard time

后端 未结 3 496
野性不改
野性不改 2021-01-19 01:13

I have a datetime field in SQL Server 2008 which stores the date and time in military format (or international format if you prefer)

examples:

2011-         


        
相关标签:
3条回答
  • 2021-01-19 01:30

    As you requested this in T-SQL, you might want to look at the CAST and CONVERT syntax which specifically lists various date and time formats.

    For example:

    select convert(varchar, getdate(), 100)

    Would give you:

    Feb 3 2012 3:26PM

    0 讨论(0)
  • 2021-01-19 01:40

    DateTime is stored in an internal representation - there is no format associated. When you need to display the DateTime, specify the formatting you want for converting the DateTime into a string.

    It is much better to let the application decide on formatting than formatting in SQL.

    See standard and custom Date and Time Format Strings for more information.

    0 讨论(0)
  • 2021-01-19 01:44

    One way is:

    Convert to varchar, which will give you:

    Select CONVERT(varchar, @dt, 100) -- where @dt is your date time
    

    Feb 15 2011 3:30PM

    Subsequently, to remove the date, and get 7 chars from the right, which is the most, and TRIM is added just for extra safety, but probably isn't needed.

    Select LTRIM(RIGHT(CONVERT(varchar, @dt, 100),7))
    

    Will give you 3:30PM

    Side Note: Denali makes this easier, no more magic numbers

    0 讨论(0)
提交回复
热议问题