Format hh:mm with SQL Server - sql

I'm using
CONVERT( CHAR(5), DATEADD(n, value),0), 108)
to convert minutes to a hh:mm format, but it breaks when I have more minutes then fit in 24hrs. E.g. 1440 gives 00:00 instead of 24:00.
How can I fix that?

Consider using the % modulo operator:
select cast(n/60 as varchar(20)) + ':' +
right('0' + cast(n%60 as varchar(2)), 2)

Try something like this:
declare #minutes int
select #minutes = 1440
select convert(varchar, #minutes/60) + ':' + right ('0'+convert(varchar, #minutes%60), 2)

Related

Unable to get difference between 2 dates in required format

My start and finish columns are in the format Yyyy-mm-dd HH:mm:ss.ms I want the difference between the two in HH:mm:ss.ms format. How do I go about this?
My query looks like this:
select *, convert(time,
Dateadd(s,
Datediff(s, A.Finish, A.Start),
Cast('1900-01-01 00:00:00.000000' as datetime2)
)
) as dif
from (
select *,
dateadd(s,convert(int,left(start,10)),'1970-01-01') as Start,
dateadd(s,convert(int,left(finish,10)),'1970-01-01') as Finish,
from tableB
) A
order by dif asc
I've converted unix time stamps to standard format in inner query. When I run this the start date and start time appear as '2019-12-11 15:45:20.000' and '2019-12-12 15:45:17.000' but my dif appears as '00:00:03',which is wrong.
Any help would be appreciated
I have used two sources to try to help you:
https://www.rodolforodarte.com/2011/05/using-datediff-to-display-hhmmss/
AND
Show datediff as seconds, milliseconds
Here is my result code:
DECLARE #START_DATE DATETIME
DECLARE #END_DATE DATETIME
SET #START_DATE = '2011-01-01 16:00:22.000'
SET #END_DATE = '2011-01-01 22:47:21.022'
SELECT CONVERT(varchar(6), DATEDIFF(MILLISECOND, #START_DATE, #END_DATE)/3600000)
+ ':'
+ RIGHT('0' + CONVERT(varchar(2), (DATEDIFF(MILLISECOND, #START_DATE, #END_DATE) % 3600000) / 60000), 2)
+ ':'
+ RIGHT('0' + CONVERT(varchar(2), ((DATEDIFF(MILLISECOND, #START_DATE, #END_DATE) % 3600000) % 60000) / 1000), 2)
+ ':'
+ RIGHT('000' + CONVERT(varchar(2), (((DATEDIFF(MILLISECOND, #START_DATE, #END_DATE) % 3600000) % 60000) % 1000)), 3) AS 'HH:MM:SS:MS'
And here is a small demo

Convert from bigint to time only

I have big-int data, and I want to convert it from Big-int to just time, for example I have this value 53006745 for ' 14hrs 43min '
Please help me to write the query in SQL Server, so that will convert big-int into minutes.
If You're looking Pure Time you could try below SQL Query which will give Minutes from Your Integer Value:
DECLARE #value INT = 53006745
SELECT DATEPART(MINUTE, DATEADD(s, CONVERT(BIGINT, #value) / 1000, CONVERT(DATETIME, '1-1-2017 00:00:00'))) [Minutes];
Result :
Minutes
43
You can use this code for yourself in SQL :
SELECT DATEADD(s, CONVERT(BIGINT, 53006745 ) / 1000,
CONVERT(DATETIME,
'1-1-1970 00:00:00'))
This code return 1970-01-01 14:43:26.000 as you like
DECLARE #Value INT = 53006745
SELECT
CONVERT(VARCHAR, #Value/3600000) + ':' -- Hours
+ RIGHT('0' + CONVERT(VARCHAR, #Value%3600000/60000), 2) + ':' -- Minutes
+ RIGHT('0' + CONVERT(VARCHAR, #Value%3600000%60000/1000), 2) + '.' -- Seconds
+ RIGHT('00' + CONVERT(VARCHAR, #Value%1000), 3) -- Milliseconds

Convert smallint to time

I am trying to convert a smallint to a time format.
I am using SQL Server 2005 which doesn't support the time datatype.
I am storing the time as 1400 which is a smallint. When retrieving I want it to be converted to a time format. Such as 14:00
Any ideas or guidance on the matter. If there is an easier way to do it or if the way I am trying is possible?
You can get a result as varchar by using this:
SELECT
RIGHT('0' + CONVERT(varchar(10), yourTime / 100), 2) + ':' +
RIGHT('0' + CONVERT(varchar(10), yourTime % 100), 2) As timeString
FROM
yourTable
You can also have a result in DATETIME format like this:
SELECT
CONVERT(datetime, CONVERT(varchar(10), yourTime / 100)+ ':' + CONVERT(varchar(10), yourTime % 100))
FROM
yourTable
In SQL Server 2012+ you can have a result in time format:
SELECT
TIMEFROMPARTS(yourTime / 100, yourTime % 100, 0, 0, 0) As timeFormat
FROM
yourTable

SQL: float number to hours format

Is there a easy way to format a float number in hours in Ms SQL server 2008?
Examples:
1.5 -> 01:30
9.8 -> 09:48
35.25 -> 35:15
Thanks a lot.
I like this question!
DECLARE #input float = 1.5;
DECLARE #hour int = FLOOR(#input);
DECLARE #minutes int = (SELECT (#input - FLOOR(#input)) * 60);
SELECT RIGHT('00' + CONVERT(varchar(2), #hour), 2) + ':' + RIGHT('00' + CONVERT(varchar(2), #minutes), 2);
SELECT SUBSTRING(CONVERT(NVARCHAR, DATEADD(MINUTE, 1.5*60, ''), 108), 1, 5)
This works by:
starting from the "zero" date
adding 1.5 x 60 minutes (i.e. 1.5 hours)
formatting the result as a time, hh:mm:ss (i.e. format "108")
trimming off the seconds part
It is necessary to use 1.5 x 60 minutes instead of 1.5 hours as the DATEADD function truncates the offset to the nearest integer. If you want high-resolution offsets, you can use SECOND instead, suitable scaled (e.g. hours * 60 * 60).
Sure. Easy, but not exactly...straightforward:
declare #hours float
set #hours = -9.8
select substring('- ',2+convert(int,sign(#hours)),1) -- sign
+ right('00' + convert(varchar, floor(abs(#hours))) , 2 ) -- hours component
+ ':' -- delimiter
+ right('00' + convert(varchar,round( 60*(abs(#hours)-floor(abs(#hours))) , 0 ) ) , 2 ) -- minutes
Another option that will give the correct result. You might need to tweak it to round minutes and to ensure that both fields are 2 digits wide.
declare #hours float
set #hours = -9.8
select convert(varchar, datediff(minute,dateadd(minute,#hours*60,convert(datetime,'')),'') / 60 )
+ ':' + convert(varchar, datediff(minute,dateadd(minute,#hours*60,convert(datetime,'')),'') % 60 )
WITH m AS
SELECT Minutes = CAST(#hours * 60 AS int)
)
SELECT CAST(Minutes / 60 AS varchar) + ':' + RIGHT(100 + Minutes % 60, 2)
FROM m
select dateadd(MINUTE, cast((8.18 % 1) * 60 as int), dateadd(hour, cast(8.18 as int), convert(varchar(10), getdate(), 10)))

Datetime format help

I want only Hr and Min in time format so i did
select convert(varchar(20),GETDATE(),108) ===>> output is "12:57:54"
but i want only "12:57" this much so how i will do that in sql server
select convert(varchar(5),GETDATE(),108)
Select cast(DatePart(hour, getdate()) as varchar(2)) + ':' + cast(DatePart(minute, getdate())as varchar(2))