SQLYog convert DD/MM/YYYY field not working - datetime

I have a table with a column called 'Start_Date', in the format DD/MM/YYYY.
I want to convert this into a datetime within a query, and have been trying various methods without success.
I currently have...
SELECT CONVERT(DATETIME, start_date, 103) FROM product_backfile;
...but get the error message...
Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'start_date, 103)
FROM product_backfile LIMIT 0, 1000' at line 1
I should add that I've tried this too, with no luck...
SELECT CONVERT(DATE, start_date, 103) FROM product_backfile;
The full syntax I'm trying to implement is...
CONCAT(DATE_FORMAT(start_Date, '%Y-%m-%d'),'T', '00:00:00Z'), CHAR(93), ' TO ', CHAR(91), CONCAT(DATE_FORMAT(End_Date, '%Y-%m-%d'),'T', '00:00:00Z')
I've narrowed the problem down to the way in which the Start_Date and End_Date fields are being interpreted, and have previously got the full CONCAT string to work in MySQL Workbench...
I'm at the end of my tether...please help!! :)

Answered my own question, after some more investigation....! :)
DATE_FORMAT(STR_TO_DATE(start_date, '%d/%m/%Y'), '%Y-%m-%d')
Full syntax I was after...
CONCAT(DATE_FORMAT(STR_TO_DATE(start_date, '%d/%m/%Y'), '%Y-%m-%d'),'T', '00:00:00Z')

Related

How do i subtract time from datetime in snowflake?

pdt.startTime is datetime
s_first.FromTimeOfDay is a time
I want to subtract the time drom the datetime. When i run the code below, Snowflake gives me this error invalid type [CAST(S_FIRST.FROMTIMEOFDAY AS TIMESTAMP_NTZ(9))] for parameter 'TO_TIMESTAMP_NTZ'
select (pdt.StartTime - (SELECT s_first.FromTimeOfDay::datetime FROM Shift s_first))
from RAW_CPMS_AAR.POWERBI_DowntimeTable AS PDT
When i try this:
select (pdt.StartTime::TIMESTAMP_NTZ(9) - (SELECT s_first.FromTimeOfDay::TIMESTAMP_NTZ(9) FROM Shift s_first))
from RAW_CPMS_AAR.POWERBI_DowntimeTable AS PDT
I get more or less the same error: invalid type [CAST(S_FIRST.FROMTIMEOFDAY AS TIMESTAMP_NTZ(9))] for parameter 'TO_TIMESTAMP_NTZ'
How do I convert the time into a datetime format so that I can subtract the two. It doesnt seem to me that there is a clear way to convert time into datetime in snowflake.
Is this what you're after?
select current_timestamp() as sample_timestamp
, time(sample_timestamp) as sample_time
, date(sample_timestamp) as sample_date;
A user pointed me in the right direction. i didnt realize i could use "dateadd" to also subtract time.
dateadd(HOUR, - (HOUR(current_timestamp())), temp.DateTime)

Date conversion handling YYYY-MM-DD HH:MM:SS.SSS

My source is a file and loading into SQL Server table. I'm working on a scenario where i have to convert a string '2019-04-02T21:24:00.065' to informatica datetime format.
I tried below expression but some times its failing due to we are not receiving milliseconds from our source file in few occasions.
IIF(NOT ISNULL(DATEFIELD),TO_DATE(SUBSTR (DATEFIELD, 0, 10) || ' ' || SUBSTR(DATEFIELD, 12, 12), 'YYYY-MM-DD HH24.MI.SS.US'),NULL)
I'm looking for a permanent fix to handle all types of datetime formats regardless of what we receive in the file.
Well... I'm sorry to say, but there is no magic component that will recognize all possible date and time formats (including e.g. verbal in swahili).
You will need to detect the format for yourself. You can use a DECODE function, like e.g.:
DECODE(True,
IS_DATE(your_input_port, 'DD/MM/YYYY'), TO_DATE(your_input_port, 'DD/MM/YYYY'),
...)
If you are completely sure that only seconds/milliseconds are the missing part, you can check for length, if less than 12, use RPAD to the second part of your SUBSTR with missing format, or you can use decode as suggested by #maciejg and write code for all possible date formats.
Thanks for your inputs guys. Since we are not sure which date format we are receiving , we decided to go with a simple fix for this. I have changed the target field to varchar and simply replacing the T with ' ' value since this a staging mapping.
Again thanks for your time and inputs.

