concat date and varchar and convert to timestamp - sql

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.

Related

How to substract 2 varchar dates in oracle?

I have these varchar : 20211026231735.
So I would like a query to substract actual sysdate to that date and convert the substraction to DAY HOURS AND SECONDS.
select TO_CHAR(SYSDATE,'YYYYMMDDHH24MISS') - start_time from TABLEA where job_name='jOB_AA_BB';
I get 4220.
Any help please? Thanks
When you do datetime arithmetic with the DATE datatype, you get back a NUMBER of days. To get an INTERVAL you can subtract two TIMESTAMPs. You don't say what the data type is for start_time, but you might get away with this:
select localtimestamp - start_time
from tablea where job_name='jOB_AA_BB';
LOCALTIMESTAMP gives you a TIMESTAMP value in the current session time zone. There's also CURRENT_TIMESTAMP, which give you the same thing in a TIMESTAMP WITH TIME ZONE and SYSTIMESTAMP that gives you the database time in TIMESTAMP WITH TIME ZONE. You may need to convert your start_time to avoid time zone differences, if any.
You can us the function numtodsinterval to convert the results of date arithmetic to an interval. If necessary then use extract to pull out the needed components.
with tablea(job_name, start_time) as
(select 'jOB_AA_BB','20211026231735' from dual)
select numtodsinterval((SYSDATE - to_date( start_time,'yyyymmddhh24miss')),'hour') date_diff
from tablea where job_name='jOB_AA_BB' ;
with tablea(job_name, start_time) as
(select 'jOB_AA_BB','20211026231735' from dual)
select extract (hour from date_diff) || ':' || extract (minute from date_diff)
from (
select numtodsinterval((sysdate - to_date( start_time,'yyyymmddhh24miss')),'day') date_diff
from tablea where job_name='jOB_AA_BB'
);
NOTE: I am not sure how you got any result, other than an error, as your query winds up as a string - a string. You should not convert sysdate to a string but your string to a date (better yet store it as the proper data type - date).
You can convert the value to a date (rather than converting SYSDATE to a string) and then subtract and explicitly return the value as an INTERVAL DAY TO SECOND type:
SELECT (SYSDATE - TO_DATE('20211026231735', 'YYYYMMDDHH24MISS')) DAY TO SECOND
FROM DUAL;
Or, for your table:
SELECT (SYSDATE - TO_DATE(start_time,'YYYYMMDDHH24MISS')) DAY(5) TO SECOND
FROM TABLEA
WHERE job_name='jOB_AA_BB';
db<>fiddle here

How do I convert iso to DATE without the trailing time string?

The following function returns dates in this format, "2021-01-01T00:00:00.000Z" and all I need is just the date portion of "2021-01-01".
DATE_TRUNC(‘day’, timestamp)
The return value from your current call to DATE_TRUNC already functionally is the date 2021-01-01, which in timestamp form is at midnight. That being said, if you want to view as a date only, then maybe you want this:
SELECT FORMAT_DATETIME("%Y-%m-%d", DATE_TRUNC('day', timestamp))
FROM yourTable;
Another trick which might work on BigQuery is to cast the timestamp to a VARCHAR of the right length:
SELECT CAST(DATE_TRUNC('day', timestamp) AS VARCHAR(10))
FROM yourTable;

How to convert BIGINT to DATE in Redshift?

I am trying to figure out how to convert and format a BIGINT field (i.e. 20200301) to a DATE type field using Redshift SQL. I was successful in getting the snippet below to work but I believe that returns a string and I need a valid date returned in 'YYYY-MM-DD' format. I've tried several other version unsuccessfully. Thank you in advance.
'''to_char(to_date(date_column::text, 'yyyymmdd'), 'yyyy-mm-dd')'''
You just want the to_date() part:
select to_date(date_column::text, 'YYYYMMDD')
When it is a timestamp we need the below code to convert into correct value.
select trunc(TIMESTAMP 'epoch' + date_column / 1000 * INTERVAL '1 second')

convert text to date datatype

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.

Convert unix timestamp to Date and DateTime - SQL/ORACLE

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.