How to store -00:00 time zone value in snowfkale - sql

I had a scenario in storing the data reading from source file to snowflake table:
Input data line:
3 AC 0060876543NOV221080123 23 5 7 56709000900+0900
I have to read the last four digit in "09:00" format based on the plus or minus symbol. If the data coming as -0900 then in snowflake it should store as "-09:00".
SELECT
(CASE SUBSTR(raw_data, 48, 1)
WHEN '-' THEN CONCAT('-' , SUBSTR(raw_data,49,2) , ':' , SUBSTR(raw_data,51,2))
ELSE CONCAT(SUBSTR(raw_data,49,2) , ':' , SUBSTR(raw_data,51,2)) END) +
(CASE SUBSTR(raw_data,40,4) WHEN '2400' THEN 24 ELSE 0 END)
AS COLUMN_1
FROM
(SELECT temp_row.$1 as raw_data from
#JOB_MANAGEMENT.SNOWFALKE (file_format => 'DB.TBL_FILE_FORMAT',
pattern=>'.*/input_file.txt') temp_table) temp;
But, I'm getting error as below :
For minus value : Numeric value '-04:00' is not recognized
For plus value: Numeric value '09:00' is not recognized
UPDATED:
This is my teradata sql:
select (case '-' when '-' then '-' ||'04' || ':' ||'00'
else '04' || ':' ||'00'
end (Interval hour to minute)) +
(case
'2400' when '2400' then 24
else 0
end (interval hour));
output : -04:00
select (case '-' when '-' then '-' ||'04' || ':' ||'00'
else '04' || ':' ||'00'
end (Interval hour to minute)) +
(case
'1835' when '2400' then 24
else 0
end (interval hour));
output: 20:00
I want to implement the same in snowflake and thus in the second case statement it is throwing error.
The final column which I need to store the content in varchar column.

SUBSTR(raw_data, LEN(raw_data)-5,3) || ':' || RIGHT(raw_data,2)

Related

Snowflake: Conversion error of an teradata query to snow sql

