Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert time into an integer in SQL Server

I have a date column and a time column that are integers

I converted the date portion like this

select convert(int, convert(varchar(10), getdate(), 112))

I thought I could do the same with this query that gives the time in HH:mm:ss

SELECT CONVERT(VARCHAR(8), GETDATE(), 108)

How do I convert just the time into an integer?

like image 258
user867621 Avatar asked Oct 16 '14 15:10

user867621


1 Answers

This should convert your time into an integer representing seconds from midnight.

SELECT (DATEPART(hour, Col1) * 3600) + (DATEPART(minute, Col1) * 60) + DATEPART(second, Col1) as SecondsFromMidnight FROM T1;
like image 149
DeadZone Avatar answered Nov 15 '22 07:11

DeadZone