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')
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 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;
I have a column LOGIN_DATETIME from LOGIN table
The column is varchar2 has values in two format
20-11-2018 01:00:00
20-07-2018 14:00
I need to derive a varchar value from this column with the following format
dd-mmm-yyyy hh:mi am/pm
Result expected
20-Nov-2018 01:00 AM
20-Jul-2018 02:00 PM
I tried like
Format 1:
SELECT TO_CHAR(TO_DATE(LOGIN_DATETIME, 'DD-mm-yyyy hh:mi:ss'), 'dd-mmm-yyyy hh:mi AM')
FROM LOGIN
WHERE LOGIN_DATETIME is not null;
Format 2:
SELECT TO_CHAR(TO_DATE(LOGIN_DATETIME, 'DD-mm-yyyy hh:mi'), 'dd-mmm-yyyy hh:mi AM')
FROM LOGIN
WHERE LOGIN_DATETIME is not null;
It's not working.
You could use this only one query:
SELECT
CASE
WHEN LENGTH(login_datetime) = 19 THEN TO_CHAR(TO_DATE(login_datetime, 'DD-MM-YYYY HH24:MI:SS'), 'DD-Mon-YYYY HH:MI AM')
WHEN LENGTH(login_datetime) = 16 THEN TO_CHAR(TO_DATE(login_datetime, 'DD-MM-YYYY HH24:MI'), 'DD-Mon-YYYY HH:MI AM')
END AS login_datetime
FROM login
ORDER BY 1;
Tested ok in rextester
Use case and select format based on field length
SELECT
CASE WHEN LENGTH(LOGIN_DATETIME) = 16 THEN TO_CHAR(TO_DATE(LOGIN_DATETIME, 'dd-mm-yyyy hh24:mi'), 'dd-mm-yyyy HH:mi AM')
ELSE TO_CHAR(TO_DATE(LOGIN_DATETIME, 'dd-mm-yyyy hh:mi:ss'), 'dd-mm-yyyy HH:mi AM')
END
FROM LOGIN
WHERE LOGIN_DATETIME is not null;
That was easy one I dunno the syntax of SQL it was mon not mmm in date format
SELECT
TO_char(to_timestamp (LOGIN_DATETIME, 'DD-MM-RRRR HH24:MI:SS.FF'), 'DD-Mon-YYYY HH12:mi AM')
from LOGIN
WHERE LOGIN_DATETIME is NOT null;
OR
SELECT
TO_char(to_timestamp (LOGIN_DATETIME, 'DD-MM-YYYY HH24:MI:SS.FF'), 'DD-Mon-YYYY HH12:mi AM')
from LOGIN
WHERE LOGIN_DATETIME is NOT null;
The both formats work in one query itself.
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.
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;