Date Conversion in snowflake - snowflake-cloud-data-platform

I am facing issue in date conversion in snowflake
Tab1 : Date(DD-MM-YYYYY) Col1 : 03-10-2018
Tab2 : Date(YYYY-MM-DD) Col2 : 2018-10-03
I need to join
Tab1.Col1=Tab2.Col1
I am getting error as
Date '03-10-2018' is not recognized.
How to convert this date into 'YYYY-MM-DD' Please advise.
I have tried with all format :
TO_TIMESTAMP_NTZ(Date(DOS, 'YYYY-MM-DD')),
Date((DOS, 'YYYY-MM-DD'),
TO_Varchar(Date(DOS, 'DD-MM-YYYY'),'YYYY-MM-DD')
All above mentioned formula facing the same error :
Thanks in advance

I don't quite understand this part of your question if datatype of your columns tab1.col1 and tab2.col2 is date: Date(DD-MM-YYYYY) and Date(YYYY-MM-DD)
Date is date, it does not have any formats when it is saved. You apply formats when you insert dates in text format to tell in which format the given value is, or when you select the date and want to output it in some specific format.
This works with date-type columns, joining is done without any formatting functions:
create table tab1 (col1 date);
create table tab2 (col2 date);
insert into tab1 (col1) values ( to_date('03-10-2018','dd-mm-yyyy') );
insert into tab2 (col2) values ( to_date('2018-10-03','yyyy-mm-dd') );
select *
from tab1, tab2
where Tab1.Col1=Tab2.Col2;

Related

How to convert UNIX Epoch to date in Snowflake

My table INV_STOCK_TIME with 4000 rows with unix epoch data in snowflake. That data needs to be converted to date. Here is sample data and the desired output should be in 2 columns as given below
STOCK_RAWTIME
1617772221333
1616217315003
1601376748863
STOCK_RAWTIME STOCK_DATE
1617772221333 2021-04-07
1616217315003 2021-03-20
1601376748863 2020-09-29
I could to convert value in column using either of the below command.
select to_char(to_timestamp_tz(1617772221333,3),'YYYY-MM-DD') from dual;
OR
SELECT to_char(dateadd('ms',timestamp_col,'1970-01-01'),'YYYY-MM-DD') from (select 1617772221333 as timestamp_col) as timetest;
I would like to pass the values of column1 STOCK_RAWTIME (unix epoch value)
to populate column2 STOCK_DATE. I'm unable to pass list of values to convert column1 records with below error
Format argument for function 'TO_TIMESTAMP_TZ' needs to be a string
I believe you can get this requirement with the following:
SELECT STOCK_RAWTIME as STOCK_RAWTIME, to_date(STOCK_RAWTIME)AS STOCK_DATE FROM (select STOCK_RAWTIME from INV_STOCK_TIME);
Resolved with this step in snowflake as to_date or to_timestamp works fine:
alter table inv_stock_time add column stock_date date;
update inv_stock_time set stock_date=to_date(STOCK_RAWTIME);

insert into not working on oracle in date field

I have created table as:
create table ot.eligible(
id number,
name varchar2(255),
join_date date,
left_date date
);
The problem I am getting is I am unable to insert the data which has date column:
insert into ot.eligible(ID,NAME,JOIN_DATE,LEFT_DATE) values(1,'ashwin',to_date(01/12/2017,'MM/DD/yyyy'),to_date(01/2/2018,'mm/dd/yyyy'));
before i tried without using to_Date and numeric found error was there so,I added to_date but,at this time I got error as:
ORA-01858: a non-numeric character was found where a numeric was expected
My sysdate is:
10/17/2019 8:42:29 PM
when I hitted select sysdate from dual;
You are missing single quotes in dates -
insert into ot.eligible(ID,
NAME,
JOIN_DATE,
LEFT_DATE)
values(1,
'ashwin',
to_date('01/12/2017','MM/DD/yyyy'),
to_date('01/02/2018','mm/dd/yyyy'))

I need to convert date (25/01/2019) to Jan'19 format in sql server

I need to convert the contents of a DATE colum and store it in another column.
Example
(25/01/2019) to Jan'19.
This following option is not a good one considering performance but you can get your desired result-
SELECT
REPLACE(CAST(FORMAT(CONVERT(DAte,'25/01/2019',103), 'MMM-yy') AS VARCHAR),'-','''')
Use of FORMAT is to get value is desired format.
if you have hardcode date you can use this one :
SELECT
REPLACE(FORMAT(CONVERT(Date,'25/01/2019',103), 'MMM-yy'),'-','''')
if you have column in database then you can use :
SELECT REPLACE(FORMAT(DateColumnHere, 'MMM-yy') ,'-','''')
FROM Table1
A date type column has no format but we can change the display format by converting it to a string. In your case, [myDate] should be a string type column such as varchar, and we'll cast it to date before converting the display format. So consider :
with tab([myDate]) as
(
select '25/01/2019'
)
select convert(varchar,
cast(concat(substring(myDate,7,4),'-',
substring(myDate,4,2),'-',
substring(myDate,1,2)) as date) ,6)
as [myDate]
from tab;
myDate
---------
25 Jan 19
Demo

Converting VARCHAR to Date using TO_DATE

I have a set of data where the col is VARCHAR() but I need it to be in DATE format.
I was trying to do it as follows:
CREATE OR REPLACE TABLE df_new
AS SELECT
col1 AS NAME
col2 AS first_name
col3 AS last_name
,TO_DATE(col4, 'yyyymmdd') AS date
FROM df_old
but I am getting an error "Can't parse '' as date with format 'yyyymmdd'".
I have tried messing with the input for the date format (such as 'yyyy/mm/dd') but I am pretty new to SQL so I am unsure of how to proceed.
Just use TRY_TO_DATE, it will return NULL for values where it can't parse the input.
If you are certain that all of your values other than NULLs are of the string 'yyyymmdd' then the following will work in snowflake.
TO_DATE(TO_CHAR(datekey),'yyyymmdd')
Sounds like some of your col4 entries are NULL or empty strings. Try maybe
... FROM df_old WHERE col4 ~ '[0-9]{8}'
to select only valid inputs. To be clear, if you give the format as 'YYYYMMDD' (you should use uppercase) an entry in col4 has to look like '20190522'.
All dates are stored in an internal format.
You cannot influence how a date is STORED. However you have it format it the way you want when you pull the data from the database via query.
I was able to resolve by the following:
CREATE OR REPLACE TABLE df_new
AS SELECT
,col1 AS NAME
,col2 AS first_name
,col3 AS last_name
,CASE WHEN "col4" = '' then NULL ELSE TO_DATE(col4, 'YYYYMMDD') AS date
FROM df_old
I resolved the issue as follows:
CREATE OR REPLACE TABLE df_new
AS SELECT
col1 AS NAME
,col2 AS first_name
,col3 AS last_name
,CASE WHEN col4 = '' THEN NULL ELSE TO_DATE(col4, 'YYYY-MM-DD') END AS date
FROM df_old

Automatic adding current time in SQL Server 2012

I have table with a column of datatype time(4).
When I'm inserting values into the table I need to auto insert just the current time with milliseconds (without date) into that column. I have tried with time stamp, date time... but without any success.
If you want to get the current time in SQL Server 2012, just use the CAST operator to achieve this:
SELECT
CAST(SYSDATETIME() AS TIME(4))
This will get the current date & time, and cast this to just the time, as you need it.
To achieve automatic entry of time values when a new row is added, you can use the above expression in a default constraint. For example:
DECLARE #T AS table
(
SomeValue integer NOT NULL,
TheTime time(4) NOT NULL
DEFAULT CAST(SYSDATETIME() AS time(4))
);
INSERT #T (SomeValue)
VALUES (1);
SELECT
SomeValue,
TheTime
FROM #T;
As far as I understand, this is what you may be looking for:
SELECT GETDATE() 'Today',
CONVERT(TIME(4),GETDATE()) 'time_only'
You could use the above conversion in your insert statement as following:
INSERT INTO TABLEA (
column1
,column2
,record_insert_time
)
VALUES (
value1
,value2
,CONVERT(TIME(4), GETDATE())
);

Resources