Snowflake - Setting Date Time format in result of Query - snowflake-cloud-data-platform

I'm running a query in snowflake to then export. I need to set/convert a date value to the following format 'yyyy-MM-ddThh:mm:ss' from 2022-02-23 16:23:58.805
I'm not sure what is the best way to convert the date format. I've tried using TO_TIMESTAMP, but keep getting the following error '1 too many arguments for function [TO_TIMESTAMP(FSA.LAST_UPDATED, 'yyyy-MM-ddThh:mm:ss')] expected 1, got 2'

This looks like a conversion issue. Please check datatype for your column last_updated. Also seems there is some typo in your question - for the time portion in format, use mi (hh:mi:ss).
Refer below -
select to_timestamp('2022-02-23 16:23:58.805'::TIMESTAMP,'yyyy-mm-dd hh:mi:ss.ff')
;
000939 (22023): SQL compilation error: error line 1 at position 7
**too many arguments for function
[TO_TIMESTAMP(TO_TIMESTAMP_NTZ('2022-02-23 16:23:58.805'), 'yyyy-mm-dd hh:mi:ss.ff')] expected 1, got 2**
select to_timestamp('2022-02-23 16:23:58.805'::string,'yyyy-mm-dd hh:mi:ss.ff');
TO_TIMESTAMP('2022-02-23 16:23:58.805'::STRING,'YYYY-MM-DD HH:MI:SS.FF')
2022-02-23 16:23:58.805

TO_TIMESTAMP is for string -> timestamp, TO_CHAR is for timestamp -> string of which the TO_CHAR( <date_or_time_expr> [, '<format>' ] ) form is the one you seem to be wanting.
this SQL show string -> timestamp -> formatted string
SELECT
'2022-02-23 16:23:58.805' as time_string,
to_timestamp(time_string) as a_timestamp,
to_char(a_timestamp, 'yyyy-MM-ddThh:mm:ss') as formating_string;
TIME_STRING
A_TIMESTAMP
FORMATING_STRING
2022-02-23 16:23:58.805
2022-02-23 16:23:58.805
2022-02-23T16:02:58

Related

handle incorrect values in snowflake

HI I have one doubt in snow flake server.
how to handle non ascii value in snowflake
table : emp
Empno|Empname
1 |ravÉi
2 |banu raju
3 |raḠu kumar
based on above data i want output like below
Empno|Empname
1 |ravEi
2 |banu raju
3 |raGu kumar
I have tried like below
select empno,uncode(empname,ecoding='utf-8')lname from emp
but above query throwing error:
sql compilation erro: error line 1 at postition 27 invalid identifier encoding
can you please tell me how to write query to achive this task in snow flake server .
did you try, it should work , the function Unicode returns the Unicode code point for the first Unicode character in a string.
select empno,empname from emp;
select unicode('ravÉi')lname from dual; will return the value
Given your sample input and output what you really want is to replace Unicode characters with their closest ASCII equivalent.
A JS UDF in Snowflake can help you do that:
You can solve this with a JS UDF in Snowflake:
CREATE OR REPLACE FUNCTION normalize_js(S string)
RETURNS string
LANGUAGE JAVASCRIPT
AS 'return S.normalize("NFD").replace(/\p{Diacritic}/gu, "")'
;
select normalize_js('áéÉña');
-- 'aeEna'
See https://stackoverflow.com/a/66606937/132438.

year coming wrong in TO_DATE function in db2

I tried to run below query in DB2 database:
My date string: 122887 mmddyy
select DATE(TO_DATE('122887', 'mmddyy')) from SYSIBM.dual;
now result is: 2087-12-28
But i am expecting below 1987-12-28.
How to achieve this?
You need to use the "adjusted year" for your query. Instead of YY it is RR:
values(DATE(TO_DATE('122887', 'mmddrr')))"
1
----------
12/28/1987
Details are in the documentation for TO_DATE/TIMESTAMP_FORMAT.

Importing Dates into R from SQL Server

I am trying to query a SQL Server database to check for the MAX date in one field and to select the next day for input into another process. I am able to query the database and pull back a date, but I can't convert it into a date object.
Here is what I tried:
tmpMaxDate <- sqlQuery(dbhandle, 'select MAX([date]) + 1 from dbo.My_Data ')
tmpMaxDate
1 2016-01-02
IsDate(tmpMaxDate)
[1] FALSE
maxDate <- as.Date(tmpMaxDate)
Error in as.Date.default(tmpMaxDate) :
do not know how to convert 'tmpMaxDate' to class “Date”
maxDate
NULL
IsDate(maxDate)
[1] FALSE
maxDate <- as.Date(tmpMaxDate, format = "%Y-%M-%D")
Error in as.Date.default(tmpMaxDate, format = "%Y-%M-%D") :
do not know how to convert 'tmpMaxDate' to class “Date”
IsDate(maxDate)
[1] FALSE
maxDate
NULL
The packages I am using are RODBC, chron, lubridate, and RSQLserver
I thought I needed to convert to a date object to use the date from SQL Server in R.
Any thoughts on why my date won't convert? Do I need to convert to be able to use that date to filter data in R?
DateVar <- sqlQuery(dbhandle,"SELECT CONVERT(varchar(10),GETDATE() + 1,101) as Date")
DateVar
NewDate <- as.Date(DateVar$Date, format = "%m/%d/%y")
NewDate
I think the trick for me to was give the date response a column name and then reference the column (being the specific string value needed to convert) name in the conversion.
class(NewDate)
will show as "Date"

