Error when using TO_DATE in SQL - sql

I understand the basic TO_DATE usage in Oracle SQL. i google and found some guides to use TO_DATE which is to convert julien date to normal date.
the basic working code is :
SELECT TO_CHAR((TO_DATE(2016365, 'YYYYDDD'))) FROM DUAL)
However, i want to convert date that is in a column which has thousands of them.
What i did was :
SELECT TO_CHAR((TO_DATE(PREVDT, 'YYYYDDD'))) FROM DUAL)
The changes is PREVDT because all my julian date is in PREVDT column. However, im getting invalid identifier.....can anyone help me?
I also tried this but no luck:
TO_CHAR((TO_DATE(PREVDT, 'YYYYDDD')))

You need to provide the table name in which your column PREVDT is present.
select TO_CHAR((TO_DATE(PREVDT, 'YYYYDDD'))) FROM yourTable

Oracle DUAL table does not have the column PREVDT. What you want to do is run the query against your table, the one with column PREVDT.
SELECT TO_CHAR((TO_DATE(PREVDT, 'YYYYDDD'))) FROM your_table_name;
The PREVDT values should be in the specified date format otherwise you will get an error.
Read more about the DUAL table here.

Related

Trouble with convert integer to date+time in Oracle

i have in table one column in integer. This integer looks like:
2204010044 - it is YYMMDDHH24MI.
I want this column in sql query convert to date. But when i try, i get error:
ORA-01840.
I testing this: TO_DATE("mycolumn",'yymmddhh24mi')
I've tried multiple options, but always to no avail.
NLS_DATE_FORMAT is for database: DD.MM.YY (i dont know,if its relevant)
Maybe with something like this:
select to_timestamp(cast(2204010044 as varchar(10)),'YYMMDDHH24MI')
from dual
If you want to keep the time, you must cast to timestamp, not date.
If you want just the date, use:
select to_date(2204010044,'YYMMDDHH24MI')
from dual
You can test on this db<>fiddle
Try the following SQL query:
select TO_DATE(2204010044,'yymmdd hh24:MI:ss')
FROM dual
dbfiddle

bad date format for pivot

together
I can not figure out why in PowerPivot or Excel a Microsoft query does not provide the date fields in the format for Excel as they are necessary. I have already gotten from you in another thread the approach, as in Microsoft Query the fields are output directly as a date.
I use the following query to put the records into Excel as a table:
select to_char(DB_Gen.STRT, 'DD.MM.YYYY') "Date" FROM XXX
So I get the data in the format "DD.MM.YYYY".
So I connected this date column to the automatic calendar table in PowerPivot, but somehow the link does not work. The pivot table can not work with it.
Where exactly is the error? Why is not the field accepted in date format?
How can I find out the error?
Edit:
Somehow, the table created from the query does not get the date format. Although the data is displayed as "DD.MM.YYYY", the column does not have the date format. How can this be changed?
Best Regards
Joshua
Check if the column DB_Gen.STRT is of date type, if so just fetch the data as below
select DB_Gen.STRT "Date" FROM XXX
If the column data type is char, then use TO_DATE to convert it to date type
select TO_DATE(TO_CHAR(DB_Gen.STRT, 'MM/DD/YYYY'), 'MM/DD/YYYY') "Date" FROM XXX

String to Date SQL

