i have these two queries.
to_char(to_date('1970-01-01 **1:00:00**', 'yyyy-mm-dd hh24:MI:SS') + NET_START.STARTPROC/1000/60/60/24, 'yyyy-mm-dd hh24:MI:SS') as STARTPROC;
SELECT TO_CHAR(SYSTIMESTAMP, 'tzr') FROM dual
query 2 returns a string that contains 1:00:00 and i would like to use that query inside query 1 to replace the highlighted 1:00:00.
something like this
to_char(to_date('1970-01-01 (SELECT TO_CHAR(SYSTIMESTAMP, 'tzr') FROM dual)’, 'yyyy-mm-dd hh24:MI:SS') + NET_START.STARTPROC/1000/60/60/24, 'yyyy-mm-dd hh24:MI:SS') as STARTPROC
Oracle use || to connect string
to_date('1970-01-01'||(SELECT TO_CHAR(SYSTIMESTAMP, 'tzr') FROM dual), 'yyyy-mm-dd hh24:MI:SS')
try this.
You can simply use the || operator without query as follows:
to_char(to_date('1970-01-01 ' || TO_CHAR(SYSTIMESTAMP, 'tzr') , 'yyyy-mm-dd hh24:MI:SS')
+ NET_START.STARTPROC/1000/60/60/24, 'yyyy-mm-dd hh24:MI:SS') as STARTPROC;
Related
select 'BETWEEN TO_DATE(''' || to_char(CURRENT_DATE, 'dd/MM/yyyy 00:00:00') || ''', ''DD/MM/YYYY HH24:MI:SS'') AND TO_DATE(''' || to_char(CURRENT_DATE + 1, 'dd/MM/yyyy 00:00:00')
Is there something bad in this Query? It's not the full query it's just a part of it. I'ts throwing this error:
ORA-01756: quoted string not properly terminated
Use the q-quoting mechanism.
By the way, your code can be simplified to
select q'[between trunc(current_date) and trunc(current_date + 1)]' result
from dual;
No need to to_char and then to_date current_date; it already is DATE datatype, just remove (truncate to midnight) time component.
By the way #2, format mask you used is wrong; should be dd/mm/yyyy hh24:mi:ss (not dd/mm/yyyy 00:00:00)
If you insist (though, I don't know why would you), then
select q'[between to_date(to_char(current_date , 'dd/mm/yyyy hh24:mi:ss'), 'dd/mm/yyyy hh24:mi:ss')]' ||
q'[ and to_date(to_char(current_date + 1, 'dd/mm/yyyy hh24:mi:ss'), 'dd/mm/yyyy hh24:mi:ss')]'
as result
from dual;
Does it work? Yes:
SQL> select q'[between to_date(to_char(current_date , 'dd/mm/yyyy hh24:mi:ss'), 'dd/mm/yyyy hh24:mi:ss')]' ||
2 q'[ and to_date(to_char(current_date + 1, 'dd/mm/yyyy hh24:mi:ss'), 'dd/mm/yyyy hh24:mi:ss')]'
3 as result
4 from dual;
RESULT
--------------------------------------------------------------------------------
between to_date(to_char(current_date , 'dd/mm/yyyy hh24:mi:ss'), 'dd/mm/yyyy
hh24:mi:ss') and to_date(to_char(current_date + 1, 'dd/mm/yyyy hh24:mi:ss'),
'dd/mm/yyyy hh24:mi:ss')
SQL>
Now copy/paste the result into another query and verify it:
SQL> select *
2 from dual
3 where sysdate
4 -- this is the "result":
5 between to_date(to_char(current_date , 'dd/mm/yyyy hh24:mi:ss'), 'dd/mm/yyyy hh24:mi:ss') and to_date(to_char(current_date + 1, 'dd/mm/yyyy hh24:mi:ss'), 'dd/mm/yyyy hh24:mi:ss')
6 ;
D
-
X
SQL>
Didn't fail, eh?
I've a Oracle query like this:
SELECT * FROM
WHERE
CODE='AMB01'
AND MyHour BETWEEN
TO_DATE('11/01/2018 16:00:00', 'DD/MM/YYYY hh24:mi:ss') AND
TO_DATE('11/01/2018 16:30:00', 'DD/MM/YYYY hh24:mi:ss')
It return an error :
Month not valid
Myhour is a String field.
What can be the error ?
Thanks
Edited:
You need to convert MyHour, not between part.
SELECT * FROM
WHERE
CODE='AMB01'
AND TO_DATE(MyHour, 'DD/MM/YYYY hh24:mi:ss') BETWEEN
'11-01-2018 16:00:00' AND
'11-01-2018 16:30:00'
if MyHour type is different , Set column type as Date
UPDATE_DATETIME DATE
SELECT * FROM YourTable
WHERE MyHour BETWEEN
TO_DATE('11/01/2018 16:00:00', 'DD/MM/YYYY hh24:mi:ss') AND
TO_DATE('11/01/2018 16:30:00', 'DD/MM/YYYY hh24:mi:ss')
I'm trying to convert data in format 2016/06/26 into 26/06/2016 00:00:00
I was trying few option all the time getting error "Invalid months name",
Any idea/advice?
Thanks
select to_date('2016/05/07 00:00:00','mm/dd/yyyy HH24:MI:SS') from dual
In order to convert a string to a date you need to convert it first to a date. Your problems is that you are trying to format a string not a date. So for you specific case it would be:
--convert it first to a date
select to_date('2016/05/07 00:00:00','yyyy/mm/dd HH24:MI:SS')
from dual
--then convert it to a string in the format you want:
select to_char( to_date('2016/05/07 00:00:00','yyyy/mm/dd HH24:MI:SS'),
'mm/dd/yyyy HH24:MI:SS' )
from dual
--since you want it as a date:
--then convert it to a string in the format you want:
select to_date( to_char( to_date('2016/05/07 00:00:00',
'yyyy/mm/dd HH24:MI:SS'),
'mm/dd/yyyy HH24:MI:SS' )
'mm/dd/yyyy HH24:MI:SS' )
from dual
If you want just to convert your string into a date no matter the format, just use the first select I showed. Thanks to #Boneist in comments for pointing it out.
I need a query to insert rows into a table within the last N days from today.
Insert into Table select 'xpto', name
from users where login_date between
TO_DATE(:DATE || ' 00:00:00', 'mm/dd/yyyy HH24:MI:SS')
and TO_DATE(:DATE || ' 23:59:59', 'mm/dd/yyyy HH24:MI:SS')
I need this :DATE to be represented by last 30 days... (each day)..
So it would be 30 inserts.
How can I do that?
EDIT
Lets say I want to insert data from the last 30 days... So we have:
01/18/2016
01/19/2016
01/20/2016
...
02/01/2016
02/02/2016
02/03/2016
....
02/15/2016
....
02/22/2016
I want a single query - or statement - to insert my data based on each days above like this:
Insert into Table select 'xpto', name
from users where login_date between
TO_DATE('01/15/2016' || ' 00:00:00', 'mm/dd/yyyy HH24:MI:SS')
and TO_DATE('01/15/2016' || ' 23:59:59', 'mm/dd/yyyy HH24:MI:SS')
--- another insert
Insert into Table select 'xpto', name
from users where login_date between
TO_DATE('01/16/2016' || ' 00:00:00', 'mm/dd/yyyy HH24:MI:SS')
and TO_DATE('01/16/2016' || ' 23:59:59', 'mm/dd/yyyy HH24:MI:SS')
I don't want to do one query for each day...
ANOTHER EDIT
I'm sorry I just got my work notebook here...here is the real sample:
BEGIN
for day in (SELECT to_char(TO_DATE (SYSDATE, 'dd/mm/yyyy')-30 + LEVEL) AS DATE_CHECK
FROM DUAL
CONNECT BY SYSDATE - 30 + LEVEL <= SYSDATE)
LOOP
v_date := to_char(day.date_check);
INSERT INTO resume (date_check, type, total)
SELECT v_data AS DATA, type, COUNT (*) total
FROM ( select ....
from table
WHERE DATE_COLUMN BETWEEN TO_DATE(v_data ||' 00:00:00', 'dd/mm/yyyy hh24:mi:ss') and TO_DATE(v_date || ' 23:59:59', 'dd/mm/yyyy hh24:mi:ss')
union
select ....
from table
WHERE DATE_COLUMN BETWEEN TO_DATE(v_data ||' 00:00:00', 'dd/mm/yyyy hh24:mi:ss') and TO_DATE(v_date || ' 23:59:59', 'dd/mm/yyyy hh24:mi:ss')
)
end loop;
end;
but it doesn't work... if I do the same insert manually replacing v_date by any date (14/02/2016) it works....
:(
If I understand what you're trying to do correctly the following might serve:
Insert into Table
select 'xpto', name
from users
where login_date between TRUNC(SYSDATE) - INTERVAL '30' DAY
and TRUNC(SYSDATE) + INTERVAL '1' DAY - INTERVAL '1' SECOND
EDIT
Based on the edit to the question it appears that we can just expand the range, as in:
Insert into Table
select 'xpto', name
from users
where login_date between TO_DATE('01/18/2016', 'MM/DD/YYYY')
and TO_DATE('02/22/2016', 'MM/DD/YYYY') + INTERVAL '1' DAY - INTERVAL '1' SECOND
SECOND EDIT
Thank you for clarifying. Perhaps the following will help:
BEGIN
for day in (SELECT TRUNC(SYSDATE)-30 + LEVEL AS DATE_CHECK
FROM DUAL
CONNECT BY TRUNC(SYSDATE) - 30 + LEVEL <= TRUNC(SYSDATE))
LOOP
INSERT INTO resume (date_check, type, total)
SELECT day.DATE_CHECK AS DATA, type, COUNT (*) total
FROM (select ....
from table
WHERE DATE_COLUMN = TRUNC(day.DATE_CHECK));
end loop;
end;
Best of luck.
Need to convert date format from (source oracle) yyyy/mm/dd to (target - SQL Server) mm/dd/yyyy.
Current Query:
SELECT* FROM WVT.WVCAS
WHERE to_date(dttmcutpull, 'yyyy/mm/dd hh24:mi:ss', 'mm/dd/yyyy hh24:mi:ss')
Getting error:
ORA-00920: invalid relational operator
Please help.
"Invalid relational operator" usually means that you have a where clause without a comparison. In your case, the where clause has the conversion to_date(), but no comparison. Perhaps you mean something like:
SELECT *
FROM WVT.WVCAS
WHERE dttmcutpull > sysdate - 1;
In other words, merely converting the data is not sufficient, you have to compare it to something.
If you just want to do the conversion, then put that in the select:
SELECT w.*,
to_char(dttmcutpull, 'mm/dd/yyyy hh24:mi:ss') as NewDate
FROM WVT.WVCAS w;
EDIT:
You have to convert each column independently, not all at once. to_char() takes two arguments, a date and a format:
SELECT w.*,
to_char(dttmcutpull, 'mm/dd/yyyy hh24:mi:ss') as date1,
to_char(DTTMPULLll, 'mm/dd/yyyy hh24:mi:ss') as date2,
to_char(DTTMRUNll, 'mm/dd/yyyy hh24:mi:ss') as date3,
to_char(SYSLOCKDATEll, 'mm/dd/yyyy hh24:mi:ss') as date4,
to_char(SYSMODATEll, 'mm/dd/yyyy hh24:mi:ss') as date5,
to_char(SYSCREATEDATE 'mm/dd/yyyy hh24:mi:ss') as NewDate
FROM WVT.WVCAS w;
SELECT w.*,
TO_CHAR(to_date(dttmcutpull, 'yyyy/mm/dd hh24:mi:ss'), 'mm/dd/yyyy hh24:mi:ss') as NewDate
FROM WVT.WVCAS w;
What I am doing is,
dttmcutpull is a STRING (VARCHAR) with format yyyy/mm/dd hh24:mi:ss
Initially converting the string to a DATE using TO_DATE
Re-converting the DATE to a string of mm/dd/yyyy hh24:mi:ss using TO_CHAR function
Used Syntax is similar to
SELECT
TO_CHAR
(
TO_DATE (STRING_FIELD_VALUE, EXISTING_FORMAT_OF_STRING_FIELD_VALUE),
EXPECTED_FORMAT_OF_NEW_VALUE
) AS NEW_STRING_FIELD_VALUE;
EDIT: Your Query should be:
If dttmcutpull, DTTMPULLll,DTTMRUNll etc are DATE datatypes then
SELECT w.*,
to_char(dttmcutpull, 'mm/dd/yyyy hh24:mi:ss') as NEW_DATE1,
to_char(DTTMPULLll, 'mm/dd/yyyy hh24:mi:ss') as NEW_DATE2,
to_char(DTTMRUNll, 'mm/dd/yyyy hh24:mi:ss') as NEW_DATE3,
to_char(SYSLOCKDATEll, 'mm/dd/yyyy hh24:mi:ss') as NEW_DATE4,
to_char(SYSMODATEll, 'mm/dd/yyyy hh24:mi:ss') as NEW_DATE5,
to_char(SYSCREATEDATE 'mm/dd/yyyy hh24:mi:ss') as NEW_DATE6
FROM WVT.WVCAS w;
If dttmcutpull, DTTMPULLll,DTTMRUNll etc are VARCHAR(STRING/CHARACTER) datatypes then
SELECT w.*,
TO_CHAR(to_date(dttmcutpull, 'yyyy/mm/dd hh24:mi:ss'), 'mm/dd/yyyy hh24:mi:ss') as NEW_DATE1,
TO_CHAR(to_date(DTTMPULLll, 'yyyy/mm/dd hh24:mi:ss'), 'mm/dd/yyyy hh24:mi:ss') as NEW_DATE2,
TO_CHAR(to_date(DTTMRUNll, 'yyyy/mm/dd hh24:mi:ss'), 'mm/dd/yyyy hh24:mi:ss') as NEW_DATE3,
TO_CHAR(to_date(SYSLOCKDATEll, 'yyyy/mm/dd hh24:mi:ss'), 'mm/dd/yyyy hh24:mi:ss') as NEW_DATE4,
TO_CHAR(to_date(SYSMODATEll, 'yyyy/mm/dd hh24:mi:ss'), 'mm/dd/yyyy hh24:mi:ss') as NEW_DATE5,
TO_CHAR(to_date(SYSCREATEDATE , 'yyyy/mm/dd hh24:mi:ss'), 'mm/dd/yyyy hh24:mi:ss') as NEW_DATE6
FROM WVT.WVCAS w;