Comparing date with sysdate in oracle - sql

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.

Related

Getting a date format error while executing

ORA-01840: input value not long enough for date format
01840. 00000 - "input value not long enough for date format"
*Cause:
*Action:
SELECT TO_DATE (
TO_CHAR (TO_DATE (attribute39, 'MM/DD/YYYY'), 'DD/MM/YYYY'),
'DD/MM/YYYY') AS "PO Valid To Date"
FROM table;
Want to execute the query without error,
attribute39 is date formate in mm/dd/yyyy and varchar(250)
TO_DATE supports 'DEFAULT return_value ON CONVERSION ERROR' and if you prefix the date format with 'FX' you will be able to detect rows where attribute39 is not exactly compliant to your expectation:
TO_DATE (attribute39, DEFAULT to_date('01/01/0001','dd/mm/yyyy') ON CONVERSION ERROR, 'FXMM/DD/YYYY')
You could put NULL as DEFAULT if you don't have it as possible value for attribute39, if not selecting a value you are sure is not in your data makes easier to detect rows with invalid attribute39.
You may get ORA-01840 if you have strings where the year is only two digits (meaning from 1950 to 2049).
You could also run a query with a regex to detect unexpected values in attribute39.
select distinct(col1)
from Customer_flex_attr_value
where regexp_like (col1, '([0-9][0-9]|3[0-1])/([0-9]|[0-9]{2})/[0-9]{4}')
order by 1 desc;
This would bring different date format

To get all the dates in a format dd/mm/yyyy. I have a date field in the format yyyymmdd

In my DB, there is a date field in the format yyyymmdd.
I have to get all the dates in the format dd-mm-yyyy for that particlar date.
ex:
Date
20170130
20170228
20170325
for the above dates, I need the output in the below format with the dates and day of the particular dates
date day
30-01-2017 tuesday
28-02-2017 tuesday
25-03-2017 saturday
If the column is a string, then it can hold invalid date values such as February 31, one way to avoid this is by a small function such as this:
create or replace
function my_to_date( p_str in varchar2 ) return date
is
begin
return to_date( p_str );
exception
when others then
return null;
end;
\\
select to_char(my_to_date('20170231'),'DD-MM-YYYY Day')
from dual
\\
Demo
Try below:
Select to_char(yrdate, 'dd-mm-yyyy'), to_char(yrdate, 'D') from yrtable
It sounds like your dates aren't actually DATE fields but some kind of CHAR field? The best option would be to convert to DATE and then convert back to CHAR:
SELECT TO_CHAR(TO_DATE(mydate, 'YYYYMMDD'), 'DD-MM-YYYY Day')
FROM mytable;
This uses the YYYYMMDD mask to convert your string into a date, then uses the mask DD-MM-YYYY Day to convert it back into a string. Use day if you want the day name in lowercase (as in your OP).
#user2778168 answer will give you the results you want. But why?
Your database does not have dates stored in yyyymmdd format or any other date format for at mater, unless it's defined with a character type definition. Oracle stores all dates in a single internal structure, and with only slight variations timestamps are the same. The format used only tells Oracle how to display the value or to convert a string to a date. Unless a specific format is specified Oracle uses the NLS_DATE_FORMAT for this determination. See here and scan down to "Datetime Format Models" for format specifications.
To see this run the following:
select value
from nls_session_parameters
where parameter = 'NLS_DATE_FORMAT';
Select yrdate default_format
, to_char(yrdate, 'dd-mm-yyyy') specified_format
, dump(yrdate) unformated
from yrtable;
alter session set nls_date_format = 'Month dd,yyyy';
Rerun the above queries.
It seems you hold date column(date1) in character format. Suppose you have a table named days:
SQL> desc days
date1 varchar2(10)
then,
we should convert it into date, and then into char format, with aliases in quotation marks to provide lowercase aliases as you wanted.
perhaps your database's date language is non-english like mine(turkish), then you need to convert it to english.
lastly, it'a appropriate to order the results according to converted date, seen from your output. So we can use the following SQL :
select to_char(to_date(date1,'yyyymmdd'),'dd-mm-yyyy') "date",
to_char(to_date(date1,'yyyymmdd'),'day','nls_date_language=english') "day"
from days
order by to_date(date1,'yyyymmdd');
D e m o

