Solved

Converting Smalldatetime to Military Time

Posted on 2001-08-30
6
1,261 Views
Last Modified: 2012-06-22
I am using Sql Server 2000.  I am using the following code to change a smalldatetime to a military time:

cast(DATEPART(month, a.completion_date_time) as varchar)+ '/' +
--    cast(DATEPART(day, a.completion_date_time) as varchar) + '/' +
--    cast(DATEPART(year, a.completion_date_time) as varchar) + ' ' +
--    cast(DATEPART(hh, a.completion_date_time) as varchar) +
--    cast(DATEPART(n, a.completion_date_time) as varchar)

The purpose is to make the smalldatetime a military time.  For example, change 03/10/01 06:30 to 03/10/01 0630.  This code uses the Datepart function to pick out the month, day, year, hour, and minute.  Using the cast, it changes the smalldatetime to a varchar datatype.  It concatenates the date parts into one date with a military.  

Problem:  The system omits the zeros in the hour and minute dateparts.  For example, it is suppose to be:   03/10/01 0630 instead of 03/10/01 63.

Thanks for help

BirdsOfFire1
0
Comment
Question by:BirdsOfFire1
  • 3
  • 2
6 Comments
 
LVL 18

Expert Comment

by:nigelrivett
ID: 6442672
convert(char(11),getdate(),103) + replace(convert(varchar(5),getdate(),108),':','')
0
 
LVL 18

Accepted Solution

by:
nigelrivett earned 100 total points
ID: 6442689
Sorry - 2 digit year.
convert(char(9),getdate(),03) + replace(convert(varchar(5),getdate(),108),':','')
0
 
LVL 18

Expert Comment

by:nigelrivett
ID: 6442699
If you want to 0 pad your digits you could

right('00' + convert(varchar(2),DATEPART(month, a.completion_date_time)),2)

This is a general way to right/left justify any string in almost any language.

0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 44

Expert Comment

by:bruintje
ID: 6442708
Hi, don't have a SQL server here but something like

Right("0" + cast(DATEPART(hh, a.completion_date_time) as varchar), 2)

should do the trick, the extended zero will drop off if hours are 9 > and will be added to hours < 10

HTH:O)Bruintje
0
 
LVL 44

Expert Comment

by:bruintje
ID: 6442710
oops should refresh
0
 

Author Comment

by:BirdsOfFire1
ID: 6448470
Thanks for answering my call and thanks to the other MS-SQL experts answering my call.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

831 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