I have a Teradata query (updated the fields with sample values):
select (case '-' when '-' then '-' ||'04' || ':' ||'00'
else '04' || ':' ||'00'
end (Interval hour to minute)) +
(case
'2400' when '2400' then 24
else 0
end (interval hour));
output : -04:00 (varchar type)
select (case '-' when '-' then '-' ||'04' || ':' ||'00'
else '04' || ':' ||'00'
end (Interval hour to minute)) +
(case
'1835' when '2400' then 24
else 0
end (interval hour));
output: 20:00 (varchar type)
Want to convert the same in snowflake, but the same output value was not able to insert in snowflake varchar column:
SELECT
(CASE SUBSTR(raw_data, 48, 1)
WHEN '-' THEN CONCAT('-' , SUBSTR(raw_data,49,2) , ':' , SUBSTR(raw_data,51,2))
ELSE CONCAT(SUBSTR(raw_data,49,2) , ':' , SUBSTR(raw_data,51,2)) END) +
(CASE SUBSTR(raw_data,40,4) WHEN '2400' THEN 24 ELSE 0 END)
AS COLUMN_1
FROM
(SELECT temp_row.$1 as raw_data from
#JOB_MANAGEMENT.SNOWFALKE (file_format => 'DB.TBL_FILE_FORMAT',
pattern=>'.*/input_file.txt') temp_table) temp;
Sample:
SELECT
(CASE '-' WHEN '-'
THEN CONCAT('-' , '04 , ':' , '00')
ELSE CONCAT('04' , ':' , '00') END) +
(CASE '1825' WHEN '2400' THEN 24 ELSE 0 END)
Output : -04:00 (column type - varchar) -> but throwing error in snowflake.
Numeric value '-04:00' is not recognized
Instead of trying to rewrite query 1:1, this approach focuses on rewriting the logic using TIME_FROM_PARTS:
SELECT
CASE WHEN SUBSTR(raw_data, 48, 1) = '-'
THEN TIME_FROM_PARTS(24-SUBSTR(raw_data,49,2)::INT, SUBSTR(raw_data,51,2)::INT, 0)
ELSE TIME_FROM_PARTS(SUBSTR(raw_data,49,2)::INT, SUBSTR(raw_data,51,2)::INT, 0)
END
FROM ...

How to deal with non standard date and month format (single digit) in SQLite using strftime? [duplicate]

I am looking to convert one date format to another using SQL. I am using DB Browser for SQLite and the dates are stored in a column of type 'TEXT'.
Here are 2 examples of the current format:
4/17/2017 9:09:09 AM
10/4/2017 10:21:13 PM
Note that in the current format for the day, month and hour they currently do not pad with a leading '0' if only a single digit. Also they are putting the month first, then the day, then the year.
These 2 examples should be converted to the YYYY-MM-DD HH:MM:SS (24 hour) format:
i.e.
4/17/2017 9:09:09 AM -> 2017-04-17 09:09:09
10/4/2017 10:21:13 PM -> 2017-10-04 22:21:13
etc.
If anyone could provide me with a SQL query that can convert all values in a column like the first format into the second format that would be greatly appreciated. Thanks!
So I don't know much about python so had to go with the 'obscene chain of instr() and substr() commands'. It's a mess, but it seems to work now.
instr(DATE,'/') used to find the position of the first '/'
length (rtrim(DATE, 'PMA0123456789: ')) used to find the position of the second '/'
instr(DATE, ':') used to find the position of the first ':'
Well below is the mess I created :D
UPDATE testtable
SET DATE =
substr (DATE, length (rtrim(DATE, 'PMA0123456789: '))+1,4) || '-' ||
CASE instr(DATE, '/')
WHEN 2 THEN
'0'
ELSE ''
END
|| substr (DATE,1,instr(DATE,'/')-1) || '-' ||
CASE (length (rtrim(DATE, 'PMA0123456789: '))) - instr(DATE,'/')
WHEN 2 THEN
'0'
ELSE ''
END
|| substr (DATE,instr(DATE,'/')+1, length (rtrim(DATE, 'PMA0123456789: ')) - instr(DATE,'/')-1)
|| ' ' ||
CASE substr(DATE,length(DATE)-1,2)
WHEN 'AM' THEN
CASE substr(DATE, instr(DATE, ':')-2,2)
WHEN '12' then
'00'
ELSE
CASE substr(DATE, instr(DATE, ':')-2, 1)
WHEN ' ' THEN
'0'
ELSE
substr(DATE, instr(DATE, ':')-2,1)
END
|| substr(DATE, instr(DATE, ':')-1,1)
END
WHEN 'PM' THEN
CASE substr(DATE, instr(DATE, ':')-2,2)
WHEN '12' THEN
substr(DATE, instr(DATE, ':')-2,2)
ELSE
CAST (substr(DATE,instr(DATE, ':')-2,2) AS INT) + 12
END
ELSE
'error'
END
|| ':' || substr(DATE,instr(DATE, ':')+1,5);

Converting from one date format to another using DB Browser for SQLite

I am looking to convert one date format to another using SQL. I am using DB Browser for SQLite and the dates are stored in a column of type 'TEXT'.
Here are 2 examples of the current format:
4/17/2017 9:09:09 AM
10/4/2017 10:21:13 PM
Note that in the current format for the day, month and hour they currently do not pad with a leading '0' if only a single digit. Also they are putting the month first, then the day, then the year.
These 2 examples should be converted to the YYYY-MM-DD HH:MM:SS (24 hour) format:
i.e.
4/17/2017 9:09:09 AM -> 2017-04-17 09:09:09
10/4/2017 10:21:13 PM -> 2017-10-04 22:21:13
etc.
If anyone could provide me with a SQL query that can convert all values in a column like the first format into the second format that would be greatly appreciated. Thanks!
So I don't know much about python so had to go with the 'obscene chain of instr() and substr() commands'. It's a mess, but it seems to work now.
instr(DATE,'/') used to find the position of the first '/'
length (rtrim(DATE, 'PMA0123456789: ')) used to find the position of the second '/'
instr(DATE, ':') used to find the position of the first ':'
Well below is the mess I created :D
UPDATE testtable
SET DATE =
substr (DATE, length (rtrim(DATE, 'PMA0123456789: '))+1,4) || '-' ||
CASE instr(DATE, '/')
WHEN 2 THEN
'0'
ELSE ''
END
|| substr (DATE,1,instr(DATE,'/')-1) || '-' ||
CASE (length (rtrim(DATE, 'PMA0123456789: '))) - instr(DATE,'/')
WHEN 2 THEN
'0'
ELSE ''
END
|| substr (DATE,instr(DATE,'/')+1, length (rtrim(DATE, 'PMA0123456789: ')) - instr(DATE,'/')-1)
|| ' ' ||
CASE substr(DATE,length(DATE)-1,2)
WHEN 'AM' THEN
CASE substr(DATE, instr(DATE, ':')-2,2)
WHEN '12' then
'00'
ELSE
CASE substr(DATE, instr(DATE, ':')-2, 1)
WHEN ' ' THEN
'0'
ELSE
substr(DATE, instr(DATE, ':')-2,1)
END
|| substr(DATE, instr(DATE, ':')-1,1)
END
WHEN 'PM' THEN
CASE substr(DATE, instr(DATE, ':')-2,2)
WHEN '12' THEN
substr(DATE, instr(DATE, ':')-2,2)
ELSE
CAST (substr(DATE,instr(DATE, ':')-2,2) AS INT) + 12
END
ELSE
'error'
END
|| ':' || substr(DATE,instr(DATE, ':')+1,5);

Grouping SQL-Result by time does not work

I am trying to set up an SQL query in Firebird 2.5 in order to make a statistic about sales grouped by time.
My approach I followed up so far would be:
SELECT EXTRACT(WEEKDAY FROM ODR.ORDERDATE) AS S1, EXTRACT(HOUR FROM ODR.ORDERDATE) AS S2,
CASE
WHEN (EXTRACT(HOUR FROM ODR.ORDERDATE)) < 10 THEN
'0' || CAST(EXTRACT(HOUR FROM ODR.ORDERDATE) AS VARCHAR(2)) || ':00 - ' || '0' ||
CAST(EXTRACT(HOUR FROM ODR.ORDERDATE) AS VARCHAR(2)) || ':59 '
WHEN EXTRACT(HOUR FROM ODR.ORDERDATE) >= 10 THEN
CAST(EXTRACT(HOUR FROM ODR.ORDERDATE) AS VARCHAR(2)) || ':00 - ' ||
CAST(EXTRACT(HOUR FROM ODR.ORDERDATE) AS VARCHAR(2)) || ':59 '
END AS TINTERVAL,
COUNT(ODR.ID) AS ABSCOUNT
FROM ODR
GROUP BY S1,S2,TINTERVAL
ORDER BY S1,S2 ASC
The syntax of this query seems to be fine for SQL-Connection established by Firebird ODBC 2.5.
My question:
For hour=0 (<10) the query returns a result set which is not presented by the data given by the orders.
e.g. the result-set for this query might look like this:
S1 S2 TINTERVAL ABSCOUNT
0 0 00:00 - 00:59 30
0 1 01:00 - 01:59 2
I don't know how this query comes to its strange count in hour = 0.
This does not make any sense to me.

DB2 date conversion

I have 2 INTEGER columns like the following:
Month Year
----- -----
5 2011
Is there any way to convert that to a single column VARCHAR like this: May-2011
I don't know of an easy way to do this since you don't have a date object (ie its not like youre finding the month of a timestamp), you can use a case statement but it gets long.
SELECT CASE Month
WHEN '1' THEN 'January'
WHEN '2' THEN 'February'
WHEN '3' THEN 'March'
WHEN '4' THEN 'April'
...
END+'-'+Year
FROM TABLE
I think this will do it:
SELECT
MONTHNAME(
DATE(CAST(Year AS CHAR(4)) || '-' || TRIM(CAST(Month AS CHAR(2))) || '-1')
) || '-' || CAST(Year AS CHAR(4))
FROM TABLE
This should do the trick, assuming that the columns Month and Year are integers and Month has the domain 1-12:
select substring('---JanFebMarAprMayJunJulAugSepOctNovDec', 3*Month , 3 )
+ '-'
+ right(digits(Year),4)
from some_table
If Month is 0 you'll get '---' as the month; if it's less than 0 or greater than 12, you'll get some sort of blooey.
You could create a function to convert the month value, like this...
CREATE FUNCTION INT2MONTH (MONTH INTEGER)
RETURNS VARCHAR(100)
LANGUAGE SQL
CONTAINS SQL
NO EXTERNAL ACTION
DETERMINISTIC
RETURN MONTHNAME('2000-' || RIGHT('0' || STRIP(CHAR(MONTH)), 2) || '-01')
Then you can...
select int2month(month) || '-' || strip(char(year)) from test
1
--------------------------------------------------
May-2011
June-2011
December-2012
If you want a 3 char month then change last last on function to...
RETURN LEFT(MONTHNAME('2000-' || RIGHT('0' || STRIP(CHAR(MONTH)), 2) || '-01'), 3)
I realize this question is pretty old, but there's a way that is a lot simpler than any of the options listed here (in my opinion) -- a combination of some date math and the VARCHAR_FORMAR() function:
SELECT
VARCHAR_FORMAT(
DATE('0001-01-01') + (month_col - 1) MONTH + (year_col - 1) YEAR
,'Month-YYYY'
)
FROM your_table