Solved

sql server 8.0 - Formatting time and numbers

Posted on 2006-06-13
2
334 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel object stays open 19 70
python question 5 69
Currency Conversion? 1 67
How to insert gas or chemical names in SQL server. For e.g. CO2 to CO₂. 5 67
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

939 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

Need Help in Real-Time?

Connect with top rated Experts

4 Experts available now in Live!

Get 1:1 Help Now