I am working on following sql and keeping getting error on date part
Error i am getting is :- ORA-01861: literal does not match format string
SELECT COUNT(*)
FROM STGDBA.INTERACTIONS
WHERE APP_ID='Home Depot'
AND PEGA_ID ='0'
AND TO_CHAR(to_date(END_TIME))BETWEEN TO_DATE('2012-AUG-01', 'YYYY-MON-DD')AND TO_DATE
('2012-AUG-31', 'YYYY-MON-DD')
This is how date is stored in database:- 2011-10-24 08:46:31.621
First off, you should never store date or timestamp data in a table using a
VARCHAR2data type. You should always use the proper data type (date,timestamp,timestamp with time zone, etc.). Storing data in the wrong data type leads to performance problems (the optimizer’s cardinality estimates are generally going to be much less accurate and indexing the column becomes much more problematic) as well as bugs that are difficult to find when some application inadvertently stores a string with the wrong format in your table causing various queries to start throwing errors or when subtle changes in execution plans cause errors to suddenly start (or stop) appearing.Second, assuming every
end_timestring in every row in the table (whether or not the other two predicates are satisfied) is of the formatYYYY-MM-DD HH24:MI:SS.FFFYou always want to compare dates to dates and strings to strings so you don’t want to wrap the left side of the expression with a
to_char. You always want to specify an explicit format mask when you callto_date, which is what I do here. And adatedoes not have subsecond precision so you have to strip off the fractional seconds (this would not be an issue if you declaredend_timeto be atimestamp(3)).