Snippet: Select the Time portion from a datetime

SQL Server 2000

Earlier on I wrote about how you can Select just the date part from a DateTime or SmallDateTime data type. (See Snippet: Select a Date or Current Date Without Time)

Using the CONVERT( ... , ... , style ) function we can specify the style attribute, that lets us specify to SQL Server (using style 14 or 114) that we just want the time portion.

-- Get current time (without date) in 24-hour time:
SELECT CONVERT( CHAR(8), GetDate(), 14)

By changing the number of CHAR characters you can select the time with or without milliseconds and seconds.

-- Get current time (without date) in HH:MM:SS:mmm format:
SELECT CONVERT( CHAR(12), GetDate(), 14)

-- Get current time (without date) in HH:MM:SS format:
SELECT CONVERT( CHAR(8), GetDate(), 14)

-- Get current time (without date) in HH:MM format:
SELECT CONVERT( CHAR(6), GetDate(), 14)

Also see Snippet: Select a Date or Current Date Without Time



Share
  • Facebook
  • Google Bookmarks
  • Ask
  • LinkedIn
  • Socialogs
  • Wikio
  • Digg
  • Twitter
  • SlashDot
  • Reddit
  • MySpace
  • Del.icio.us
  • Blogter
  • BlogMemes
  • Yahoo Buzz






Snippet: Select the Time portion from a datetime | 0 comments | Create New Account
The following comments are owned by whomever posted them. This site is not responsible for what they say.


Gold Coast Aquarium Maintenance | Gold Coast Marine Fish | Gold Coast Tropical Fish
Jewel Jones - Counselling Service - Penrith, Richmond
 
     
 Copyright © 2017 Julian Kuiters
 All trademarks and copyrights on this page are owned by their respective owners.