BigQuery Convert number of seconds into time format - google-bigquery

I'm trying to convert average total of seconds into time format
PARSE_TIME( '%T',
CAST(ROUND( IEEE_DIVIDE(SUM( totals.timeOnSite ), SUM(totals.visits)),0) AS STRING )) AS AvgSessionDuration,
code is valid, but it's giving me this error:
Failed to parse input string "245"

try below approach
time(timestamp_seconds(number_of_seconds_int64)) AS AvgSessionDuration
instead of PARSE_TIME( '%T', CAST(ROUND( IEEE_DIVIDE(SUM( totals.timeOnSite ), SUM(totals.visits)),0) AS STRING )) AS AvgSessionDuration

Related

Snowflake converting numeric value into HHMMSS format

I have a column that has number in it in the format of HHMMSS but the number is not consistent (not all have six digits), ex: "83455" "153651" "91251".
The number 83455 would mean 8:34 am and 55 as SS. I tried converting into varchar and use TO_TIME but the output is not the same as it is. Similarly, I also tried converting into timestamp then get the time from it but it just won't work. The output I want here would be 8:34:55, what is the best way to convert the number?
Try this. I split hours minutes and seconds and then concatenate them into time format.
SELECT
CAST(FLOOR(col / 10000) || ':' ||
FLOOR(MOD(col / 100, 100)) || ':' ||
MOD(col, 100) AS TIME) AS converted_time
FROM
yourtable
MOD()
An alternative approach is to use the built in function for this task.
TRY_TO_TIME().
Sometimes the built in functions are easier to read, understand, less typing, optimized (runs faster/cheaper).
SELECT
TRY_TO_TIME (DONT_WASTE, 'HH24MISS" )VOLIA
,TO_CHAR (VOLIA, 'HH12:MI:SS AM' ) AM_PM
FROM
(SELECT '83455 'DONT_WASTE UNION SELECT '153651 'UNION SELECT '91251')
The core of this problem is the format string was wrong
MM is Month, MI is minutes.
The first path I went down was slicing the string up, but the TO_TIME/TRY_TO_TIME both handle the smaller string of the pre lunchtime, but does not handle no hours, for that you might need to pad if you have such truncated values from number I assume:
select column1
,try_to_time(column1, 'HH24MMSS') as wrong
,try_to_time(column1, 'HH24MISS') as r1
,lpad(column1, 6, '0') as p
,try_to_time(p, 'HH24MISS') as r2
from values
('83455'),
('153651'),
('91251'),
('1251')
;
COLUMN1
WRONG
R1
P
R2
83455
null
08:34:55
083455
08:34:55
153651
null
15:36:51
153651
15:36:51
91251
09:00:51
09:12:51
091251
09:12:51
1251
12:00:01
null
001251
00:12:51
Thus inline it would be:
try_to_time(lpad(column1, 6, '0'), 'HH24MISS')

TSQL Convert VARCHAR of HH:MM:SS.mmm to TIME

Unable to convert 15600.02 value to time .
Ex :
select cast(stuff(stuff('15600.02',5,0,':'),3,0,':') as time)
ERROR :
Conversion failed when converting date and/or time from character string.
can any one help on it .
Thanks,
Kumar
Assuming this is really what this number represents...
The location you are using in the STUFF command is 1 off when earlier than 10 o'clock.
This isn't pretty, but try select cast(stuff(stuff(REPLACE(SPACE(7 - CHARINDEX('.', '15600.02' + '.')),' ','0') + '15600.02',5,0,':'),3,0,':') as time)

SQL- How to exclude wrong time(saved in form of nvarchar) in my dataset?

I am trying to convert records in form of nvarchar into time. For example:
1520 into 15:20:0000
But it throws error: Conversion failed when converting date and/or time from character string.
Because there are wrong timestamps in the dataset like values:
4059','7054','4054','4030','3040','6046','7555','8030', '1169'
I am using the query:
select [QHHHMM],
cast(SUBSTRING([QHHHMM], 1, 2) + ':' + SUBSTRING([QHHHMM], 3, 2) as time) as TimeHistory
FROM [MOCK124].[dbo].[F2QH]
where
[QHHHMM] not in ('4059','7054','4054','4030','3040','6046','7555','8030')
and [QHHHMM] not in ('1169')
But I still see there are few more records that are invalid as time. Could anyone please assist what should be the where clause to exclude all the invalid timestamps?
Thanks
Look at the hours/mins as integers?
where cast(left(QHHHMM, 2) as int) between 0 and 23
and cast(right(QHHHMM, 2) as int) between 0 and 59
Figured out, we can use
where
[QHHHMM] > '2359'
or
substring([QHHHMM], 3,2) > '59'
Thanks!

Data in SQL not in proper date format, how to change it into the right format?

I have a column called ProcessTimeOnHold that shows how long it takes us to process an order.
Any order that takes less than 24 hours to process is shown in this format HH:MM:SS. As far as I can tell this data is in the proper date format however any order that takes longer than 24 hours shows up in this format D.HH:MM:SS So, if an order took 29 hours and 34 minutes it will show up as 1.05:34:00.
The problem I have is if I try to convert or manipulate the data for example into minutes the orders in the proper data format will work but anything after 24 hours will return an error.
For example if I try to convert to minutes with this:
LTRIM(DATEDIFF(MINUTE, 0, ProcessTimeOnHold))
It will convert all orders into minutes until it runs into an order with the D.HH:MM:SS format and then it will return this error:
Error Message: Conversion failed when converting datetime from
character string
Any way that I can change my entire column into datetime?
You might want to search for a less verbose solution, but this is one option, using string manipulation:
SELECT DATEDIFF(MINUTE, 0, (convert(time,RIGHT(ProcessTimeOnHold,8)))) +
(case when ProcessTimeOnHold like '%.%'
then
(cast((LEFT(ProcessTimeOnHold,CHARINDEX(ProcessTimeOnHold,'.')-1)) AS INT) * 24 * 60)
else 0 end) as TotalMinutes
(Edited based on comment)
Here's a way to get just the minutes:
declare #table table (ProcessTimeOnHold varchar(16));
insert #table (ProcessTimeOnHold)
values ('00:20:00')
, ('01:20:00')
, ('1.05:34:00');
select ProcessTimeOnHold
, datediff(minute,0,right(ProcessTimeOnHold,8))
+ isnull(nullif(replace(left(ProcessTimeOnHold,charindex('.',ProcessTimeOnHold)),'.',''),''),0) * 1440 'Minutes'
from #table
RETURNS:
ProcessTimeOnHold Minutes
----------------- -----------
00:20:00 20
01:20:00 80
1.05:34:00 1774
Try this one-
ANSI format: 2006.10.23
SELECT [ANSI]=CONVERT(char,CURRENT_TIMESTAMP,102)

Convert hh:mm to ss on netezza (SQL)

I have a char datatype that contains data in hh:mm format and i would like to convert into seconds in netezza DB.
For eg: 1:23 to seconds?
Thanks
First you need to convert your time to the right datatype and then extract EPOCH to get the seconds.
select
extract ( EPOCH from to_timestamp(to_char(current_date,'YYYY-MM-DD')
||' '||sample.dt_str,'YYYY-MM-DD MI:SS')-current_date)
from
(
select '01:23' as dt_str
) sample
Also if you have the SQL Functions Toolkit installed you could simply run this.
select sql_functions.admin.second ('01:01:23')
select '1:23' aTime, position(':' in aTime) pos,
cast(substring(aTime from 1 for (pos-1)) as int)*3600 HR,
cast(substring(aTime from (pos+1)) as int) * 60 mins,
hr+mins secs