Convert Number into Date format in oracle - sql

I have number column in oracle database which stores a timestamp. I want to convert this into a DATE and I have no clue on how to do it.
Below is what I am looking for, please suggest.
The value 1465484486246 should be converted to 2016/06/09 15:01:26,246 GMT

You can use NUMTODSINTERVAL along with to_date to achieve what you want:
TO_CHAR(TO_DATE('1970/01/01 00:00:00', 'YYYY/MM/DD HH24:MI:SS') + NUMTODSINTERVAL(col / 1000,'SECOND'),
'YYYY/MM/DD HH24:MI:SS')
Here I assume that your timestamp column is called col. The timestamp 1465484486246 you gave us is in milliseconds, which is why I used col / 1000 in NUMTODSINTERVAL.

Related

How to use TO_DATE to update a DATE field in SQL to a PM time

What I need to do is pretty simple.
I just need to update a DATE field in SQL to a PM time.
Only thing is, if I use the TO_DATE function to update to an AM time, no problem...
TO_DATE('2021-09-30 11:00:00', 'YYYY-MM-DD HH:MI:SS')
However if I try to do the same thing to set to a PM time using military time...
TO_DATE('2021-09-30 23:00:00', 'YYYY-MM-DD HH:MI:SS')
It says that the hour has to be between 1 and 12.
Any suggestions?
Thanks
According to the Oracle documentation, in order to convert a time in 24 hour format, you need to use HH24. When you use just HH, Oracle assumes the time to be in AM/PM format, i.e. that the number must be between 1 and 12.
So you need to change your code to the following
TO_DATE('2021-09-30 23:00:00', 'YYYY-MM-DD HH24:MI:SS')
Refer to this db<>fiddle

Get date and time from date column in oracle

I inserted date and time in database, now when trying to retrieve date and time both from database only getting time part.
I tried insert date using TO_DATE('08/13/2019 09:10:03', 'MM/DD/YYYY HH24:MI:SS') into 'Time' col. Now trying to get date from table using TO_DATE(Time, 'DD/MON/RR HH24:MI:SS'), but only getting date part. My database nls date format is "DD/MON/RR". 'Time' col is date type and I'm using oracle 10g xe.
I can get date and time using TO_CHAR(Time, 'DD/MON/RR HH24:MI:SS') but as I need to use this in comparison operation like below:
select TO_CHAR(Time,'DD/MON/RR HH24:MI:SS') from Table where (TO_CHAR(Time, 'DD/MON/RR HH24:MI:SS') <= TO_DATE('08/07/2019 10:13:52', 'MM/DD/YYYY HH24:MI:SS'))
it gives this error 'ORA-01830: date format picture ends before converting entire input string'. Also tried to use TO_DATE(TO_CHAR(Time, 'DD/MON/RR HH24:MI:SS')), still it gives only time part. Should I use TIMESTAMP datatype for 'Time' col?
I want to get date and time from table where i can use them in comparison operation.
Date need not be converted into date again.
You can simply write your query like this:
SELECT
TIME -- use to_char for formatting the output date
FROM Table
WHERE
TIME <= TO_DATE('08/07/2019 10:13:52', 'MM/DD/YYYY HH24:MI:SS')
Cheers!!

Oracle Convert Char to Date

I have the following date in oracle table:
'2017-08-01 00:00:00,000000000'
I want to convert this to date which I am using the following but I don't know how to deal with zeroes?!
.
.
.
T.EXECUTION_LOCAL_DATE_TIME
between to_date('2017-08-01 00:00:00,000000000', 'yyyy-mm-dd hh:mi:ss')
and to_date('2017-08-10 00:00:00,000000000', 'yyyy-mm-dd hh:mi:ss');
Could anyone help me with this?
Oracle dates do not support milliseconds. Assuming your EXECUTION_LOCAL_DATE_TIME column is a date, then the following comparison is the best you can do:
T.EXECUTION_LOCAL_DATE_TIME
BETWEEN TO_DATE('2017-08-01 00:00:00', 'yyyy-mm-dd hh:mi:ss') AND
TO_DATE('2017-08-10 00:00:00', 'yyyy-mm-dd hh:mi:ss');
If you wanted to convert a text string with milliseconds to a timestamp, you could use something like this:
TO_TIMESTAMP ('2017-08-01 00:00:00,000000000', 'yyyy-mm-dd hh:mi:ss,ff')
Generally speaking this values in not a date but a timestamp so I would use following conversion:
select to_timestamp('2017-08-01 00:00:00,000000000', 'yyyy-mm-dd hh24:mi:ss,ff6')
from dual;
Technically you can have non zeros after comma, so If you want to get correct but not truncated value use timestamp date type.

