to_date function strange behaviour for specific year - sql

Different behaviour ,when the date contains year 2000
select to_date(add_months(sysdate,-50),'dd/mm/yyyy') from dual --error
ORA-01858: a non-numeric character was found where a numeric was expected
-- 04/08/2000 12:59:15 contains year 2000
select to_date(sysdate,'dd/mm/yyyy') from dual --this work,but diff output

You should use TO_CHAR instead of TO_DATE.
The first parameter of TO_DATE function is a STRING parameter so here your data is converted to STRING representation and then converted to date format BUT in the DIFFERENT format:
to_char(add_months(sysdate,-50),'dd/mm/YYYY')

Your first query select to_date(add_months(sysdate,-50),'dd/mm/yyyy') from dual works fine for me
Result:
TO_DATE(ADD_MONTHS(SYSDATE,-50),'DD/MM/YYYY')
---------------------------------------------
04-OCT-09

Related

How to convert unknown variable of value 2019-12-31 into date format

Data = 2019-12-31
Don't know the data type of the data variable
Below query
Select to_date (data, 'yyyy-mm-dd') from dual
Working as follows
Select to_date (2019-12-31, 'yyyy-mm-dd') from dual;
output is
Error
Input value not long enough for date format
Don't say i haven't given single quotes .becoz i cant give single quotes to 'data' in the query
Can u give solution s assuming scenarios where the data comes without single quote or where data is number or date datatype
select to_char(to_date(20191231,'yyyymmdd'),'mm-dd-yyyy') from dual;
Convert number to date sql oracle

Convert date to month year format and pass it in the query

I have a BIRT report where user will be entering the dates in dd-mm-yyyy format however I need to convert dd-mm-yyyy to MON-YYYY format.
I have tried to use VARCHAR_FORMAT(FIELDNAME,'MON-YYYY') however it doesn't work.
select …….
where VARCHAR_FORMAT(fieldname,'MON-YYYY') = '2017-05-15';
User would end the date as
15/05/2017
The value present in the database for this field is 2017-05-15 07:30:00.0
update
Apparently the column is not a string but a datetime which means the conversion is only
to_date(fieldname, 'MON-YYYY')
But if the column is used in a Where clause it shouldn’t be converted at all.
——
Use to_date and to_char to first convert your string to a date and then back to a string with the right format
to_char(to_date(fieldname, 'DD-MM-YYYY'), 'MON-YYYY')
select *
from table (values
timestamp('2017-05-15-07.30.00')
) t(fieldname)
where
fieldname between to_date('15/05/2017', 'DD/MM/YYYY') and to_date('15/05/2017', 'DD/MM/YYYY') + 1 day
--date(fieldname) = to_date('15/05/2017', 'DD/MM/YYYY')
;
You may run it as is.
Both cases work, but to use the 2-nd one efficiently, you must create an index by the date(fieldname) expression (since db2 10.5) or add generated always column to the table with the same expression and index on it.

Date value returns random integer - oracle

select 31/10/2016 as date from dual;
Retuns me a long integer value.
Whereas,
select '31/10/2016' as date from dual;
Gives me the required result.
What is the transformation happening when we give the date value without quotes?
Non of them are right.
The 1st is an arithmetic computation.
31/10/2016 = ((31 / 10) / 2016) = 3.1 / 2016 = 0.00153...
The 2nd is a string
select '31/10/2016' + 1 from dual;
ORA-01722: invalid number
ANSI/ISO Dates literals are written as
DATE 'YYYY-MM-DD'
Datetime Literals
In your case
DATE '2016-10-31'
select DATE '2016-10-31' + 1 from dual;
2016-11-01 00:00:00
P.s.
date is a reserved word.
You couldn't have used it as an alias for your columns without getting a parsing error.
B Oracle Reserved Words, Keywords, and Namespaces
select 31/10/2016 : Its arithmetic operations ( dividation )
select '31/10/2016' : Using quotes its become string. Single quotes delimit a string constant or a date/time constant.
When you execute
select 31/10/2016 as date from dual;
Oracle interprets it as a airthmatic division operation and divides 31 by 10 and result by again 2016 .
When you execute
select '31/10/2016' as date from dual;
Oracle interprets as it as a string and it will throw error since Date is a reserved keyword and you cannot use reserve keywords either as column names or as alias.
The right way to do it as below:
select to_date('31/10/2016','dd/mm/yyyy') as dte from dual;

Converting dates stored as VARCHAR2 to a date

