I am trying to convert a field with positive and negative seconds to HH:MM:SS but failing in the process.
CONVERT(VARCHAR(6), RPAYCODE.TIMEINSECONDS/3600) +
':' +
RIGHT('0' + CONVERT(VARCHAR(2), (RPAYCODE.TIMEINSECONDS % 3600) / 60), 2)
AS "Pay Code Hrs"
When the seconds are positive it works fine = 00:30:00
When the seconds are negative I get this = 0:0*
This should handle the negative values:
CASE
WHEN #time_in_seconds < 0 THEN '-'
ELSE ''
END +
CONVERT(VARCHAR(6), ABS(RPAYCODE.TIMEINSECONDS)/3600) + ':' +
RIGHT('0' + CONVERT(VARCHAR(2), (ABS(RPAYCODE.TIMEINSECONDS) % 3600)/60), 2) AS [Pay Code Hrs]
Lean and simple conversion
Skip all those unnecessary calculations and string operations:
CASE WHEN RPAYCODE.TIMEINSECONDS < 0 THEN '-' ELSE '' END -- sign
+ CONVERT(varchar, DATEADD(s, ABS(RPAYCODE.TIMEINSECONDS), 0), 108) -- hh:mm:ss
Number format 108 is hh:mm:ss.
Related
I am currently migrating all of my company's reports into Splunk Data Labs input for ingestion. The reports create temp tables using the CREATE TABLE format, which is incompatible with Splunk, however, SELECT INTO format works just fine.
The error that I am getting however when changing to the SELECT INTO format, is the DATETIME variable which should be MM/DD/YYYY hh:mm format loses the hh:mm end, and instead shows MM/DD/YYYY MM/DD/YYYY:
Original SQL:
CREATE TABLE #Stats#(date_slice DATETIME NULL, raw_value REAL NULL)
INSERT INTO #Stats#
SELECT CONVERT(CHAR(11), data_datetime, 111) + ' ' +
CASE WHEN DATEPART(MINUTE, data_datetime) < 30 THEN
RIGHT('0' + LTRIM(str(DATEPART(hour, data_datetime))), 2) + ':00' ELSE
RIGHT('0' + LTRIM(str(DATEPART(hour, data_datetime))), 2) + ':30' END AS date_hour
,SUM(ship_qty) AS moves
FROM #tmpAllData
GROUP BY CONVERT(CHAR(11), data_datetime, 111) + ' ' +
case when DATEPART(minute, data_datetime) < 30 THEN
RIGHT('0' + LTRIM(str(DATEPART(hour, data_datetime))), 2) + ':00' ELSE
RIGHT('0' + LTRIM(str(DATEPART(hour, data_datetime))), 2) + ':30' END
ORDER BY 1
Modified SQL:
--CREATE TABLE #Stats#(date_slice DATETIME NULL, raw_value REAL NULL)
SELECT CONVERT(CHAR(11), data_datetime, 111) + ' ' +
CASE WHEN DATEPART(MINUTE, data_datetime) < 30 THEN
RIGHT('0' + LTRIM(str(DATEPART(hour, data_datetime))), 2) + ':00' ELSE
RIGHT('0' + LTRIM(str(DATEPART(hour, data_datetime))), 2) + ':30' END date_slice
,SUM(ship_qty) raw_value
INTO #Stats#
FROM #tmpAllData
GROUP BY CONVERT(CHAR(11), data_datetime, 111) + ' ' +
case when DATEPART(minute, data_datetime) < 30 THEN
RIGHT('0' + LTRIM(str(DATEPART(hour, data_datetime))), 2) + ':00' ELSE
RIGHT('0' + LTRIM(str(DATEPART(hour, data_datetime))), 2) + ':30' END
ORDER BY 1
Original Output: "07/12/2018 10:00:00 "
Modified Output: "2018/07/12 2018/07/12"
You second statement is creating the temporary table #Stats without any preset column definitions. It instead creates the columns based on the return data type of the SELECT
This means that the SQL Server is not reading the output in question as a DATETIME but instead as a STR.
I would try to use a CONVERT in your modified statement to see if you get different functionality.
It sounds like this question is mostly for your curiosity so I will add that the original statement is the standard way to accomplish what you've outlined.
This is because SELECT... INTO statements are harder to read and revise for new users and because they can lead to some unexpected functionality as you have displayed above.
So, similar to what Edward had said. The query I had posted was returning a DATETIME data type, however, at the very end of the query when it selects all information to display to the user, it got converted to CHAR somewhere in between, so I just converted it to DATETIME before converting to CHAR and splitting up the values.
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
I have a column Time in my table. It holds a time value in minutes, ie: 605. What I want it to show it as 10:05, as hh:mm format.
I am doing it like this:
...
Time = cast(AVG(Time) / 60 as varchar) + ':' + cast(AVG(Time) % 60 as varchar),
...
It shows the value as 10:5, but I want to make it as 10:05 and I couldn't find out a way to add '0' before '5' in the minutes section.
Any help would be appreciated.
Try this:
SELECT
TIME = CASE WHEN #t < 600 THEN '0' ELSE '' END +
CAST(Time / 60 as varchar(10)) + ':' +RIGHT(100 +(Time % 60), 2)
Example:
DECLARE #t int = 55121
SELECT
TIME = CASE WHEN #t < 600 THEN '0' ELSE '' END +
CAST(#t / 60 as varchar(10)) + ':' +RIGHT(100 +(#t % 60), 2)
Result:
Time
918:41
You could simply add a Right('0' + ...,2) expression as below to get the '0' in there.
Time = cast(AVG(Time) / 60 as varchar) + ':' + RIGHT('0'+cast(AVG(Time) % 60 as varchar),2)
To answer your question in comments, getting DD:HH:MM is a similar setup.
DayTime = cast(AVG(Time) / 1440 as varchar) + ':' + RIGHT('0' + cast((AVG(Time) / 60) % 24 as varchar),2) + ':' + RIGHT('0'+cast(AVG(Time) % 60 as varchar),2)
You can use a case expression to do this.
Also you should always specify the length of varchar when you use cast, or it would only show the first character.
case when len(cast(AVG(Time)%60 as varchar(2))) < 2 then '0'+cast(AVG(Time)%60 as varchar(2))
else cast(AVG(Time)%60 as varchar(2))
end
Running the queries below i get the output '16:6' and '3:40'.
It shows the zero on the second one, but for the first it shows '16:6'
The first one makes it look like '16:60' which by time would be wrong.
How can I make it show '16:06' and not mess up anything else?
SELECT
right('0' + convert(float,datediff (second, '2015-02-09 10:58:42.202','2015-02-09 11:14:48.245')/ 60 ), 2) + ':' +
right('0' + convert(float,datediff (second, '2015-02-09 10:58:42.202','2015-02-09 11:14:48.245')% 60),2) as total_time
SELECT
right('0' + convert(float,datediff (second, '2015-02-09 08:07:35.284','2015-02-09 08:11:15.863')/ 60 ), 2) + ':' +
right('0' + convert(float,datediff (second, '2015-02-09 08:07:35.284','2015-02-09 08:11:15.863')% 60),2) as total_time
Use VARCHAR() instead of FLOAT:
SELECT
right('0' + convert(VARCHAR(4),datediff (second, '2015-02-09 10:58:42.202','2015-02-09 11:14:48.245')/ 60 ), 2) + ':'
+right('0' + convert(VARCHAR(4),datediff (second, '2015-02-09 10:58:42.202','2015-02-09 11:14:48.245')% 60),2) as total_time
The point of the RIGHT('0'+ ... ),2) portion is to add a leading zero to 1-digit values, but it only works on strings.
If you want to show more than 2 digits for the minutes portion you'll have to change how you go about adding leading zeroes.
Assuming this is T-SQL, with one datediff:
SELECT
right(convert(varchar, dateadd(second, datediff(second, '2015-02-09 10:58:42.202','2015-02-09 11:14:48.245'), 0), 108), 5) as total_time
The code below is doing just what i want it to do, as far as converting the time to a format that works for my needs. However, when the minutes and seconds are in the single digit, the format is not looking good. I would like to adjust the code to pad the minutes and seconds where it is needed. Any suggestions? Again, i would like to stay with the existing code as much as possible.
Thank you!
SELECT SUBSTRING(CONVERT(CHAR(14), DateTimeRaw, 100), 13, 2) + ':' +
CONVERT(nvarchar, DATEPART(minute, DateTimeRaw)) + ':' +
CONVERT(nvarchar, DATEPART(second,
DateTimeRaw)) + '.' + CONVERT(nvarchar, DATEPART(ms, DateTimeRaw) / 100)
+ ' ' + CONVERT(nvarchar, CASE WHEN datepart(hour, DateTimeRaw)
< 12 THEN 'AM' ELSE 'PM' END) AS AGMPLUSMSTIME
FROM RAW
RIGHT('00' + CONVERT(nvarchar, DATEPART(minute, DateTimeRaw)), 2)
Basically: concatenate 2 zeroes with your time value, ending up with something like 001, 0023, or 0059, then take the 2 right-most characters, leaving you with 01, 23, 59 respectively.
How about this:
select right(stuff(convert(varchar(26), getdate(), 9), 23,2, ' '), 13)
Result:
4:35:15:4 PM
I used getdate() instead of your field, just as an example