Solved

Convert int to military time

Posted on 2016-11-25
8
32 Views
Last Modified: 2016-11-25
How do I convert and int to military time?

example:

Int
7      = 0700
19    = 1900
23    = 2300
0
Comment
Question by:MikeM670
8 Comments
 
LVL 46

Expert Comment

by:Vitor Montalvão
ID: 41901695
It's only for the hours? No minutes?
If so you can try this:
SELECT REPLICATE('0', 2-LEN(CAST(ColumName AS VARCHAR(4))))+CAST(ColumName AS VARCHAR(4)) + '00'
FROM TableName

Open in new window

0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 41901706
If it is just the hours then:

select right('0' + cast((colname * 100) as nvarchar(2)), 4)

Open in new window


Basically multiply by 100 and prefix with a 0 and return the right 4 chars
0
 

Author Comment

by:MikeM670
ID: 41901716
Victor,

Yes it's only for the hours.  I'm using a stored procedure that uses:
SET @8hrfirstshiftstart = 7
SET @8hrfirstshiftend = 15
ect....

to find the start and end of shifts.   But I want to return the  7 or other values from the stored procedure to use as labels in crystal reports.

Like this...

0700 - 1500
0
 
LVL 23

Expert Comment

by:Pawan Kumar
ID: 41901721
Try.. Easiest One :)

DECLARE @T AS INT = '7'
SELECT LEFT(@t * 1000,4)

Open in new window


trial


DECLARE @T AS INT = '7'
SELECT LEFT(@t * 1000,4)

GO

DECLARE @T AS INT = '19'
SELECT LEFT(@t * 1000,4)

GO

DECLARE @T AS INT = '23'
SELECT LEFT(@t * 1000,4)

GO

DECLARE @T AS INT = '1'
SELECT LEFT(@t * 1000,4)

Open in new window


Output

----
7000

(1 row(s) affected)


----
1900

(1 row(s) affected)


----
2300

(1 row(s) affected)


----
1000

(1 row(s) affected)

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:MikeM670
ID: 41901726
Pawan,

Unfortunately that won't work for time values like

7    =    0700
1    =    0100

----
7000

(1 row(s) affected)


----
1900

(1 row(s) affected)


----
2300

(1 row(s) affected)


----
1000

Open in new window

0
 
LVL 46

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 41901730
So use this in your SP:
DECLARE @8hrfirstshiftstart INT = 7
DECLARE @8hrfirstshiftend INT = 15

SELECT REPLICATE('0', 2-LEN(CAST(@8hrfirstshiftstart AS VARCHAR(4))))+CAST(@8hrfirstshiftstart AS VARCHAR(4)) + '00 - '
     + REPLICATE('0', 2-LEN(CAST(@8hrfirstshiftend AS VARCHAR(4))))+CAST(@8hrfirstshiftend AS VARCHAR(4)) + '00' 

Open in new window


Result:
--------------------------------
0700 - 1500

(1 row(s) affected)
0
 

Author Comment

by:MikeM670
ID: 41901731
Lee,

Arithmetic overflow error converting expression to data type nvarchar

Open in new window



Returns a error:
Arithmetic overflow error converting expression to data type nvarchar
0
 

Author Closing Comment

by:MikeM670
ID: 41901732
That is exactly what I needed.  Thanks!
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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

932 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

18 Experts available now in Live!

Get 1:1 Help Now