oracle between clause inclusive format HH24:MI

My query looks something like:
select *
from mytable
where date_field between to_date(#from#, 'YYYY/MM/DD HH24:MI')
and to_date(#to#, 'YYYY/MM/DD HH24:MI')
As an example:
if from = 2012/07/18 00:00 and
to = 2012/07/18 00:09
will this include records with timestamp 2012/07/18 00:09:01 to 2012/07/18 00:09:59?
or should I change the statement to:
select *
from mytable
where date_field >= to_date(#from#, 'YYYY/MM/DD HH24:MI')
< to_date(#to#, 'YYYY/MM/DD HH24:MI')
here substituting from : 2012/07/18 00:00 & to: 2012/07/18 00:10 should give me all records with timestamp between midnight & 9M59S past midnight, which is what I want.
The between clause accepts both the interval bounds.
I suggest the second option to you
select *
from mytable
where date_field >= to_date(#from#, 'YYYY/MM/DD HH24:MI')
< to_date(#to#, 'YYYY/MM/DD HH24:MI')
You may find this article interesting.
The date conversion is going to convert the values into dates, which contain all date elements. You have not specified seconds in the strings, so these will become 0.
In other words, the range ":01" - ":59" is not included.
Since you are working with strings and the strings have date elements in the proper order for comparison, why not do string compares instead:
where to_char(datefield, 'YYYY/MM/DD HH24:MI') between #from# and #to#
I think this does exactly what you want, without fiddling around with date arithmetic.
You can also change the statement as you propose, by incrementing the #to# column and using "<" instead of between.
You could do something like this:
SELECT *
FROM mytable
WHERE date_field between to_date(#from#, 'YYYY/MM/DD HH24:MI')
AND to_date(#to#||':59', 'YYYY/MM/DD HH24:MI:SS')
Ignoring the date portion of the DATE elements, since both 00:09:01 and 00:09:59 come after your "to" time of 00:09:00, no, this query will not include those records.
If you want to include those records, you will need to extend your "to" time to 00:10:00 or TRUNC your records's timestamps to the nearest minute.
Edit:
If your from and to are only accurate to the minute, I'd do this:
SELECT *
FROM mytable
WHERE date_field >= to_date(#from#, 'YYYY/MM/DD HH24:MI')
AND date_field < to_date(#to#, 'YYYY/MM/DD HH24:MI') + 1/24/60/60 /* 1 minute */
And make sure you use bind variable for from and to. Is this ColdFusion? If so, use cfqueryparam.

Oracle SQL : timestamps in where clause

I need to look up rows within a particular time frame.
select *
from TableA
where startdate >= '12-01-2012 21:24:00'
and startdate <= '12-01-2012 21:25:33'
I.e.: I need to look up rows with timestamp precision of SECONDS. How do I achieve this?
FYI: The startdate column is of type TIMESTAMP.
to_timestamp()
You need to use to_timestamp() to convert your string to a proper timestamp value:
to_timestamp('12-01-2012 21:24:00', 'dd-mm-yyyy hh24:mi:ss')
to_date()
If your column is of type DATE (which also supports seconds), you need to use to_date()
to_date('12-01-2012 21:24:00', 'dd-mm-yyyy hh24:mi:ss')
Example
To get this into a where condition use the following:
select *
from TableA
where startdate >= to_timestamp('12-01-2012 21:24:00', 'dd-mm-yyyy hh24:mi:ss')
and startdate <= to_timestamp('12-01-2012 21:25:33', 'dd-mm-yyyy hh24:mi:ss')
Note
You never need to use to_timestamp() on a column that is of type timestamp.
For everyone coming to this thread with fractional seconds in your timestamp use:
to_timestamp('2018-11-03 12:35:20.419000', 'YYYY-MM-DD HH24:MI:SS.FF')