What is new_time equivalent in snowflake - snowflake-cloud-data-platform

I have to convert "to_char(trunc(new_time(sysdate('GMT','PST' )) -1,'Mon-YY')" Oracle query into snowflake. Could anyone please help me.

Convert_timezone is the function in Snowflake that converts a timezone of a timestamp.
select convert_timezone('America/New_York', 'UTC', current_timestamp::timestamp) ;
For more information you can refer snowflake docs link

Related

Snowflake timestamp conversion issue

I am facing an issue with snowflake when converting a string to timestamp. Here is an example....
SELECT TO_TIMESTAMP('18090322033010','DDMMYYHH24MISS')
can anyone help me to convert this? the output should be 2003-09-18 22:03:3010
Appreciate your help
Why are you expecting 3010 to be seconds?
Try this:
SELECT TO_TIMESTAMP('180903220330','DDMMYYHH24MISS');
OR this:
SELECT TO_TIMESTAMP('18090322033010','DDMMYYHH24MISSFF');

Convert number to varchar using to_varchar with leading zeros in Snowflake

In Snowflake there is a number column storing values like: 8,123,456. I am struggling determining how to structure select statement to return a value like: 00008123456.
In SQL SERVER you can do something like this:
right(replicate('0', 11) + convert(varchar(11), job_no), 11) AS job_no
I understand Snowflake to_varchar function can be used, but not sure how to structure like in SQL Server.
Just add a format string on to_varchar():
select to_varchar(8123456, '00000000000');
Give it try with: lpad(to_char(nos),11,0)
More examples & details: https://docs.snowflake.com/en/sql-reference/functions/lpad.html#examples
a solution provided by Greg Pavlik saved me.
I just switched from DB2 to Snowflake.
So, TO_VARCHAR( <numeric_expr> [, '<format>' ] ) worked.

fetching data from SQL based on a given range of dates throwing conversion error

In my application i am trying to fetch data from the database where in the records are to be fetched within a given range of dates.
Below given is the query i am using to fetch the data and the error i am getting from SQL server.
Query
select * from table_name
where RequestCreatedOn BETWEEN '31-08-2015 12:00:00.000' and '15-09-2015 12:00:00.000'
Error
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
The values in the table are as follows :
The datatype of the column is varchar.
I tried many solutions but could not find any relevant answer. Please if anybody could help me with this issue.
Thanks in Advance!
Try to use convertion before comparison:
SELECT
*
FROM table_name
WHERE RequestCreatedOn BETWEEN CONVERT(DATETIME, '31-08-2015 12:00:00.000',105)
AND CONVERT(DATETIME, '15-09-2015 12:00:00.000',105)
Your problem appears to be that your SQL Server is using US dates m/d/y. You should use the standard format yyyy-mm-dd which should always be valid.
So for your query try:
select *
from table_name
where CAST(RequestCreatedOn as DateTime) BETWEEN '2015-08-31 12:00:00.000' AND '2015-09-15 12:00:00.000'
You should also use the most appropriate data type o I would be tempted to change the datatype in your table to datetime.

How to use extractvalue in SQL Server

I am trying to convert my Oracle query to SQL Server. The query shown below is causing some difficulty when trying to convert EXTRACTVALUE, XMLSEQUENCE, XMLTYPE functions.
Can anyone help me learn how to convert these to SQL Server?
EXTRACTVALUE(COLUMN_VALUE, 'pipeline/#name') NAME
FROM TABLE (SELECT XMLSEQUENCE(XMLTYPE(MESSAGE).EXTRACT('processEngine'))
FROM NMS_MESSAGES WHERE OBJECT_CODE='pe_monitor' AND ID=#WHERE:PARAM:USER_DEF:INTEGER:PARAM_PROCESS_ENGINE#)) INSTANCENAME
,(SELECT EXTRACTVALUE(COLUMN_VALUE, 'processEngine/#id') FROM TABLE (SELECT XMLSEQUENCE(XMLTYPE(MESSAGE).EXTRACT('processEngine'))
FROM NMS_MESSAGES WHERE OBJECT_CODE='pe_monitor' )) ENGINE_ID
Thanks in advance.
Think about XQuery. As I See that EXTRACTVALUE seems like #xml.values('(#node/#attr)[1]','type') and EXTRACT is #xml.nodes('path/path/path')
If know structure of xml that you ought to parse exactly than you will convert this query without any problems.

How to convert 71632.0638353154 to 71,632.06 in SQL Server and SYBASE?

Here I have a question about data converting regarding SQL Server and SYBASE.
Take 71632.0638353154 as an example, how to convert it to 71,632.06 in SQL Server and SYBASE?
I know that there is a convert() function in SQL Server and SYBASE, but every time when I tried to use it to convert that number, the database UI will throw me an exception.
I use sybase UI to excute below SQL instance:
select convert(varchar(30),convert(varchar(8),convert(money,71632.0638353154),1))
but this causes this error:
Insufficient result space for explicit conversion of MONEY value '71,632.06' to a VARCHAR field.
Would anyone tell me how to do it? thx.
I'm unable to test in Sybase but
SELECT CONVERT(VARCHAR(30), CAST(71632.0638353154 AS MONEY),1)
works for me in SQL Server.
The more generic solution is:
select cast(71632.0638353154 as decimal(10, 2))
The cast function is standard SQL and available in most databases. DECIMAL is a built-in data type.

Resources