Solved

sql server 8.0 - Formatting time and numbers

Posted on 2006-06-13
2
338 Views
Last Modified: 2010-04-17
Hi Experts,
In sql server, I need to format time part of the datetime field. Such as the datetime value in a column called checkintime is 2006-02-20 20:04:07. I want to format it as 20:04 (just the hour and minutes).
Also, I want to know what is the equivalent in sql server of Visual basic's "##"  format mask which, in VB6, always shows two-digit number by left-padding with 0 in case input value is a single-digit number. Thanks
0
Comment
Question by:uzairp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
dttri earned 250 total points
ID: 16899550
Hi,
You can use the CONVERT function to do that:
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
data_type: the destination format
expression: the value you want to convert
style: apply to datetime datatype

List of style is:
Style ID                 Style Type
0 or 100            mon dd yyyy hh:miAM (or PM)
101                   mm/dd/yy
102                   yy.mm.dd
103                   dd/mm/yy
104                   dd.mm.yy
105                   dd-mm-yy
106                   dd mon yy
107                   Mon dd, yy
108                   hh:mm:ss
9 or 109            mon dd yyyy hh:mi:ss:mmmAM (or PM)
110                   mm-dd-yy
111                   yy/mm/dd
112                   yymmdd
13 or 113          dd mon yyyy hh:mm:ss:mmm(24h)
114                   h:mi:ss:mmm(24h)
20 or 120          yyyy-mm-dd hh:mi:ss(24h)
21 or 121          yyyy-mm-dd hh:mi:ss.mmm(24h)
126                  yyyy-mm-dd Thh:mm:ss.mmm(no spaces)
130                  dd mon yyyy hh:mi:ss:mmmAM
131                  dd/mm/yy hh:mi:ss:mmmAM

Eg: convert(varchar(5),DateColumn,108) to display only the time part, vachar(5) to get only the first 5 characters (hour and minute)
0
 
LVL 4

Expert Comment

by:imperial_p79
ID: 16900362
declare @test datetime
set @test = '2006-02-20 20:04:07'
select convert(varchar(5),@test,108)

Hope this helps
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Six Sigma Control Plans

627 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