How do I convert this to Snowflake SQL? - sql

My T SQL -
left(convert(char,Month,111),7) "Year-Mon"
Below is the error I'm getting
Error: invalid identifier 'CHAR'

You can use this
to_char(to_date(Month,'yyyy/mm/dd'),'yyyy-mm') "Year-Mon"
From your question, I assumed,
Month is a string in yyyy/mm/dd format.
And you are expecting a string in yyyy-mm format.

Related

timstamp from string to timestamp in BigQuery

i have the followoing format of a timestamp which is saved as string.
2022-09-15T11:07:28.872
I've tried to convert it to timestamp like this
PARSE_TIMESTAMP("%Y-%m-%d-%H:%M:%S",JSON_VALUE(DATA,'$.payload.onHoldUntil'))
but i get the following error
Mismatch between format character '-' and string character 'T'
How can i solve this issue ?
The format string you provide in the PARSE_TIMESTAMP function should align to the format of your string. In your case it does not as you are missing a T and the milliseconds.
Try the following:
select '2022-09-15T11:07:28.872' as s_ts
, PARSE_TIMESTAMP('%FT%R:%E3S', '2022-09-15T11:07:28.872')

Trouble converting malformed ISO8601-formatted varchar to timestamp

I was trying to convert a timestamp string with the following format into a timestamp format but encountered an error as such:
Invalid format: "20201216T090000+0000" is malformed at "0000+0000"
Original Query
SELECT from_iso8601_timestamp(date_ts)
FROM
...
I thought of applying substr to retrieve 20121216T09 only as a string, which I know would work with from_iso8601_timestamp. But any other advice would be appreciated!
The from_iso8601_timestamp() supports format with dashes and colons:
presto> SELECT from_iso8601_timestamp('2020-12-16T09:00:00+00:00');
_col0
-----------------------------
2020-12-16 09:00:00.000 UTC
In the case input is formatted like 20201216T090000+0000, the parse_datetime() would be more appropriate.

format date in oracle SQL

I was trying to learn how to format date in oracle pl oracle, when I ran below query its returns error
SELECT TO_DATE('01-JAN-00', 'YYYY-DD-MM') FROM dual;
the error message is
ORA-01858: a non-numeric character was found where a numeric was expected
01858. 00000 - "a non-numeric character was found where a numeric was expected"
*Cause: The input data to be converted using a date format model was
incorrect. The input data did not contain a number where a number was
required by the format model.
*Action: Fix the input data or the date format model to make sure the
elements match in number and type. Then retry the operation.
You are either not using the correct format specifier, or not passing the correct string. You want:
SELECT TO_DATE('2000-01-01', 'YYYY-DD-MM') FROM DUAL;
Or:
SELECT TO_DATE('01-JAN-00', 'DD-MON-YY') FROM DUAL;
Or you can simply declare a DATE litteral:
SELECT DATE'2000-01-01' FROM DUAL;
for my scenario I had to use to_char which perfectly solve the formatting issue.
SELECT TO_CHAR('01-JAN-00', 'yyyy-DD-MM') FROM dual;

SQL Command to convert YYYYMMDD to YYYY-MM-DD

I am facing an operand type clash error due to this issue (in Pre-SQL Informatica) that says the date is incompatible with int, even though the date in target table is defined as datetime. How can I convert the existing YYYYMMDD date to YYYY-MM-DD format for my query?
You can try using this:
to_char(to_date(your_date, 'YYYYMMDD'), 'YYYY-MM-DD')
or
TO_DATE(your_date as String, 'YYYY-MM-DD')
Use this function also it will work.
1) v_ port---date/time TO_DATE(TO_CHAR(COLUMN_NAME),'YYYYMMDD')
2) o_port----string TO_CHAR(v_ port,'YYYY-MM-DD')

I am not able to find age by using year in SQL Server 2005?

I am writing a query as following
SELECT DATEDIFF(year,Clmn_Dob,getdate()) AS DiffDate
FROM tblABCD
where Clmn_Dob is a NVARCHAR column. I want to find the age based on date of birth. But instead of this I am getting an error
Arithmetic overflow error converting expression to data type datetime
My string format is dd/MM/yyyy and getdate() format is MM/dd/yyyy.
I do not get any solution
You can use the CONVERT function:
SELECT
DATEDIFF(year,CONVERT(datetime, Clmn_Dob, 103),getdate()),
FROM tblABCD
and here doc:
http://msdn.microsoft.com/en-us/library/ms187928.aspx