Oracle date time to datetime - oracle

I've looked at the answers on here but none of them seem to work.
I have the following date and time columns with example times how they are stored as below:
DATE_V TIME_V
26-NOV-15 10:58
How do I add these together into one column and convert it to a datetime as below? The trailing zeros are not necessary.
DateTime_V
2015-11-26 10:58:00.000
I’ve used the following which saves it as string but I can't get it to datetime.
TO_CHAR(DATE_V, 'YYYY-MM-DD') || ' ' || TO_CHAR(TO_timestamp(time_V, 'HH24:MI'),'HH24:MI')
= 2015-11-26 10:58

Assuming (bad word) that these are both stored as VARCHAR2 fields the following should work:
SELECT DATE_V, TIME_V, TO_DATE(DATE_V || ' ' || TIME_V, 'DD-MON-RR HH24:MI') AS DATETIME_V
FROM YOURTABLE
SQLFiddle here
Best of luck.

Related

Oracle ORA -932 expected Number got CHAR

I am running the following query and can't seem to figure out where the error is-
select case when month>=7 then substr(year,3,2)|| '/'||TO_NUMBER( substr(year,3,2))+1
else to_number(substr(year,3,2))-1 || '/'|| substr(year,3,2) end as fiscal_year
FROM ( SELECT DISTINCT to_Char(extract( year from date)) as year,
extract( month from date)as MONTH FROM TABLE )
I want to convert year to fiscal year like 19/20, 20/21 etc
The operator precedence rules means the string concatenation is happening before the addition; this expression:
substr(year,3,2)|| '/'||TO_NUMBER( substr(year,3,2))+1
is evaluated as
substr(year,3,2)|| '/'||TO_NUMBER( substr(year,3,2))
and then it tries to add 1 to that string result. Hence the error you get.
You can add parentheses to make it add 1 to the year number before then concatenating that:
substr(year,3,2)|| '/'|| (TO_NUMBER( substr(year,3,2))+1)
You could also do this without so much string manipulation:
select case when extract (month from your_date) >= 7 then
to_char(your_date, 'YY') || '/' || to_char(add_months(your_date, 12), 'YY')
else
to_char(add_months(your_date, -12), 'YY') || '/' || to_char(your_date, 'YY')
end as fiscal_year
FROM (
SELECT DISTINCT trunc(your_date, 'MM') as your_date
FROM your_table
)
db<>fiddle
and there are other options, of course.

Difference between two dates and getting result in timestamp

I´m trying to calculate the difference between two dates in Oracle and getting the result as a TimeStamp. This is the easiest thing to do in SQL Server, but it seems that Oracle does not have a easy way to solve this. I refuse to believe that I have to write that much code to get what I need. Can someone tell me if there is a easier way to get that difference?:
SELECT TO_CHAR(EXTRACT(HOUR FROM NUMTODSINTERVAL(enddate-startdate, 'DAY')), 'FM00')
|| ':' ||
TO_CHAR(EXTRACT(MINUTE FROM NUMTODSINTERVAL(enddate-startdate, 'DAY')), 'FM00')
|| ':' ||
TO_CHAR(EXTRACT(SECOND FROM NUMTODSINTERVAL(enddate-startdate, 'DAY')), 'FM00')
I need the result be something like:
enddate = '2017-03-01 17:30:00'
startdate = '2017-03-01 10:00:00'
difference: 07:30:00
Substract the two dates. Add the result to the current date (without any time component, trunc(sysdate)) and show only the time.
select to_char(trunc(sysdate) + (to_date('2017-03-01 17:30:00', 'YYYY-MM-DD HH24:MI:SS') -
to_date('2017-03-01 10:00:00', 'YYYY-MM-DD HH24:MI:SS'))
,'HH24:MI:SS')
from dual

Get diff of time Oracle

I have the next idea :
SELECT TO_CHAR('14:00:00','HH24:MI:SS') - MIN(TO_CHAR(DATETIME,'HH24:MI:SS')) AS MINFECHA
FROM ARCHIVO2
WHERE DIA='LUNES';
I want to get the difference between the 2 fields that should be something like
00:46:00
Any comment will be appreciated.
You can't add/subtract dates and times when they're in character strings. To accomplish what you're trying to do you need to convert the character strings to DATE values, perform the necessary calculations, and then convert the result back to a character string:
WITH DATE_DATA AS
(SELECT DIA,
DATETIME,
TO_DATE(TO_CHAR(DATETIME, 'DD-MON-YYYY') || ' ' || '14:00:00', 'DD-MON-YYYY HH24:MI:SS') AS BASE_TIME
FROM ARCHIVO2)
SELECT DIA,
DATETIME,
BASE_TIME,
(DATETIME - BASE_TIME) * 1440 AS MINUTES_LATE
FROM DATE_DATA;
SQLFiddle here
Best of luck.

PLSQL Date Filter Error

I have a table with data since 2012. I need to get data in this table from 31 days back from given date. so i wrote below query to get data.
select ('[' || work_date || '] ' || field_name || ' - ' ||work_desc) d
from DAILY_WORK
where TO_CHAR(work_date,'DD/MM/YYYY') >= TO_CHAR(to_date('30-Jan-13','dd-MON-yyyy') - (31),'DD/MM/YYYY')
order by work_date desc
When i execute this query, it returns data in below dates only.
31-AUG-12
31-OCT-12
30-DEC-12
31-DEC-12
But actually i need to get data from 2012-12-30 to 2013-01-30.
How could i do this ?
use this:
select ('[' || to_char(work_date, 'dd-MON-yyyy') || '] ' || field_name || ' - ' ||work_desc) d
from DAILY_WORK
where work_date >= to_date('30-Jan-2013','dd-MON-yyyy') - 31
order by work_date desc
You can use the below query:
select * from DAILY_WORK where work_date >= TRUNC(to_date('01-30-2013','MM/DD/YYYY')) - 31
Check date format which tool you are using As per tool i am using date format is 'MM/DD/YYYY'
This query give correct result.You can check.

oracle 10g , query format

I have a small doubt. I have below query
SELECT empno
|| '|'
|| ename
|| '|'
|| sal
|| '|'
|| comm
FROM (SELECT empno,
ename,
sal,
comm
FROM emp);
the output is coming as :
7611|Grp Fract|2001|.11
7499|ALLEN WOR|1600|.22
7521|WARD|1250|10.23
7566|JONES|2975|234.23
7654|MARTIN|1250|.98
the last column COMM has value as 0.11, 0.22, 0.98
but the above query returns data as .11,.22,.98. Can anyone help me understanding why it is happening when I am concatenating the data and how to resolve this , I need exact value as it is in COMM column.
The o/p should be as
7611|Grp Fract|2001|0.11
7499|ALLEN WOR|1600|0.22
7521|WARD|1250|10.23
7566|JONES|2975|234.23
7654|MARTIN|1250|0.98
Ths comm column is defined as number(7,2).
Thanks
Use the TO_CHAR function with a proper format model. It seems you want LTRIM(TO_CHAR(comm,'999990.99')) here.
The LTRIM(TO_CHAR(comm,'999990.99')) works for values which have 2 digits after decimal point.
If value like 0.123523 is used above it rounds off the decimal places keeping 2 digits only.

Resources