Change the data format - oracle

I use a script in oracle which select at a moment a date :
CURSOR SAURON IS select TOUTDOUX_ID, TYPE_OF_ACTION, USER_ID, PROFILE_NAME, START_DATE, END_DATE, PLATFORM, COMMENTS, PERM_FLAG, ACTIVE_FLAG from uam.tout_doux
But the format (25-JUL-2013) is not the one I expected (2013/07/25).
How can I select the date with the right format ?

Use the Oracle TO_CHAR function with date-time format elements. In your case you want the format string YYYY/MM/DD:
CURSOR SAURON IS
select TOUTDOUX_ID, TYPE_OF_ACTION, USER_ID, PROFILE_NAME,
TO_CHAR(START_DATE, 'YYYY/MM/DD') AS SDate,
TO_CHAR(END_DATE, 'YYYY/MM/DD') AS EDate,
PLATFORM, COMMENTS, PERM_FLAG, ACTIVE_FLAG
from uam.tout_doux

Related

After running SELECT DISTINCT, duplicates still show

I am still seeing duplicates in my results after running the SELECT DISTINCT despite using all the appropriate techniques to insure duplicate rows are truly duplicate (applying trim to all fields, formatting the date fields as date only etc.). I even tried GROUP BY, yet after running it, duplicates still showed up. Does anyone have any clue what in the world is happening, and anything I can do?
SELECT DISTINCT
ID, Address_Line_1, Address_Line_2, City, State, Zip,
to_date(START_DATE, 'DD-MON-YYYY') as START_DATE,
to_date(END_DATE, 'DD-MON-YYYY') as END_DATE
FROM
AddressHistory
ORDER BY
ID, START_DATE DESC;
Try
SELECT DISTINCT
ID, Address_Line_1, Address_Line_2, City, State, Zip,
trunc(to_date(START_DATE, 'DD-MON-YYYY')) as START_DATE,
trunc(to_date(END_DATE, 'DD-MON-YYYY')) as END_DATE FROM
AddressHistory
ORDER BY ID, START_DATE DESC;
This truncates the times of the date and is faster than type cast conversion.
What data type is START_DATE in your example? Perhaps you can get rid of the type cast completely?
This is because the time factor in your column START_DATE and END_DATE is not the same, you should get distinct if you write your query like following.
SELECT DISTINCT * FROM
(
SELECT ID, Address_Line_1, Address_Line_2, City, State, Zip, to_date(START_DATE, 'DD-MON-YYYY') as START_DATE, to_date(END_DATE, 'DD-MON-YYYY') as END_DATE
FROM AddressHistory
)T
ORDER BY ID, START_DATE desc;

Convert Numeric format dates to dates in Oracle

This is how my Date column looks like
RPT_DT (Data Type is Number)
20180131
20180130
20180129
I wanna extract month out of these dates(either Month or mm), and I tried below
select extract(month from to_date(Rpt_dt))
from
(
select distinct to_char(to_date(RPT_DT,'yyyymmdd'),'mm/dd/yyyy') Rpt_dt
from TABLE_NAME
)
I am getting the error "Not a valid month"
if there is not any particular reason to have a double conversion I would suggest you to handle the problem with this simple query:
select substr(to_char(RPT_DT),5,2)from THE_TABLE
this query should be more performant since it make only one conversion. in your sample you transform:
a number to a date
then a date to a char
the char again in date
finally you extract the month
let me know if it help
r.
try this,
SELECT EXTRACT(MONTH FROM TO_DATE(rpt_dt, 'YYYYMMDD'))
FROM TABLE_NAME;
and I believe you need to modify your query as you did not put the format 'MM/DD/YYYY',
select extract(month from to_date(Rpt_dt, 'MM/DD/YYYY'))
from
(
select distinct to_char(to_date(RPT_DT,'yyyymmdd'),'mm/dd/yyyy') Rpt_dt
from TABLE_NAME
)
This back-and-forth conversion is useless. Try this
select
extract(month from to_date(RPT_DT,'yyyymmdd'))
from TABLE_NAME;

