Solved

sql server 8.0 - Formatting time and numbers

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

758 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

24 Experts available now in Live!

Get 1:1 Help Now