?
Solved

Converting Smalldatetime to Military Time

Posted on 2001-08-30
6
Medium Priority
?
1,279 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
[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
  • 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 400 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

765 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