Dynamics AX 2012: Conversion failed when converting date and/or time from character string

In AX I have several entities. When I try to post unposted timesheets it works fine for all entities except of one where I'm getting SQL error: "Conversion failed when converting date and/or time from character string"
The call stack is below:
In highlighted method I see that it cannot find any SourceDocumentHeader in AccountDistribution table, so the AccountingDate is empty.
Has anybody experienced same problem and knows how to solve it?
It is strange for me because all other entities works OK.
Thanks.
The technical explanation of what you are seeing is that this part of the code generates invalid SQL, but it looks to me as if you have a problem with your setup.
If you run date2str on an empty date it returns an empty string. Please try this in a job and you will see an empty string in the infolog.
static void TestEmptyDate(Args _args)
{
AccountingDate _date;
;
info(date2str(_date, 321, 2, 3, 2, 3, 4, DateFlags::None));
}
That then gets concatenated in the method updateDistributionsForEvent to generate an SQL statement:
sqlStatementText = strFmt('UPDATE T1 SET ACCOUNTINGEVENT=%1,RECVERSION=%2 FROM ACCOUNTINGDISTRIBUTION T1 WITH (INDEX(I_7452SOURCEDOCUMENTHEADERIDX)) CROSS JOIN SOURCEDOCUMENTLINE T2 ', _accountingEventRecId, xGlobal::randomPositiveInt32());
sqlStatementText += strFmt('WHERE (((T1.PARTITION=%1) AND (T1.ACCOUNTINGEVENT=0) AND (T1.ACCOUNTINGDATE={ d\'%2\'})) AND (T1.SOURCEDOCUMENTHEADER=%3)) AND ', getcurrentpartitionrecid(), sqlDate, _sourceDocumentRecId);
sqlStatementText += strFmt('((T2.RECID=T1.SOURCEDOCUMENTLINE) AND (T2.ACCOUNTINGSTATUS=%1 OR T2.ACCOUNTINGSTATUS=%2)) AND (T2.PARTITION=%3)', enum2int(SourceDocumentLineAccountingStatus::Completed), enum2int(SourceDocumentLineAccountingStatus::Canceled), getcurrentpartitionrecid());
Where T1.ACCOUNTINGDATE={ d\'%2\'} is the relevant part which generates T1.ACCOUNTINGDATE={ d''} in the SQL string.
If you try running
select {d''}
in SQL you will get
Msg 241, Level 16, State 3, Line 1 Conversion failed when converting
date and/or time from character string.
because an empty string cannot be parsed to a date.

How convert string "20080926112720" to datetime in SSIS?

i am getting the value from xml 20080926172720. I have loaded using ssis as a string column in sql server.but ineed to store 09/26/2008 27:17:20 like this as a datetime in sql server
convert(datetime,'20080926112720',101)
I need a output like this
09/26/2008 11:27:20
I'm using SQL Server 2005
It's not the cleanest way but SQL is having trouble parsing out the HH:mm:ss without the colon separator. This way will get you the formatting you want. I strongly suggest finding a way to make your input to the convert function formatted as 'CCYYMMDD HH:mm:ss'
DECLARE #MyDate char(15)
SET #MyDate = '20080926132720'
SELECT SUBSTRING(#Mydate,5,2)+'/'+SUBSTRING(#MyDate,7,2)+'/'+SUBSTRING(#MyDate,1,4)+SPACE(1)+SUBSTRING(#MyDate,9,2)+':'+SUBSTRING(#MyDate,11,2)+':'+SUBSTRING(#MyDate,13,2)
I can't believe I put the amount of time into this that I did but you need to create a new variable and evaluate the variable as an expression to give it the proper formatting. Right now SSIS has no clue how to parse the blurb of text you are giving it. In the expression builder you should build something like this.
SUBSTRING( "20080926112720",1, 4 )+"-"+SUBSTRING( "20080926112720",5, 2 )+"-"+SUBSTRING( "20080926112720",7, 2 )+ " "+SUBSTRING( "20080926112720",9, 2 )+ ":"+SUBSTRING( "20080926112720",11, 2 )+":" +SUBSTRING( "20080926112720",13, 2 )
This will evaluate to this value 2008-09-26 11:27:20 then you can set that variable as Datetime.
You will of course need to modify this as
SUBSTRING( ##YourDateVariable,1, 4 )+"-"+SUBSTRING( ##YourDateVariable,5, 2 )+"-"+SUBSTRING( ##YourDateVariable,7, 2 )+ " "+SUBSTRING( ##YourDateVariable,9, 2 )+ ":"+SUBSTRING( ##YourDateVariable,11, 2 )+":" +SUBSTRING( ##YourDateVariable,13, 2 )

Resources