I am new to snowflake and I am trying to convert to timestamp with the T delimiter. I have looked at examples but I seem to be getting errors. I am not sure what I am getting wrong
My code :
select Id,
to_timestamp_ntz(dt."Value",'YYYY-MM-DD"T"HH24:MI:SS'),
from Accounts
Errors
Can't parse '2018-09-08 07:40:45' as timestamp with format 'YYYY-MM-DD"T"HH24:MI:SS'
For the timestamp format you want the input timestamp should look like '2018-09-08T07:40:45', yours is missing the T.
So this works:
select to_timestamp_ntz('2018-09-08T07:40:45','YYYY-MM-DD"T"HH24:MI:SS');
I get:
2018-09-08 07:40:45.000
or otherwise use for that input timestamp the format:
select to_timestamp_ntz('2018-09-08 07:40:45','YYYY-MM-DD HH24:MI:SS');
which gives same output.
You command is doing an input formatting which means that you're saying that 2018-09-08 07:40:45 is in format YYYY-MM-DD"T"HH24:MI:SS which is not true because you are missing the T between the date and time in your input.
If you'd like to do an output formatting you would use TO_CHAR instead.
E.g.
select to_char(to_timestamp('2018-09-08 07:40:45'), 'YYYY-MM-DD"T"HH24:MI:SS');
Returns:
2018-09-08T07:40:45
Related
I am trying to convert the date I have got using the GETDATE() function into YY-MON-DD format such as 04 Nov 2022. I have used TO_VARCHAR() to convert into the date returned by the GETDATE() into a string. The output is correct till the TO_VARCHAR() is used i.e. SELECT TO_VARCHAR(GET_DATE, 'DD-MON-YY') returns the desired format.
When I try to wrap it around TO_DATE() function; the date format changes into 2022-11-04.
SELECT TO_DATE(TO_CHAR(GETDATE(), 'DD-MON-YY'), 'DD-MON-YY')
How can I resolve the problem and correct the format!?
A date will display in the default format for your session. If you want to display it in a different format you would need to use the TO_CHAR(date_col, fmt) construct. So your select statement just needs to be:
SELECT TO_CHAR(GETDATE(), 'DD-MON-YY')
wrapping a TO_DATE round it doesn't achieve anything if all you are trying to do is display that column as an output of your SELECT statement
You need set the desired format at session level
alter session
set date_output_format='DD-MON-YYYY';
select getdate()::date --getdate() returns a timestamp not a date
select current_date() --alternatively
As Nick suggested, use the select to format dates instead of tinkering with session parameters
I am using Azure Data Factory to copy data from CSV to Snowflake, the copy executes fine but it has an error when it comes to copy Date from the CSV which has this value (14/01/2000), if the Date is (12/10/2000) or less, it works very well.
Here is the error message:
ErrorCode=UserErrorOdbcOperationFailed,'Type=Microsoft.DataTransfer.Common.Shared.HybridDeliveryException,Message=ERROR [22007] Timestamp ‘14/01/2000’ is not recognized
I tried to adjust the format of the date in the copy activity to be dd/MM/yyyy or change the Culture to en-UK as the below image but it has the same issue.
I tried to use all the possible types of date in Snowflake as below but I still have the same issue:
DATE
DATETIME
TIMESTAMP
TIMESTAMP_LTZ
Snowflake doesn't supports format as DD/MM/YYYY and even it supports MM/DD/YYYY it can lead to incorrect dates (05/02/2013 could be interpreted as May 2, 2013 instead of February 5, 2013).
So this:
select '14/01/2000'::timestamp;
produces:
Timestamp '14/01/2000' is not recognized
while this:
select '01/14/2000'::timestamp;
produces:
2000-01-14 00:00:00.000
Same for:
select '14/01/2000'::date;
select '01/14/2000'::date;
The guidelines for how to use date/timestamp formats are described here.
In your case one way to get that value as a date is to use the to_date function, like this:
select to_date('14/01/2000', 'DD/MM/YYYY');
gives me:
2000-01-14
I am aware that this is a very common and have tried using the solutions suggested on similar questions. However, I cant seem to make it work.
I have a column which contains dates, but it is of varchar datatype.
The data looks like this:
startdate
-----------
15/01/2007
29/06/1998
07/12/2001
and so on..
I tried the following command
try_convert(datetime, startdate) as 'startdate'
But it returns a lot of NULLs even when there is a date populated in the original column.
Can someone please help?
The try_convert method accepts three arguments: Data type, Expression, and Style.
You need to use the Style argument:
try_convert(datetime, startdate, 103) as 'startdate' -- 103 is dd/MM/yyyy
(a list of supported styles can be found here)
Also, You should store date values as Date, not as varchar.
For more information, read Aaron Bertrand's Bad habits to kick : choosing the wrong data type
I have an input to search for what date to show, the input can be a date format (yyyy or yyyy-mm or yyyy-mm-dd) like '2006' or '2017-01' or '2008-10-10', i use query like this
SELECT * FROM MEMBER WHERE UPPER(join_date) LIKE UPPER(%".$input."%);
but the result is empty, is there a way to correctly use like statement for datetime in postgresql
LIKE is for strings, not for DATE values. That means you must first convert the date to a properly formatted string value:
SELECT *
FROM member
WHERE to_char(join_date, 'YYYY-MM-DD') LIKE '%.... %';
Using upper(join_date) is subject to the evil implicit data type conversion and will not work reliably.
Also: upper() on a string with only numbers doesn't really make sense.
SELECT communication.*
,employeedetails.resourcename
FROM communication
,employeedetails
WHERE employeedetails.employeenumber = communication.employeenumber
AND communication.project = employeedetails.projectname
ORDER BY
CONVERT(DATETIME ,communication.month ,5)
I am getting the following error message:
"Conversion failed when converting date and/or time from character
string"
Kindly help me out
It is not easy having no info regarding the data tables involved!
However if names are meaningful, how can you convert a month to a datetime?
If it is existing try this:
SELECT communication.*, employeedetails.resourcename
FROM communication, employeedetails
WHERE employeedetails.employeenumber = communication.employeenumber and communication.project = employeedetails.projectname
ORDER BY communication.day, communication.month, communication.year --due to style 5
even if I would suggest to go for style 12
The datatype of communication.month is presumably a character type. If this holds "January" for January etc. then SQL has no way of converting that character to a datetime. You would need to append a string such that SQL could parse it as a datetime.
e.g. ORDER BY CONVERT(datetime,communication.month + '01 2000',5).
However, storing the communication date as a datetime in the first place would be better...