I have seen similar posts to this, but I am not able to resolve my query.
I am trying to query a table that has a column ("VALUE") of VARCHAR2 datatype.
The rows in this column are mixed with both numerical and date values (I do not know why the dates were stored as VARCHAR2).
I only need the dates and I have filtered off the rows with LIKE function.
SELECT
PARENTID,
NAME,
VALUE
FROM TIMINGEVENT
WHERE NAME like 'last%'
;
Now the column only has the dates and I need to convert from VARCHAR2 to date.
PARENTID ++ NAME ++ VALUE
1701480 ++ lastCycle1 ++
1701480 ++ lastCycle2 ++
1701480 ++ lastCycle3 ++ 20150901092520 AM
1701480 ++ lastCycle4 ++ 20150901092834 AM
1701480 ++ lastCycle5 ++ 20150901085047 AM
My attempts to use TO_DATE resulted in the following error:
ORA-01858: a non-numeric character was found where a numeric was expected
I am using Oracle 11g SQL Developer and the NLS preferences for date format is set to DD-MON-RR.
I found the below approach in another post, but when I use it it throws the below error?
SELECT
PARENTID,
NAME,
VALUE,
TO_CHAR(TO_DATE(VALUE, 'MM/DD/YYYY'), 'MM/DD/YYYY') AS "test"
FROM TIMINGEVENT
WHERE NAME like 'last%'
;
ORA-01843: not a valid month
01843. 00000 - "not a valid month"
It seems that you only need the right format:
with test(parentId, name, value) as (
select '1701480','lastCycle1','' from dual union all
select '1701480','lastCycle2','' from dual union all
select '1701480','lastCycle3','20150901092520 AM' from dual union all
select '1701480','lastCycle4','20150901092834 AM' from dual union all
select '1701480','lastCycle5','20150901085047 AM' from dual
)
select to_date(value, 'YYYYMMDDHHMISS AM')
from test
When you use a format specifier inside to_date(), what it does is to try to map the value in the column exactly in the format that has been as format specifier, not something less, not something more.
So, When you use TO_DATE(VALUE, 'MM/DD/YYYY') it tries to map the first 2 characters ie, 20 as MM. Hence it is giving the error as not a valid month.
You need a proper format specifier to deal with the column, as one is shown below -
SELECT TO_DATE(VALUE, 'yyyymmddhhmiss am') FROM TIMINGEVENT
It will give output like -
9/1/2015 9:25:20 AM
Later you can again format the output of this as per your requirement by using to_char and again using a proper format specifier.
SELECT to_char(TO_DATE(VALUE, 'yyyymmddhhmiss am'),'DD-MON-YYYY HH12:MI:SS AM') FROM TIMINGEVENT
This will give an output like -
01-SEP-2015 09:25:20 AM
Please note, it does not matter is your value contains am or pm in it, you can use both am or pm. I mean, ironically it is not mandatory to use 'am' if the value contains 'am' in it. You can use 'pm' too even if the value contains 'am'. Not that it makes much sense in using it that way, still just an FYI.
First understand the usage of to_date and to_char functions:
TO_CHAR:
To change other datatypes like date or number to string, if you are changing a date to character, then you must specify the date format to which it should be converted.
TO_DATE
To change string/char to date, the second parameter is date format of your string in your case, the date format of the string "20150901092520 AM" is "YYYYMMDDHHMISS AM", so you have use it as
to_date('20150901092520 AM', 'YYYYMMDDHHMISS AM'), this will convert it to date object, now to print it required format as "09/01/2015" use to_char and specify the format as "MM/DD/YYYY" like below
to_char(to_date('20150901092520 AM', 'YYYYMMDDHHMISS AM'),'MM/DD/YYYY')
You should be able to use the ISDATE function to test the string before converting
https://docs.oracle.com/cd/B28359_01/olap.111/b28126/dml_functions_1106.htm
Generally...
SELECT cust_last_name,
CASE value WHEN isdate(value) = 'Yes' THEN convert(date,value)
WHEN isdate(value) <> 'Yes' THEN ''
END
FROM TABLE;

Comparing date with sysdate in oracle

I have a column which is of "DATE" type and I want to run a query on it comparing it with sysdate.
But I am getting following error, Can someone please let me know what I am missing here?
SQL> select distinct file_name as r
from table_1
where view_day >= TO_DATE(SYSDATE-10, 'YYYY/MM/DD');
ERROR at line 1:
ORA-01858: a non-numeric character was found where a numeric was expected
You shouldn't use to_date on a date, To_date is for casting a varchar to date, not a date.
If you do use the function to_date on a date, then oracle will refer to it as a string according to nls_date_format which may vary in different environments.
As #jonearles said, if you want to remove the time in sysdate then use TRUNC
USE:
select distinct file_name as r
from table_1
where view_day >= TRUNC(SYSDATE-10)
Error shows that a VIEW_DAY column is varchar so you need to convert DATE to String. Use TO_CHAR or convert VIEW_DAY to date type.