Date format error

Select
(*)
From
MISC
WHERE
TO_DATE(TRANSACTION_TIME, YYYY-MM-DD HH24:MI:SS.FF6) Between
TO_DATE(:Variable1, 'YYYYMMDD') and TO_DATE(:Variable2, 'YYYYMMDD')
The transaction time column is CHAR(78) and returns 2017-06-12-12.35.37.444978 as and example. I have been struggling with this for awhile now and getting a variety of errors. With the current setup, I receive
Error Executing Query: ORA-01821: date format not recognized.
Variable1 = 20170612
Variable2 = 20170615
That's a timestamp not a date so you need to use to_timestamp() not to_date()
If you are only looking at the date, then perhaps a simpler version will work:
WHERE TO_DATE(SUBSTR(TRANSACTION_TIME, 10), 'YYYY-MM-DD') Between
TO_DATE(:Variable1, 'YYYYMMDD') and TO_DATE(:Variable2, 'YYYYMMDD')
Also, why not pass in the variables as dates rather than strings? That would get around the second conversion. If they are actually dates, then that might be where the conversion problem is.

Converting string to date in sql

I need to convert 2014-11-18T14:08:43+00:00 which is in varchar in my sql developer to date format in order to filter a few entries.
I tried
to_date(LAST_UPDATE_DATE,'YYYY-MM-DD')
but it gives an error
ORA-01830: date format picture ends before converting entire input
string.
Kindly help..
Just in case you didn't mean to put up sql server but instead you need to use oracle (seeing as you are using to_date and you are getting an ora exception)
I added a quick datetime conversion for date and timestamp (no milliseconds) for your date format:
SELECT to_Date(concat
(substr
(myvar,0,10),
concat(' ',
substr(myvar,12,8)
)
),'YYYY-MM-DD HH24:mi:ss') AS mydate
FROM mytable
Fiddle
declare #varDate as nvarchar(50) = '2014-11-18T14:08:43+00:00'
select CAST(substring(#varDate,0,CHARINDEX('T',#varDate)) as date)
Either you can use it like this
declare #Date as nvarchar(100) = '2014-11-18T14:08:43+00:00'
SELECT CONVERT(DATE,#Date) AS Date
OR go for this answer which is accepted in this question
ORA-01830: date format picture ends before converting entire input string / Select sum where date query
ORA-01830: date format picture ends before converting entire input string.
to_date(LAST_UPDATE_DATE,'YYYY-MM-DD')
2014-11-18T14:08:43+00:00 is TIMESTAMP and not DATE.
First of all, you should never ever store DATE/TIMSTAMP as string. It is a database design flaw.
Anyway, you could convert it to TIMESTAMP WITH TIMEZONE.
For example,
SQL> SELECT to_timestamp_tz('2014-11-18T14:08:43+00:00',
2 'YYYY-MM-DD"T"HH24:MI:SS.FFTZH:TZM')
3 AS tm_stamp
4 FROM dual;
TM_STAMP
----------------------------------------------------------------
18-NOV-14 02.08.43.000000000 PM +00:00
SQL>
You could try this;
Select CAST ('2014-11-18T14:08:43+00:00' as date)
The assumption is you are in SQL Server 2012

Formatting date sql

I want to format existing dates in a column called mydate in a table called empl. However, I am going no where with this. I have tried
SELECT CONVERT(VARCHAR(10), mydate,103) from empl
but get a missing expression error.
I want to change the current format in the column which is in the format
dd-mm-yy to mm/dd/yy
EDIT: im using oracle
Any suggestions?
If the datatype of the column is DATE, you can use the TO_CHAR() function:
SELECT to_char(mydate, 'mm/dd/yy')
FROM empl ;
Are you able to change the column type to date? Then you could use date_format
The Convert statement you show is for MS-SQL, and you would want code 3 not 103, the 100 series give 4 digit years.
Try:
select to_char(mydate,'MM/DD/YY') from empl
Assuming mydate is stored in date datatype, and using oracle, easy way to do that is:
select to_char(mydate, 'dd/mm/yy') from empl
In Oracle, convert is used to change character set with parameters
(char, destination_char_set, source_char_set)
select TO_CHAR(sysdate, 'MM/DD/YYYY') CHRDATE from dual;