I am getting the error: ORA-01861: literal does not match format string
I was trying to create a select statement where I can find a the entity_id and name before march 3rd of 2013 and have a status report = W.
SELECT ENTITY_ID,NAME FROM STATUS_TABLE
WHERE DATERETURN <= '2013-03-01'
AND REPORT_STATUS LIKE 'W';
change this:
WHERE DATERETURN <= '2013-03-01'
to this:
WHERE DATERETURN <= to_date('2013-03-01', 'yyyy-mm-dd')
Oracle wont understand literal date format strings. You should to use the TO_DATE function:
WHERE DATERETURN <= TO_DATE('2013-03-01 00:00:00')
or the ANSI syntax
WHERE DATERETURN <= DATE'2014-02-05'
Related
Just a general date filter question, what are the valid operators for me to filter a specific timeframe as I had this error:
Cannot apply operator: date <= varchar(8)
code:
where utc_date >= '2022-04-01'
data type = date
'2022-04-01' is a varchar literal, you need to convert it to date for example by prefixing it with date:
where utc_date >= date '2022-04-01'
I have table in Oracle SQL Developer like below:
col1
--------
2019-11-29 11:14:00.821822
2020-02-11 09:14:00.821847
And I would like to select only rows where date is '2019-11-29' how can I do that?
You can use:
where trunc(col1) = date '2019-11-29'
However, that cannot use an index on col1. So, it is often better to use:
where col1 >= date '2019-11-29' and
col1 < date '2019-11-30'
You can to_date() or to_char() functions. The Oracle/PLSQL TO_DATE function converts a string to a date and TO_CHAR function converts a date to a String. The TRUNC(date) function returns date without the time portion.
select *
from myTable
where trunc(col1) = to_date('2019-11-29', 'yyyy-mm-dd');
or:
select *
from myTable
where to_char(col1, 'yyyy-mm-dd') = '2019-11-29'
I've to query a Oracle DB in order to get data from a table according to the day of the week.
If it's Monday, I need to query FL_PO_LUN, if it's Tuesday, my query will be on FL_PO_MAR and so on.
This is the query I'm trying to use.
SELECT DISTINCT(DEPCOD),ENTECOD FROM RIO_PLA_DEP WHERE
DT_INIZIO_VAL <= trunc(sysdate) AND
DT_FINE_VAL >= trunc(sysdate) AND
(SELECT DECODE(
TO_CHAR(SYSDATE,'d'),
'1','FL_PO_LUN',
'2','FL_PO_MAR',
'3','FL_PO_MER',
'4','FL_PO_GIO',
'5','FL_PO_VEN',
'6','FL_PO_SAB',
'7','FL_PO_DOM') AS DAY_OF_WEEK
FROM DUAL) = 1;
Decode statement works fine, but the whole statement won't work.
Actually i get a ORA-01722 error.
Expected result would be:
SELECT DISTINCT(DEPCOD),ENTECOD FROM RIO_PLA_DEP WHERE
DT_INIZIO_VAL <= trunc(sysdate) AND
DT_FINE_VAL >= trunc(sysdate) AND
FL_PO_LUN = 1;
Drop quotes around column names. Currently you are trying to compare char to int type. This is impossible as 'FL_PO_LUN' and other char constants do not represent int.
SELECT DISTINCT(DEPCOD),ENTECOD
FROM RIO_PLA_DEP
WHERE
DT_INIZIO_VAL <= trunc(sysdate) AND
DT_FINE_VAL >= trunc(sysdate) AND
(SELECT DECODE(
TO_CHAR(SYSDATE,'d'),
'1',FL_PO_LUN,
'2',FL_PO_MAR,
'3',FL_PO_MER,
'4',FL_PO_GIO,
'5',FL_PO_VEN,
'6',FL_PO_SAB,
'7',FL_PO_DOM) AS DAY_OF_WEEK
FROM DUAL) = 1;
I'm having trouble with my WHERE clause when trying to retrieve records for a certain month.
WHERE NVL(TO_CHAR(prsl.DEADLINE, 'YYYY-MM-DD'), TO_CHAR(prsl.SUBMIT_DATE, 'YYYY-MM-DD')) >= DATE '2015-11-01'
AND NVL(TO_CHAR(prsl.DEADLINE, 'YYYY-MM-DD'), TO_CHAR(prsl.SUBMIT_DATE, 'YYYY-MM-DD')) < DATE '2015-12-01'
Basically what this does is if DEADLINE is not null, use that to compare to the date range. If it is null, use the SUBMIT_DATE for comparison instead.
Using the above query, I get this error:
ORA-01861: literal does not match format string
You can try with EXTRACT:
EXTRACT(MONTH FROM prsl.DEADLINE) = 11
AND EXTRACT(YEAR FROM prsl.DEADLINE) = 2015
Another solution would be this:
WHERE TRUNC(COALESCE(prsl.DEADLINE, prsl.SUBMIT_DATE), 'MM') = DATE '2015-11-01'
I have the following values in my table
Select * From TimeSheetLogs where InTimeStamp <= '1/22/2013'
when i execute the above query i get null value
but as you can see i have 3 data with datefield as 1/22/2013
Then what i am doing wrong?
Thanks
that is correct because
'1/22/2013 19:21' > '1/22/2013 00:00'
You probably need to truncate your dates first to remove the time portion. Then convert both sides to date datatype. The string '1/22/2013' is a string, not a date. By looking at data your InTimeStamp is timestamp datatype. You cannot compare date or timestamp to character w/out converting the char-s to date. I'm not sure what Database are you using. This is how you'd convert in Oracle using to_date function .
SELECT in_date, compare_date
FROM
(-- This is your InTimeStamp on the fly
SELECT trunc(to_timestamp('2013-01-22 16:21:19.273', 'yyyy-mm-dd hh24:mi:ss.ff')) in_date -- this is your InTimeStamp
, to_date('1/22/2013', 'mm/dd/yyyy') compare_date
FROM dual
)
WHERE in_date <= compare_date
/
Now you can compare two dates below - this is the output of above query:
in_date compare_date
---------- ------------
1/22/2013 1/22/2013
Select * From TimeSheetLogs
where InTimeStamp <= select convert(datetime,'1/22/2013',101)
As per my comment to #Zdravko's good answer, you just need to cast your date:
Select * From TimeSheetLogs where CAST(InTimeStamp as Date) <= '1/22/2013'