I have a Comments (Clob) field which is a collection of the time the comment was made, comment made by and the comment itself.
I need to extract the 'Date', 'Comment by' from the field.
Using Regular expressions, I was able to extract the date fields and comment_by field but the date is in character.
I'm not able to convert this field into Date.
Comments Example:
7/18/2018 10:36:29 AM, beginj requesting cancellation as steps are no longer viable.
5/16/2018 8:28:04 AM, josephav Not applicable for GSC
I can get the 'date' using this regular expression (RE):
regexp_substr((dbms_lob.substr(requestcommentsdisplay,50,1)),'[^ ]+',1)
I can get the Comment_by name using this regular expression (RE)
regexp_substr((dbms_lob.substr(requestcommentsdisplay,50,1)),'[a-z]+',1)
So the dates after I extract using RE are like below:
10/5/2017
4/2/2012
12/31/2015
3/16/2014
I need to convert these into Proper dates so that I can use these further in my code
Any Help is appreciated :)
Thanks
Use
SELECT
CASE WHEN (length(<regex_string>).
<9)
Then
TO_DATE('0'||<regex_string>,'MM/DD/YYYY')
but oracle parses it with single digit as well though you can
try to debug and print dates for single digit via this
condition.
ELSE
TO_DATE(<regex_string>,'MM/DD/YYYY')
END CASE FROM TABLE;
to convert the string to date format as the o/p you got from regex_substr is a string.
This will allow you to read the input in the format you are receiving and will convert to actual oracles date format.
You can set nls_territory parameter depending on your country, before calling the query
alter session set nls_territory = 'AMERICA';--alternatively convert to 'TURKEY' as an example
and then try the following
with tab(date_chr) as
(
select '10/5/2017' from dual union all
select '4/2/2012' from dual union all
select '12/31/2015' from dual union all
select '3/16/2014' from dual
)
select to_date(date_chr,'MM/DD/YYYY')
from tab;

Oracle sql not grabbing day for the current date

I am trying to get the day of the week for the asofdate field. I am receiving an error using Oracle SQL that states: 'a non numeric character was found where a numeric character was expected.' That error is for this query:
select to_char(to_date(max(distinct(asofdate)), 'mm/dd/yyyy'), 'DY') from PS_Z_EXS251AE_EMP
the below query returns '1/6/2015'
select max(distinct(asofdate)) from PS_Z_EXS251AE_EMP
Does anyone know what I'm doing wrong?
If asofdate is stored as a date/time data type, then why are you converting it to a date. Also, why are you using max():
select to_char(asofdate, 'DY')
If asofdate is stored as a string, then you should probably fix the data. Why are you storing a date as a string. That is the wrong type.
If you want the day of the week of the maximum date, which your question suggests, then just do:
select to_char(max(asofdate), 'DY')
You can simply do
select distinct max(extract(day from asofdate)) from from PS_Z_EXS251AE_EMP;

SQL query to convert Date to another format

Thanks for your help. I am not able to make out the type/format of the "Value" in a Date column.I guess its in Julian Date format.
The Column is paid_month and the values are below.
200901
200902
So,please help in writing SQL query to convert the above values(Mostly in Julian Format) in the Date Column to normal date (MM/DD/YYYY) .
Thanks
Rohit
Hi,
I am sorry for missing in giving the whole information.
1)Its a Oracle Database.
2)The column given is Paid_Month with values 200901,200902
3)I am also confused that the above value gives month & year.Day isnt given if my guess is right.
4)If its not in Julian format ,then also please help me the SQL to get at least mm/yyyy
I am using a Oracle DB and running the query
THANKS i GOT THE ANSWER.
**Now,i have to do the reverse meaning converting a date 01/09/2010 to a String which has 6 digits.
Pls help with syntax-
select to_char(01/01/2010,**
It looks like YYYYMM - depending on your database variant, try STR_TO_DATE(paid_month, 'YYYYMM'), then format that.
Note: MM/DD/YYYY is not "normal" format - only Americans use it. The rest of the world uses DD/MM/YYYY
For MySQL check
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
Example:
SELECT DATE_FORMAT(NOW(), '%d/%m/%Y')
For MySQL, you would use the STR_TO_DATE function, see http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date
SELECT STR_TO_DATE(paid_month,'%Y%m');
Sounds like the column contains some normal dates and some YYYYMM dates. If the goal is to update the entire column, you can attempt to isolate the YYYYMM dates and update only those. Something like:
UPDATE YourTable
SET paid_month = DATE_FORMAT(STR_TO_DATE(paid_month, '%Y%m'), '%m/%d/%Y')
WHERE LENGTH(paid_month) = 6
SELECT (paid_month % 100) + "/01/" + (paid_month/100) AS paid_day
FROM tbl;
I'm not sure about how oracle concatenates strings. Often, you see || in SQL:
SELECT foo || bar FROM ...
or functions:
SELECT cat (foo, bar) FROM ...