INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Date and time Tips and tricks

Time data type and convertion to by djj55
Posted: 7 May 10 (Edited 7 May 10)

A new data type is TIME (time [(fractional second precision)]) it "Defines a time of a day. The time is without time zone awareness and is based on a 24-hour clock." (see BOL http://msdn.microsoft.com/en-us/library/bb677243.aspx)

The new help Microsoft has for the CAST/CONVERT does not go into detail about TIME and I wanted to have a time column so I did this simple test

CODE

DECLARE @mydatetime AS DATETIME = GETDATE()
SELECT @mydatetime
    , CAST(CONVERT(VARCHAR, @mydatetime, 108) AS TIME) AS MTime
    , CAST(CONVERT(VARCHAR, @mydatetime, 108) AS TIME(0)) AS MTime0
    , CAST(CONVERT(VARCHAR, @mydatetime, 108) AS TIME(1)) AS MTime1
    , CAST(CONVERT(VARCHAR, @mydatetime, 108) AS TIME(2)) AS MTime2
    , CAST(CONVERT(VARCHAR, @mydatetime, 108) AS TIME(3)) AS MTime3
    , CAST(CONVERT(VARCHAR, @mydatetime, 108) AS TIME(4)) AS MTime4
    , CAST(CONVERT(VARCHAR, @mydatetime, 108) AS TIME(5)) AS MTime5
    , CAST(CONVERT(VARCHAR, @mydatetime, 108) AS TIME(6)) AS MTime6
    , CAST(CONVERT(VARCHAR, @mydatetime, 108) AS TIME(7)) AS MTime7
    , CAST(@mydatetime AS TIME) AS STime
    , CAST(@mydatetime AS TIME(0)) AS STime0
    , CAST(@mydatetime AS TIME(1)) AS STime1
    , CAST(@mydatetime AS TIME(2)) AS STime2
    , CAST(@mydatetime AS TIME(3)) AS STime3
    , CAST(@mydatetime AS TIME(4)) AS STime4
    , CAST(@mydatetime AS TIME(5)) AS STime5
    , CAST(@mydatetime AS TIME(6)) AS STime6
    , CAST(@mydatetime AS TIME(7)) AS STime7
The tests shows that you can just use the CAST as time from a datetime value.  It also shows that a text string can be used.

Notice the fractional second precision values and the results.

 

Back to Microsoft SQL Server: Programming FAQ Index
Back to Microsoft SQL Server: Programming Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close