to_date function with sysdate

select TO_CHAR(to_date(sysdate, 'DD-MON-YYYY'), 'DAY') FROM DUAL;
When I run this query the output was : SUNDAY. But we know today is Tuesday(1-1-2013).
And
then changed the query as
select TO_CHAR(to_date('01-JAN-2013', 'DD-MON-YYYY'), 'DAY') FROM DUAL;
answer was :TUESDAY.
then Changed query as
select TO_CHAR(to_date(sysdate+1, 'DD-MON-YYYY'), 'DAY') FROM DUAL;
answer is :MONDAY.
When I using the sysdate why it is show SUNDAY as output?
I am new in oracle db. Please help me.
use this:
select TO_CHAR(sysdate, 'DAY') FROM DUAL;
you are using this :
to_date(sysdate, 'DD-MON-YYYY')
which is giving you date=1/1/0013 which is sunday
Please refer the documentation for sysdate here. Sysdate is already a date data type.
Your example query is inappropriate as to_date function takes first parameter as String not date.
Try the simple query below:
select TO_CHAR(sysdate, 'DAY') FROM DUAL;
This should return TUESDAY as output.
To_date is used to convert a strin to date. As sysdate is already a date, one must not add add to_date.

Inserting timestamp in oracle timestamp column

I have a table in which there is a column with datatype TIMESTAMP(0)
When I insert a date into this column using
INSERT INTO TEST_TIMESTAMP VALUES(SYSDATE)
it inserts a date in the following example format
12-SEP-12 10.31.19.000000000 AM
I want to know how the below timestamp formats can be inserted in the table
12-SEP-12 10.31.19 and 12-SEP-12 10.31.19 AM
I tried specifying some formats using TO_CHAR while inserting SYSDATE into the table, but it didn't work.
Please suggest.
when you store a TIMESTAMP it will always store the data at maximum precision (with fractional seconds).
I think what you want to do is supply a format to display the date when you retrieve it from the database.
You can do this like so:
select to_char(timestampColumnName,'DD-MON-YY HH24:MI:SS') "Date" from test_timestamp
or
select to_char(timestampColumnName,'DD-MON-YY HH:MI:SS AM') "Date" from test_timestamp
You can return it very easy like:
SELECT to_char(sysdate,'DD-MON-YY HH24:MI:SS') FROM DUAL;
In your case use:
INSERT INTO TEST_TIMESTAMP(column_name) VALUES(to_char(sysdate,'DD-MON-YY HH24:MI:SS'));
You were missing the extra ().
INSERT INTO TEST_TIMESTAMP
VALUES (TO_TIMESTAMP('12-SEP-12 10.31.19', 'DD-MON-YY HH.MI.SS'));
INSERT INTO TEST_TIMESTAMP
VALUES (TO_TIMESTAMP('12-SEP-12 10.31.19 AM', 'DD-MON-YY HH.MI.SS AM'));
TO_CHAR(SYSDATE, 'DD/MM/YYYY HH24:MI:SS')
This for colum type insert over mode to_char.

how to convert a string date to date format in oracle10g

My date value is stored as varchar2 and the value is 15/August/2009,4:30 PM, how to convert this to a proper date format like DD-MM-YYYY.
You can convert a string to a DATE using the TO_DATE function, then reformat the date as another string using TO_CHAR, i.e.:
SELECT TO_CHAR(
TO_DATE('15/August/2009,4:30 PM'
,'DD/Month/YYYY,HH:MI AM')
,'DD-MM-YYYY')
FROM DUAL;
15-08-2009
For example, if your table name is MYTABLE and the varchar2 column is MYDATESTRING:
SELECT TO_CHAR(
TO_DATE(MYDATESTRING
,'DD/Month/YYYY,HH:MI AM')
,'DD-MM-YYYY')
FROM MYTABLE;
You need to use the TO_DATE function.
SELECT TO_DATE('01/01/2004', 'MM/DD/YYYY') FROM DUAL;

Resources