I have a column of NUMBER(15) datatype which stores unix timestamps. I am trying to get a date and datetime from it. I have been able to get datetime from it but not a date. e.g. 1456342438 is the value i am trying to convert to date and datetime.
Either of these queries give me a DateTime:
select TO_DATE('1970-01-01','YYYY-MM-DD') + numtodsinterval(1456342438,'SECOND') from dual;
select TO_DATE('19700101000000','YYYYMMDDHH24MISS') + numtodsinterval(1456342438,'SECOND') from dual;
How can i get a date from this value?
select to_date('01/01/1970','mm/dd/yyyy') + numtodsinterval(1456342438,'SECOND'),
TRUNC(to_date('01/01/1970','mm/dd/yyyy') + numtodsinterval(1456342438,'SECOND')) from dual;
This worked.
Related
I'm looking to convert (using Oracle) a date timestamp
[2018-01-25T00:00:00.000+00:00] to a date [2018-01-24]. I've tried several formats however I can't seem to find the right one to convert it. I'm unsure of how to handle the +00:00.
Thanks in advance
It depends on what you really ask.
It you have a real Oracle timestamp and you want a string in format 'YYYY-MM-DD', you can use to_char():
select to_char(col, 'YYYY-MM-DD') as res from mytable
If you have a string in ISO timestamp format, and you want a string as a result:
select substr(col, 1, 10) as res from mytable
If you have a timestamp column and you want to set the time portion to 00:00:00:
select trunc(col) as res from mytable;
This returns a value of datatype date.
I have a truncated "date" attribute in the dataset, and need to convert the value below to a full DATE format (assuming it's the current year).
SELECT '6-May'
would output:
2020-05-06
SQL Server is very flexible about recognizing date formats. If you want to produce a date datatype, you can cast as follows:
select cast(mycol + '-2020' as date) mydate from mytable
Maturity column is of date datatype.
select TO_char(maturity,'YYYY-MM') || '-15' from tablename
The above query returns me the column value with text datatype. But how can i return the column value as date datatype.
You can cast to date using ::date. ie:
select (TO_char(maturity,'YYYY-MM') || '-15')::date from myTable
You could also do this using date arithmetic:
select maturity + (15 - extract(day from maturity)) * interval '1 day'
In general, I prefer not to convert to strings to do things that can be handled by date/time functions.
I need to convert an integer to a date format in AS400.
I have the field called ivdat8, which is integer in the formatted YYYYMMDD.
I need to use a WHERE clause to select data between two dates, today and 3 days ago.
I am using the below line to do this:
Where
ivdat8 Between (Select current date - 3 days from sysibm.sysdummy1) And (Select current date from sysibm.sysdummy1)
The current date from sysibm is a true date format, but ivdat is integer.
How can i cast ivdat8 to be a date format i can use within the WHERE clause ?
I have tried the below to convert the int to date:
cast(cast(A.ivdat8 as varchar(50)) as date)
&
TIMESTAMP_FORMAT(ivdat8,'YYYYMMDD')
Actually it's better for performance not to convert the ivdat8 column data, but do this with parameters like below.
select ivdat8
from table (values
int(to_char(current date - 2 days, 'YYYYMMDD'))
, int(to_char(current date - 5 days, 'YYYYMMDD'))
) t (ivdat8)
where ivdat8 between
int(to_char(current date - 3 days, 'YYYYMMDD'))
and int(to_char(current date, 'YYYYMMDD'));
Easiest way to do it without causing a complicated conversion in the query is to use this:
cast(digits(cast(A.ivdat8 as dec(8))) || '000000' as date)
The full where clause doesn't need to select from sysibm.dummy1 either.
where cast(digits(cast(A.ivdat8 as dec(8))) || '000000' as date) between current date - 3 days and current date
If you have indexes built on ivdat8 though, the fastest selection will be:
where A.ivdat8 between cast(Current date - 3 days as dec(8)) and cast(Current Date as dec(8))
Managed to convert the int to a date, first i had to cast it as a string, then use TO_DATE
To_Date(cast(A.ivdat8 as varchar(50)),"YYYYMMDD")
try this
Where cast(TIMESTAMP_FORMAT(cast(ivdat8 as varchar(8)), 'YYYYMMDD') as date)
Between (current date - 3 days) and current date
i have column with date type, and another with varchar type, i want to concatinate this column then convert it in timestamp
i try this but doesn't work
CAST (adjustmentDate || adjustmenttime AS TIMESTAMP (0) FORMAT 'YYYYMMDDHHMISS' )
Can you please try
select
cast(adjustmentDate as timestamp(0)) +
( CAST (adjustmenttime AS time(4) format 'HHMISS') - time '00:00:00' hour to second)
;
I works for me with some literals, so I'm hoping it can work for your table.