SQL statement DateTime format - sql

I am looking for some help in regards to the format that I want my data outputted.
Below is a snippet of the code I am using. It is currently converting the value from the database in seconds and outputting it like
1d 12:05:52
I want it to output the information so it calculates the day in the hours, so basically dropping the '1d' like below
36:05:52
CAST(FLOOR([Running] / 86400) AS VARCHAR(10))+'d ' + CONVERT(VARCHAR(8), DATEADD(SECOND, [Running], '19000101'), 8) AS [Running]
Can someone please point me in the right direction using the code above?
Thanks in advance for your help.

This should work:
SELECT
CASE WHEN [Running]/3600 <= 9 THEN '0' ELSE '' END +
CONVERT(VARCHAR(10),[Running]/3600)+':'+
RIGHT('00'+CONVERT(VARCHAR(2),([Running]%3600)/60),2)+':'+
RIGHT('00'+CONVERT(VARCHAR(2),[Running]%60),2) AS [Running]
I tested it using this:
DECLARE #Running int
SET #Running = 60*60*24*30 + 60*3 + 3 -- should output 720:03:03
SELECT
CASE WHEN #Running/3600 <= 9 THEN '0' ELSE '' END +
CONVERT(VARCHAR(10),#Running/3600)+':'+
RIGHT('00'+CONVERT(VARCHAR(2),(#Running%3600)/60),2)+':'+
RIGHT('00'+CONVERT(VARCHAR(2),#Running%60),2) AS [Running]
Output:
Running
----------------
720:03:03
(1 row(s) affected)

As #Hadi said in his comment, you can use the TimeSpan object in VB.Net (you've tagged the question with this so it seems reasonable to suggest), but you could also use this bit of SQL instead, which I think is slightly simpler than the other suggestion :
CAST(CAST(FLOOR([Running] / 3600) AS INT) AS VARCHAR) +
RIGHT(CONVERT(VARCHAR, DATEADD(SECOND, [Running], '1900-01-01'), 108), 6) as [Running]

Here's one more way to do it, good answers here already tho. :)
-- Setting param for testing purposes, replace this with actual column in the formula below
DECLARE #SECS INT
SET #SECS = 3787*26
-- Your original formula for 'D' value
SELECT CAST(FLOOR(#SECS / 86400) AS VARCHAR(10))+'d ' + CONVERT(VARCHAR(8), DATEADD(SECOND, #SECS, '19000101'), 8)
-- New one for HH:MM:SS
SELECT CAST(#SECS/3600 AS VARCHAR(20))+':'+RIGHT('0'+CAST((#SECS%3600)/60 AS VARCHAR(2)),2)+':'+RIGHT('0'+CAST(#SECS%60 AS VARCHAR(2)),2)

Related

SQL Server Converting Decimal into Time hh:mm, I need the total hours

I need to be able to get the total hours and minutes if it is over 24 hours from a decimal column in SQL Server.
This is the code I am using:
CAST(CAST(DATEADD(SECOND, 1654.86 * 60, 0) AS Time(0)) AS VARCHAR(5))
Since it's over 24 hours the output is "03:34" I would like for it to be "27:34" or if possible to tell me it will take 3 working days and "03:34" (not sure how that would work).
Thank you in advance! Paul
As explained in the comments, the time data type is not designed to represent any sort of interval or timespan, it is only designed to represent clock time. As such, it is not capable of displaying 27 hours. Instead you need to build this string yourself with methods other than simple CAST as type:
DECLARE #d table(decimal_column decimal(15,2));
INSERT #d(decimal_column) VALUES(1654.86);
SELECT d.decimal_column,
nice_time = CONVERT(varchar(11), FLOOR(h)) + ':'
+ RIGHT('0' + CONVERT(varchar(11), FLOOR(m)), 2)
FROM #d AS d
CROSS APPLY
(
VALUES(d.decimal_column/60, d.decimal_column%60)
) AS extracted(h,m);
Results:
decimal_column
nice_time
1654.86
27:34
Example db<>fiddle
You may have edge cases where you want actual rounding logic instead of FLOOR() - but if you have those cases, include them in your question so we know the desired output.
select CONVERT(VARCHAR, CONVERT(INT, 1654.86/60)) + ':'
+ CONVERT(VARCHAR, CONVERT(INT, 1654.86-(CONVERT(INT, 1654.86/60)*60)))
You can create a function for this query, it is very performance. Because we will not use the same operations in multi sections.

SQL Server: convert and merge separate date and time columns (both integers)

I am working on a query, where I have to fill a table's column ([Result_DateTime]) with datetime values.
The datetime based on two columns, both integer. One contains the date and the other is the time, as it is.
As you can see from the picture, it is a bit difficult to merge and convert these values to an actual datetime, because of the way they are stored. Mainly the time value causing problems.
I concluded how to convert the date column:
CONVERT(DATETIME, LEFT(20200131, 8))
but then I got stuck - what to do with the time and how to merge the two into one datetime effectively?
Using function STUFF looks nasty...
Could you help me out please? I am using SQL Server 2014
Below is one method to do it:
SELECT CAST(Convert(DATE, LEFT(DATEUPDT, 8)) AS VARCHAR(10)) +' '+CAST (TIMEUPDT/100 AS VARCHAR(4)) + ':' + CAST(TIMEUPDT%(100 * (TIMEUPDT/100)) AS VARCHAR(10))+':00'
FROM TEST_TABLE_TIME;
I think I found one solution. What I tried is to avoid using varchar conversions because of how the time column's zeros are cut off. However, I am not convinced that this is the most effective way to do so:
DECLARE #DateInt int = 20200131
DECLARE #TimeInt int = 345 -- 03:45:00
SELECT CONVERT(DATETIME, LEFT(#DateInt, 8)) +
CAST(DATEADD(second, FLOOR(#TimeInt / 100) * 3600 + FLOOR(#TimeInt / 1) % 100 * 60, 0) as datetime)
I was testing it with various time values, it is working.

Select length as minute:seconds format

Trying to convert seconds to a minute:seconds format.
e.g. 207 seconds would be 3:27
I have a table with column length that has the length of songs stored in seconds.
Using this query almost works, however, when a song should be 3:03 it will instead show 3:3
select concat(Length/60, ':', Length%60) as Length from songs
SELECT Convert(nvarchar, (Length/60)) + ':' + RIGHT('0' + Convert(nvarchar, Length%60), 2) as Length from songs
I build this using a varaible since I didn't have your table, but try this:
declare #length int = 207
select concat(#Length/60, ':', case len(#Length%60) when 1 then '0' else '' end, #Length%60) as [leng]
You can use dateadd to do that:
select convert(varchar, dateadd(second, 187, 0), 108)
This returns "00:03:07" so you can cut away hours if you don't need them.

Adding two timestamps in SQL

I have two fields in sql date1 varchar(4) and date2 varchar(4). The date format is just HHmm where HH is the hours and mm is the minutes.
Is there anyway in SQL which we can add the two timestamps together and work out how many hours and minutes?
e.g.
date1 = 0230 date2 = 0145 will total 0415
date1 = 0030 date2 = 0035 will total 0105
declare #d1 varchar(4), #d2 varchar(4), #dif int
set #d1 = '0230'
set #d2 = '0145'
set #dif = (CAST(left(#d1, 2) as int) * 60) + (CAST(left(#d2, 2) as int) * 60) + CAST(right(#d1, 2) as int) + CAST(right(#d2, 2) as int)
select RIGHT('00' + cast(floor(#dif / 60) as varchar), 2) + RIGHT('00' + CAST(#dif % 60 as varchar),2)
Not sure these are really timestamps, what not store them as integers?
Anyhow.. not sure there is a mysql function for this.
You can browse: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
Not sure it will offer much help.
If you store them as seconds, then you can simply have mysql add them, otherwise pull them into a script, convert them to seconds, add them together and then convert to a more readable format if desired.
Try this (use time or date time):
select ADDTIME(time1,time2);
In mysql time is of the form
'01:02:03'
and date time is
'2003-12-31 01:02:03'
IF you happen to be using SQL Server you may also be able to use the DATEDIFF function described here: http://msdn.microsoft.com/en-us/library/ms189794.aspx
This will let you get the difference between any part of a datetime value. DATEADD may be of use for this as well depending on how you want to handle it.

sql cast hour out of datetime without dropping leading zero on single digit hours

CAST(DATEPART(hh, timestamp) AS varchar(2)) + ':00' AS Hour
This will get me the hour out of a timestamp field but in the case of the hours 0-9 it does not pad a leading zero and therefore when I sort by hour descending it does not sort correctly.
Not sure what is wrong here. I specify a 2 char varchar to allow extra room for the leading zero. Hopefully there is a way to fix this without passing my field through a function that will pad the leading zero for me.
SELECT RIGHT('0' + CONVERT(VARCHAR(2), DATEPART(HOUR, GETDATE())), 2) + ':00';
Don't want to pad with 0s? OK, you can do it a similarly ugly way without the padding mess:
SELECT LEFT(CONVERT(TIME(0), GETDATE()), 2) + ':00';
Obviously replace GETDATE() with your column name. Which I hope isn't really timestamp because this is a reserved word - for a data type that has nothing to do with date or time, sadly.
If you don't like either of those solutions, then just select the data from SQL Server and let your client application handle formatting/presentation details. Surely this is easy to do with C#'s format() function, for example.
Assuming timestamp is a column in your table
select convert(char(3), timestamp, 108) + '00' AS Hour
from yourtable
Alternative
select left(cast(dateadd(hh, datediff(hh, 0, timestamp), 0) as time), 5)
from yourtable
Edit:
After testing a bit i came to the conclusion that this is the fastest way (almost the performance and syntax as Aaron's solution)
SELECT RIGHT(100 + DATEPART(HOUR, timestamp) , 2) + ':00'
from yourtable
You can use the TIME type which is 24 hours by default;
cast(cast(timestamp as time) AS varchar(2)) + ':00' AS Hour