How to convert UNIX Epoch to date in Snowflake - snowflake-cloud-data-platform

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);

Related

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

Convert to mm/dd/yyyy and select max value

Using SQL Server 2014, I have a date field named LAST_BASELINE_UPDATE_DATE that is stored as datetime.
I used the CONVERT function to convert to mm/dd/yyyy:
convert(date,LAST_BASELINE_UPDATE_DATE,101) as BASELINE_UPDATE_DATE
What I want to be able to do is then select the MAX value of BASELINE_UPDATE_DATE without creating a table. Is this even possible?
It's a little unclear from your post what your data is and what you're trying to get out. Here are a couple solutions, hopefully one of which is applicable
Assuming you want your result as a string formatted mm/dd/yyyy you can do this
select convert(varchar(10), max(LAST_BASELINE_UPDATE_DATE), 101))
from YourTable
If you just need it as a date, just do
select max(LAST_BASELINE_UPDATE_DATE)
from YourTable
if LAST_BASELINE_UPDATE_DATE is already a string (formatted mm/dd/yyyy) and you want it as a date,
select max(convert(date, LAST_BASELINE_UPDATE_DATE, 101))
from YourTable
I think you are complicating this. Just do the conversion on the max datetime values.
declare #table table (LAST_BASELINE_UPDATE_DATE datetime)
insert into #table
values
('20160701 12:21'),
('20160705 03:21'),
('20160401 19:21'),
('20161201 04:21')
select
convert(varchar(10),max(LAST_BASELINE_UPDATE_DATE),101)
from
#table
This method converts your single returned row, which is the max() value of your datetime columns, as opposed to converting every row and then finding the max value.

Cast select column to date in view to then run query against

I have a view where I Select about 100 rows to allow users to easily query data. In this data, I have a field that is sometimes a date and sometimes text. A date or text depends on type. I cast to a date value like so.
SELECT Cast(Value as Date) as column
from Table
Where type = 1
When you then try to run a query against this column, you get a Conversion failed when converting date and/or time from character string. Here is the query.
SELECT Column
From View
WHERE Column BETWEEN '01/01/2015' AND '12/31/2015'
I have another field that is a date and if I replace it in this query, the query works. Likewise the data from the whole table will load. Any ideas are appreciated. Thanks
Basically, I need this Query to work and not give me the error described above.
select cast(value as date)
from Value
where type = 1
and cast(value as date) between '01/01/2015' and '12/31/2015'
My guess is that you have entries in that column that cannot be converted to datetime. You may be able to find them by running
Select * from table where isdate(value) = 0

how to cast or convert numeric/varchar to date data type field in sql and use in update statement?

Good day!
I have 2 questions how to update a date data type column field using varchar and numeric column field
1.)mydate varchar(8)--> varchar column field
SELECT mydate from mytable
Result: 20141120
my question is how can I update my date column field using my varchar column field using cast or convert
update table2
set date = (select mydate from mytable)
which I get an error!!! and I'm stuck.
2.)mydate numeric(8) --> numeric column field
SELECT mydate from mytable
Result:
20101015
20140910
etc.......
update table2
set date = (select mydate from mytable a, mytable2 b
where a.id=b.id)
my question is how can I update my date column field using my numeric column field using cast or convert
I used different CAST and CONVERT but still I'm getting error!
What is the correct syntax for this?
Thank your for your help!
To convert a string to a date field you will need to use the CONVERT function:
CONVERT(datetime, mydate, 101)
This is expecting a string field, so if your mydate field is really a numeric field then you will need to CAST that to a string, so the CONVERT command will then look like:
CONVERT(datetime, CAST(mydate as VarChar), 101)
The third parameter of the function is determined by the format of the date in the previous parameter, you can find the full list on MSDN at http://msdn.microsoft.com/en-us/library/ms187928.aspx

date format remain same in oracle database

INSERT INTO DELLL (
DATETIMEMY)
SELECT to_date(to_char(SU_MODIFYDATE, 'YYYY/MM/DD'),'YYYY/MM/DD') AS DATETIMEMY
FROM SER_TBLSERVICES WHERE SVE_SERVICEID=422
SU_MODIFYDATE = 01/18/2013
but after insertion in DELLL date (DATETIMEMY) format still same is still same
DATETIMEMY = 01/18/2013
you dont assign a format to a DATE. They are stored internally as a number and have no format. the format comes when you want to select the date. so in your case you should just do this:
first just insert the date as-is, do not try to convert it to a char and back again:
SQL> INSERT INTO DELLL (
2 DATETIMEMY) SELECT SU_MODIFYDATE
3 FROM SER_TBLSERVICES WHERE SVE_SERVICEID=422;
1 row created.
now to select it in a specific format, you can use TO_CHAR to format it.
SQL> select * from delll;
DATETIMEMY
----------
01/18/2013 <-- which isn't what you want to see, you wanted to see yyyy/mmm/dd. so...
SQL> select to_char(DATETIMEMY,'yyyy/mm/dd') DATETIMEMY from DELLL
2 /
DATETIMEMY
----------
2013/01/18
or, to apply to all selects in that session that have a date datatype, you can alter the default display format:
SQL> alter session set nls_date_format='yyyy/mm/dd';
Session altered.
SQL> select * from delll;
DATETIMEMY
----------
2013/01/18
The INSERT or SELECT in your example does not modify how a date is stored. Dates are formatted when you SELECT them, using your current connection format preferences/locale.
You are using TO_DATE, selecting that returned value is the same as with any other date, it will be formatted according to the locale.
DazzaL is right with his answer which discusses formatting, storage and retrieval of dates.
I would like to discuss what you are doing in your code.
Oracle stores dates upto precision of a seconds. In simple terms you can retrieve the date in MM/DD/YYYY HH24:MI:SS format.
By issuing to_date(to_char(SU_MODIFYDATE, 'YYYY/MM/DD'),'YYYY/MM/DD') you are actually truncating the date up to the day part. So when you try to retrieve this value, the HH24:MI:SS part will have 00:00:00 because you have truncated the date.
If you want all the details (from year, month, day to hour, minute and seconds) from SU_MODIFYDATE to be inserted into the column in DELLL, you should just use
INSERT INTO DELLL (
DATETIMEMY)
SELECT SU_MODIFYDATE AS DATETIMEMY
FROM SER_TBLSERVICES WHERE SVE_SERVICEID=422
This will ensure all parts of date in SU_MODIFYDATE column are inserted into the new column.
If you want to truncate the date, for example, upto the minute the use to_date(to_char(SU_MODIFYDATE, 'YYYY/MM/DD HH24:MI'),'YYYY/MM/DD HH24:MI')
Likewise, you can truncate dates from year part upto the second part as per your choice.
If you want to insert truncated dates then you should use the query you already have. If you want to insert dates with all their parts then avoid truncating by using to_char and to_date.

Resources