Issue with Casting string to date in Teradata

I have a string column - COL1 in TABLE1 which is if string data type. This table is loaded by Informatica session ( data coming from mainframe) and the format of the COL1 is YYYY-MM-DD. Now I have to use TABLE1 as the source in my next mapping . In the SQL override query of second mapping i will be casting COL1 to date using the below query .
SELECT
CAST(COL1 AS DATE FORMAT 'YYYY-MM-DD') AS CHK_DT FROM TABLE1
But when i try to execute this query in Teradata SQLA, just to check if it runs fine it gives me below error.
SELECT Failed. 2666: Invalid date supplied for COL1.
Can you please help me resolve this issue ? This is not the only date column which has issue, there are two more date columns . I guess the resolution is same for all three columns .
P.S - Just to verify, I updated all rows of COL1 of TABLE1 as 2016-12-12 and ran the select statement, select worked fine . I then updated COL1 of all rows as 2016-13-12, it gave same error . If either of DD or MM is more than 12, it is giving me error
Thanks
If DATE is represented/stored in ANSI standard literal YYYY-MM-DD, the CAST will work.
SELECT CAST('2016-12-13' AS DATE FORMAT 'YYYY-MM-DD') AS Date1
However i doubt that in your case.
The date is most probably in YYYY-DD-MM format. In that case the ANSI standard format will throw the error. You need YYYY-DD-MM
select CAST('2016-13-12' AS DATE FORMAT 'YYYY-DD-MM') AS Date2
P.S. You can confirm the conversion to date using TYPE() function. It should return DATE in your case
Hi Please try this piece of code
CAST(CAST(date_col AS FORMAT 'YYYY-MM-DD') AS VARCHAR(15))
instead of the transformation you are using.
Thanks for your response. However the issue was something else. Some of the incoming records had space in this column . So I had to tweak my informatica mapping to put a trim on date column . Now the select is running fine . Thanks for your time .

Datatype Mismatch in THEN/ELSE expression

I am trying to run below query snippet in a Teradata query
WHERE COALESCE(CAST (EXPC_DLVR_TS as date),'2020-12-31') >'2016-11-18'
I tried another but similar one
WHERE CAST(COALESCE(EXPC_DLVR_TS,'12/31/2020 17:00:00.000000-08:00') as date) >'2016-11-18'
For both the queries I am getting below error -
Datatype Mismatch in THEN/ELSE expression
You need to tell Teradata that '2020-12-31' is a date, otherwise it thinks it's a string. Just preface it by DATE. It's a good habit to always do that for dates.
where COALESCE(CAST (EXPC_DLVR_TS as date),date '2020-12-31') > date '2016-11-18'

Oracle unable to convert from LONG datatype to CLOB datatype( incosistent datatype error)

Oracle 11g is giving me the following error while trying to convert a long datatype to a clob.
I try: select to_lob(long_col_name) from table1.
I get :
[Error] Execution (1: 39): ORA-00932: inconsistent datatypes: expected - got LONG
What am i doing wrong here?
Found the answer here with the help of a colleague:
http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions185.htm
But no idea why this restriction is in place
You can apply this function only to a LONG or LONG RAW column, and only in the select list of a subquery in an INSERT statement.
I suggest a workaround like this, hope this helps to somebody.
SELECT substr(Y.longtoclob,
43 + length('ALIASLONG'),
DBMS_LOB.GETLENGTH(Y.longtoclob) -
2 * (32 + length('ALIASLONG'))) longtoclob
from dual,
(select (dbms_xmlgen.getxml('SELECT t.column_long ALIASLONG
FROM TABLE_LONG_CLOB t WHERE t.id = 2')) longtoclob
from dual) Y where DBMS_LOB.GETLENGTH(Y.longtoclob) > 0
You can't directly fetch LONG to LOB. You might want to convert it to VARCHAR2 first

Resources