Usage of to_date & to_char
pls. give advanced usage of to_date & to_char
pls. give advanced usage of to_date & to_charTO_DATE
http://download-uk.oracle.com/docs/cd/B10501_01/server.920/a96540/functions137a.htm
TO_CHAR
http://download-uk.oracle.com/docs/cd/B10501_01/server.920/a96540/functions134a.htm#1009326
http://download-uk.oracle.com/docs/cd/B10501_01/server.920/a96540/functions133a.htm#1003490
http://download-uk.oracle.com/docs/cd/B10501_01/server.920/a96540/functions135a.htm#79332
Similar Messages
-
What is the difference between storing sysdate or to_date(to_char(sysdate, 'DD Mon YYYY HH:MI AM'),'DD Mon YYYY HH:MI AM') in a date data type column?
When I column value is set with sysdate the following query gives no records
select *
from Test
WHERE Updatedon = to_date('22 Sep 2012 07:55 PM','DD Mon YYYY HH:MI AM');
when I run the same query with out where clause I am getting the records with that time or storing with to_date(to_char(sysdate, 'DD Mon YYYY HH:MI AM'),'DD Mon YYYY HH:MI AM') gives records.
Can someone explain the difference please?Hi,
Welcome to the forum!
user8765044 wrote:
What is the difference between storing sysdate or to_date(to_char(sysdate, 'DD Mon YYYY HH:MI AM'),'DD Mon YYYY HH:MI AM') in a date data type column?Remember that all DATEs include hours, minutes and seconds , as well as year, month and day.
to_date(to_char(sysdate, 'DD Mon YYYY HH:MI AM'),'DD Mon YYYY HH:MI AM')is equivalent to
TRUNC (SYSDATE, 'MI')To see why, consider what
to_char(sysdate, 'DD Mon YYYY HH:MI AM'),does. It creates a string out of the year, month, day, hours and minutes of SYSDATE, but it ignores the seconds. If you convert that string into a DATE, the seconds will default to 00.
When I column value is set with sysdate the following query gives no records
select *
from Test
WHERE Updatedon = to_date('22 Sep 2012 07:55 PM','DD Mon YYYY HH:MI AM');
when I run the same query with out where clause I am getting the records with that time or storing with to_date(to_char(sysdate, 'DD Mon YYYY HH:MI AM'),'DD Mon YYYY HH:MI AM') gives records.Whenever you get confusing results with DATEs, display the full DATE, including hours, minutes and seconds.
To change the default DATE format in your session, so that the hours, minutes and seconds appear automatically, use ALTER SESSION. For example:
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-Mon-YYYY HH:MI:SS AM'; -
What is the use of To_date, to_char functions?
what is the use of To_date, to_char functions?
while i am using these in my day to day activities but never able to know the exact working of tehse.
Message was edited by:
437022Dates are stored and manipulated as coded (binary) values that always contain "century, year, month, date, hour, minute second"
We don't want to see the date as stored. We want to see the characters (digits) that make up the date. So we use to_char(the-date-value)
We want to tell the system a date, but have a heck of a time entering the binary. So we input the characters (digits) that make up the date and tell the system to convert that to a binary 'date' using to_date(our-string-that-respresents-dates)
The magic Oracle has added is the format codes (see chapter 2 of the SQL Reference manual for the complete list). We can get the system to print out dates and times, including day of week (and so on) using format codes.
The default format is to show us the date but not the time. Which leads a lot of people to believe that the time is not stored. -
Hi all,
I am working with a Oracle 9i R2 database
SQL>select TO_CHAR(sysdate, 'MM/DD/YYYY') CHRDATE from dual;
CHRDATE
11/14/2005
SQL>select TO_DATE(TO_CHAR(sysdate, 'MM/DD/YYYY'), 'MM/DD/YYYY') mydate from dual;
MYDATE
14-NOV-05
I want to retain the 4 digit year. Please suggest what I am doing incorrect.
ThanksYour select statement,SQL>select TO_DATE(TO_CHAR(sysdate,...is equivalent toselect sysdate...And whenever you select a date, SQL Plus has to convert it to a character format before it can display it on the SQL Plus output screen.
The four-digit year IS being retained internally ...until you display it on the screen. If you just set the default date format for displaying dates to include the four-digit year, you will see the full year:
SQL> select SYSDATE mydate from dual;
MYDATE
14-NOV-05
SQL> alter session set nls_date_format = 'MM/DD/YYYY';
SQL> select SYSDATE mydate from dual;
MYDATE
11/14/2005
SQL> select TO_DATE(TO_CHAR(sysdate, 'MM/DD/YYYY'), 'MM/DD/YYYY') mydate from dual;
MYDATE
11/14/2005 -
Hi
I am using this conversion to_date(to_char(sysdate,'dd/mm/yyyy hh24:mm'),'dd/mm/yyyy hh24:mm')
but it says format string appears twice
Could some one please help me with this?user11365275 wrote:
I have a requirement for taking hh:mi from a date ..ie i need to compare two dates till hrs and mins level but not secs..so i was trying to take the date till hrs,mins and then convert to dates and compare them...Exactly this is my requirement
to_date(TO_CHAR(LOAD_DATE,'DD/MM/YYYY HH24:Mi'),'DD/MM/YYYY HH24:Mi')
<(SELECT to_date(TO_CHAR(CURRLOADTIME,'DD/MM/YYYY HH24:Mi'),'DD/MM/YYYY HH24:Mi')
FROM Table1 WHERE
JOBNAME='DEFAULT_EVENT_LOG');
PLease can you suggest anything better than this comparison or let me know if this works correct nowYou are using a TO_CHAR to format a date field in a string. Then use a TO_DATE to change it back to a date. This "double" formatting is a deep misunderstanding about what is a date field.
If you want to compare dates and get rid of the seconds, use truncate, for instance :
SQL> select trunc(sysdate,'mi'),sysdate from dual;
TRUNC(SYSDATE,'MI') SYSDATE
02/02/2010 09:18:00 02/02/2010 09:18:55Your WHERE clause will become :
...trunc(LOAD_DATE,'mi')<(SELECT trunc(CURRLOADTIME,'mi')...Nicolas. -
To_date,to_char function behavior
I have a table that contains starting dates of employment for employees. Since the table could have several starting dates (This is normal in our institution), I wrote a pl/sql loop to count the number of employees whose ONLY starting date (meaning they have only one starting date) lies in the year 2008. Now I faced the following problems when running the code in a when-button-pressed trigger in forms:
1. the counter in the statement
if to_char(docexecdt,'dd-mm-yyyy) between '1-1-2008' and '31-12-2008' then x:=x+1
returned an abnormal number (too large for the size of the table)
(where docexecdt is the field containing the starting date)
is using to_char here incorrect?
2. the statement
if to_date(docexecdt) between '1-1-2008' and '31-12-2008' then x:=x+1
returned an error "invalid month"
although I tried several date formats.
given that I tried running the same to_date function on the table in sql*plus with no error returned.
What is the correct format mask for the to_date function?-> Why does the to_char(docexecdt,'dd-mm-yyyy') return an incorrect result?
Because putting dd (the day of the month) first puts EVERY day between your dates:
01-01-2008 is LESS than ALL the following dates:
01-02-1982
12-01-2000
15-08-1998
30-01-2009
30-12-2020
31-12-2008 is GREATER than ALL the above dates.
NEVER compare dates by placing DAY or MONTH before the year!!!!
When you compare using DDMMYYYY format, the only dates less than 01-01-2008 are January 1 of any prior year. The only dates greater than 31-12-2008 are 31-December of any year greater than 2008.
The correct way to find your count is given by Paul M, where you ONLY look at the year. -
Given query giving an error -invalid month????
select to_date(add_months(trunc(sysdate),-5) ||' 10:30:00 AM','mm/dd/yyyy hh:mi:ss am') from dual
giving an error-invalid month..1. Instead of comma you have put dot.
2. Below is teh correct usage
select to_date(to_char(add_months(trunc(sysdate),-5),'DD/MM/YYYY')||' 10:30:00 AM','DD/MM/YYYY HH:MI:SS AM')
from dual3. But, why are you going for this when there is a straight forward way? -
How to avoid use of to_date and to_char ?
Hi All,
I have table 'sample ' with columns id (number ) and dte (timestamp ) .
CREATE TABLE SAMPLE ( ID NUMBER,DTE TIMESTAMP);
Sample Records like
INSERT INTO SAMPLE VALUES( 1,SYSDATE);
INSERT INTO SAMPLE VALUES( 2,SYSDATE);
INSERT INTO SAMPLE VALUES( 3,SYSDATE+1);
INSERT INTO SAMPLE VALUES( 4,SYSDATE+1);
INSERT INTO SAMPLE VALUES( 5,SYSDATE+3);
After inserting the records the dte will have both date and time.....
My Question is, How to fetch the records for the given date ( not time ) without using to_date,to_char or trunc functions from the table .
For Example,
select * from sample where dte = '22-OCT-2008' ;
Note : '22-OCT-2008' is not exactly a string ,use it as date datatype.
Regards,
Hariharan STPavan Kumar wrote:
Hi,
strange Requirements, try to use substr
SELECT Id , SUBSTR(dte,1,10) FROM SAMPLE
No ! That doesn't give any garuantee about the result !
SQL> select created,substr(created,1,10) from dba_users;
CREATED SUBSTR(CREATED,1,10)
20030527 14:12:47 20030527 1
20030527 14:12:47 20030527 1
20030527 14:12:52 20030527 1
20030527 14:20:54 20030527 1
20030527 14:32:36 20030527 1
20070124 16:59:58 20070124 1
20080811 15:08:13 20080811 1
20080929 10:52:04 20080929 1
20070827 15:16:18 20070827 1
20030818 15:41:05 20030818 1
20030527 14:32:40 20030527 1
CREATED SUBSTR(CREATED,1,10)
20040301 07:04:46 20040301 0
20060830 14:14:25 20060830 1
13 rows selected.Don't use string function against date. Use the proper session parameter setting NLS_DATE_FORMAT as well. And work properly with the date format mask.
Nicolas. -
How to convert character value to_date?
I want to change the time (hours) in sysdate to '10' and then save this new date into a date column for that i've written the following command
(TO_CHAR(SYSDATE,'DD-MON-RRRR')||' 10:'||TO_CHAR(SYSDATE,'MI:SS AM'))this command will remove the actual hours in sysdate and change it with the value '10'. but the result of this command is a character value. and i want a date value to insert into a date column. for that i've converted this command to_date as follows
TO_DATE(TO_CHAR(SYSDATE,'DD-MON-RRRR')||' 10:'||TO_CHAR(SYSDATE,'MI:SS AM'))but it gives the following error
ORA-01830: date format picture ends before converting entire input string
how to do this?Gul wrote:
I want to change the time (hours) in sysdate to '10' and then save this new date into a date column for that i've written the following command
(TO_CHAR(SYSDATE,'DD-MON-RRRR')||' 10:'||TO_CHAR(SYSDATE,'MI:SS AM'))this command will remove the actual hours in sysdate and change it with the value '10'. but the result of this command is a character value. and i want a date value to insert into a date column. for that i've converted this command to_date as follows
TO_DATE(TO_CHAR(SYSDATE,'DD-MON-RRRR')||' 10:'||TO_CHAR(SYSDATE,'MI:SS AM'))but it gives the following error
ORA-01830: date format picture ends before converting entire input string
how to do this?TO_DATE(TO_CHAR(SYSDATE,'DD-MON-YYYY')||' 10:'||TO_CHAR(SYSDATE,'MI:SS AM'),'DD-MON-YYYY HH:MI:SS AM') -
Dear All,
I need Output of the following query in the 'JANUARY' or 'JAN' i.e. 'MONTH' or 'MON' format. But following query is returning full date. Could any person help to get me output in MONTH or MON format but SQL should be using TO_DATE function.
TO_DATE(to_char(trunc(ORDERED_DATE),'MONTH'),'MONTH') order_date
Above query is failing to return desired output.
ThanksFor each DATE value, Oracle stores the following information: century, year, month, date, hour, minute, and second.
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/sql_elements001.htm#sthref116
there are some conversion functions :
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions180.htm#i1009324
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions183.htm#i1003589
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions201.htm#i79761
moreover, the human eye can only read character datatypes (not number or date datytypes) !
which means, whenever you do a select sysdate from dual there will be an implicit conversion
to character datatype to make it readable for you (according to nls_date_format) .
alter session set nls_date_format = 'DD.MM.YYYY' ;
select sysdate today from dual ;
TODAY
21.12.2008
alter session set nls_date_format = 'YYYY.MM.DD' ;
select sysdate today from dual ;
TODAY
2008.21.12So the same statement select sysdate today from dual ; will give different results on different systems,
just depending on some initial settings !
You ask why ? It's because you are using implicit conversion !!!
Re: Sql Error -
To_date function now fails
I have a package that ran successfully for years. This morning it fails with a ORA-01858: a non-numeric character was found where a numeric was expected.
It fails on the to_date function ie to_date(SYSDATE,'RRRRMMDD').
Why is it failing now and did not fail previously?Kiran wrote:
then you have to write as to_Date(to_char('21/02/2012','RRRRMMDD'),'RRRRMMDD')NO!
just as sysdate is already a date and is not passed to to_date, the above is already a character string and is not passed to to_char.
And your format mask is wrong. '21/02/2012' is not in the format '21/02/2012'
It should be to_date(''21/02/2012','dd/mm/yyyy'). -
Problem with to_date function
Hi everbody, please I need some help with this:
I need to convert to date the result of the concatenation of several columns of a table.
For example:
select feriado||' '||mes||' '||ano from calendario;
1 ENERO 1997
28 MARZO 1997
4 ENERO 1997
5 ENERO 1997
The data types are:
feriado -> number(2) Example: 28
mes -> varchar2(15) Example: 03
ano -> number (4) Example: 1997
I try to convert that concatenated string to date using this sentence:
select to_date(to_char(feriado||' '||mes||' '||ano),'DD-MM-YYYY') from calendario;
But a get a message error: day of month must be between 1 and last day of month.
The result that I expect is:
01-01-1997
28-03-1997
04-01-1997
Thanks in advance!We translate the month from spanish to English and then convert to date. Hope that helps
WITH T AS
( SELECT 1 feriado, 'ENERO' mes, 1997 ano FROM DUAL UNION ALL
SELECT 28, 'MARZO', 1997 FROM DUAL UNION ALL
SELECT 4, 'ENERO', 1997 FROM DUAL UNION ALL
SELECT 5, 'ENERO', 1997 FROM DUAL
SELECT TO_DATE (FERIADO || '-' ||
DECODE(MES, 'ENERO' , 'JANUARY' ,
'FEBRERO' , 'FEBRUARY' ,
'MARZO' , 'MARCH' ,
'ABRIL' , 'APRIL' ,
'MAYO' , 'MAY' ,
'JUNIO' , 'JUNE' ,
'JULIO' , 'JULY' ,
'AGOSTO' , 'AUGUST' ,
'SEPTIEMBRE' , 'SEPTEMBER' ,
'OCTUBRE' , 'OCTOBER' ,
'NOVIEMBRE' , 'NOVEMBER' ,
'DICIEMBRE' , 'DECEMBER') || '-' ||
ANO,'DD-MONTH-YYYY') DT
FROM T
{code} -
Regarding TO_DATE function and Default Date Format DD-MON-YY
I am using oracle 10.2 version. As far as my knowledge default date format is 'DD-MON-YY'. I want to know that why first query is not giving result.
SQL> select sysdate from dual
2 where sysdate = to_date('06-JUL-11','DD-MON-YY');
no rows selected
SQL> select sysdate from dual
2 where to_date(to_char(sysdate),'DD-MON-YY') = to_date('06-JUL-11','DD-MON-YY');
SYSDATE
06-JUL-11
Thanks,
RohitA date in Oracle always contains hours, minutes and seconds. These values can optionally all evaluate to zeroes, at midnight.
TO_DATE('06-JUL-11','DD-MON-YY') means midnight of the sixth of july 2011.
So
sysdate = to_date('06-JUL-11','DD-MON-YY') only at midnight of the sixth of july 2011...
While during the day, after midnight,
trunc(sysdate) = to_date('06-JUL-11','DD-MON-YY')and
sysdate > to_date('06-JUL-11','DD-MON-YY')Max -
ORA-00932: inconsistent datatypes: expected DATE got NUMBER --TO_DATE()
ORA-00932: inconsistent datatypes: expected DATE got NUMBER
ORA-06512: at "BDSN.GENERAL", line 272
ORA-06512: at line 73
i have fec_alta="10/10/2007" and hor_alta="15:00:00"
and i am converting to DATE with the TO_DATE function
but when inserting i am having this error ORA-00932
is
fmodif DATE;
fmodif:=TO_DATE(fec_alta||' '||hor_alta,'DD/MM/YYYY HH24:MI:ss');
str:= 'insert into '||v_table||'(AF_NUM_IDENTIF,AF_NUM_ALIAS,AF_TIPO_REGISTRO,AF_NUMBRD,AF_FALTA,AF_COD_USU_A,AF_FMODIF,AF_COD_USU_M,AF_MOTIVO,AF_CONDUCTA,AF_TIPO,AF_NUMSERIE,AF_NUMSERIE_SIMPL,AF_NUMSERIE_FON,AF_MARCA,AF_MODELO,AF_MODELO_SIMPL,AF_CALIBRE,AF_ORG_REC,AF_ENT_INT,AF_TIPO_DOC_ORIG_A,AF_FECHA_DOC_ORIG_A,AF_PROC_JUDICIAL,AF_APROCJUD,AF_REF_POLI,AF_OBSV,AF_NUMBRD_BDSN,AF_FEXPIR,AF_RFID,AF_FOTO,AF_GRUPO) values ( '||num_identif||' , '||num_alias||' , '||tipo_registro||' , 0 , sysdate , '||cod_usu_a||' , '||fmodif||', '||cod_usu_m||' , '||motivo||' , '||conducta||' , '||tipo||' , '||numserie||' , '||numserie_simpl||' , '||numserie_fon||' , '||marca||' , '||modelo||' , '||modelo_simpl||' , '||calibre||' , '||org_rec||' , '||ent_int||' , '||tipo_doc||' , sysdate , '||proc_jud||' , '||aprocjud||' , '||ref_pol||' , '||obsv||' , '||numbrd_bdsn||' , sysdate , ''N'' , ''N'' , ''N'' )';
execute immediate str;
thanksInvalid Month error in PL/SQL
¿what is fec_alta? I suppose is a type date
Modify
fmodif:=TO_DATE(fec_alta||' '||hor_alta,'DD/MM/YYYY HH24:MI:ss');
For
fmodif:=TO_DATE( to_char(fec_alta, 'DD/MM/YYYY') ||' '||hor_alta,'DD/MM/YYYY HH24:MI:ss'); -
Is it possible to use MINUS when a count appears in a query qith a group by clause.
I am running the following query and it always gives the result of only the first query (which appears before the minus) it doesnt give the minus effect.
isnt this possible...
SELECT con.agent_code,a.name, PKG.PACKAGE_CODE,count(con.image_id)
FROM PM_PACKAGE PKG, sm_cam_image_detail_bulk CON,cam_agent a
WHERE (CON.PACKAGE_CODE = PKG.PACKAGE_CODE)
AND a.agent_code=con.agent_code
AND a.AG_TY_AGENT_TYPE= 'DEAL'
AND (A.agent_CODE = 'PHONE')
AND (PKG.PRE_POST_code = 'POSTPAID')
AND TO_DATE(TO_CHAR(profile_created_date,'DD/MM/YYYY'),'DD/MM/YYYY') BETWEEN TO_DATE('01/08/2006','DD/MM/YYYY') AND TO_DATE('31/08/2006','DD/MM/YYYY')
group by con.agent_code,a.name, PKG.PACKAGE_CODE
MINUS
SELECT con.agent_code,a.name, PKG.PACKAGE_CODE,count(con.image_id)
FROM PM_PACKAGE PKG, sm_cam_image_detail_bulk CON,cam_agent a, dr_registration dr
WHERE con.package_code = PKG.package_code
AND a.agent_code= con.agent_code
AND con.mobile_no = dr.mobile_no
AND con.agent_type = 'DEAL'
AND (con.agent_CODE = 'PHONE')
AND dr.package_code not like 'I_%'
AND TO_DATE(TO_CHAR(con.start_date,'DD/MM/YYYY'),'DD/MM/YYYY') = TO_DATE('01/08/2006','DD/MM/YYYY')
AND TO_DATE(TO_CHAR(con.end_date,'DD/MM/YYYY'),'DD/MM/YYYY') = TO_DATE('31/08/2006','DD/MM/YYYY')
group by con.agent_code,a.name, PKG.PACKAGE_CODE
order by agent_code,name,PACKAGE_CODETry the follwing code (not tested):
select query_1.agent_code,
query_1.name,
query_1.package_code,
cnt_query_1 - cnt_query_2 difference
from
( select con.agent_code,a.name, pkg.package_code,count(con.image_id) cnt_query_1
from pm_package pkg, sm_cam_image_detail_bulk con,cam_agent a
where (con.package_code = pkg.package_code)
and a.agent_code=con.agent_code
and a.ag_ty_agent_type= 'DEAL'
and (a.agent_code = 'PHONE')
and (pkg.pre_post_code = 'POSTPAID')
AND profile_created_date between to_date('01/08/2006','DD/MM/YYYY') and to_date('31/08/2006','DD/MM/YYYY')
group by con.agent_code,a.name, pkg.package_code
) query_1,
select con.agent_code,a.name, pkg.package_code,count(con.image_id) cnt_query_2
from pm_package pkg, sm_cam_image_detail_bulk con,cam_agent a, dr_registration dr
where con.package_code = pkg.package_code
and a.agent_code= con.agent_code
and con.mobile_no = dr.mobile_no
and con.agent_type = 'DEAL'
and (con.agent_code = 'PHONE')
and dr.package_code not like 'I_%'
and con.start_date = to_date('01/08/2006','DD/MM/YYYY')
and con.end_date = to_date('31/08/2006','DD/MM/YYYY')
group by con.agent_code,a.name, pkg.package_code
order by agent_code,name,package_code
) query_2
where query_1.agent_code = query_2.agent_code
and query_1.name = query_2.name
and query_1.package_code = query_2.package_code
;
Maybe you are looking for
-
Maybe it's because I've never looked before, but I have never before noticed a database which has been started with a pfile containing "db_files = 80" and yet displays "MAXDATAFILES 30" when I dump the control file to trace. Views? and thanks up fron
-
Random video playing in background
today i turned on my mac like normal, opened up safari to watch videos online, and this random stuff plays in the background...i recognize it as a video on youtube ive watched before but there is no window to see it or close it nothing is hidden its
-
Confirmation cancelled but still showing in RG1
Hi, I have reverse a confirmation with - 1. 102 movement type against Production order with MB31, than 2. 262 movement type same as above with MB1A But confirmation is still showing in somewhere in RG1. I dont know about RG1. Please suggest what I ha
-
Hi all I'm having an issue with a 1252 LAP that is connected to the WLC over a WAN link. Basically, it won't associate. The following is taken from a console into the LAP: *Mar 1 00:00:07.799: %LINK-3-UPDOWN: Interface GigabitEthernet0, changed state
-
Calibration: Error while taking Usage Decision (Urgent)
Hi Gurus, I created one test equipment and installed it on some m/c and while doing calibration during usage decision if i reject the equipment it is giving me the error : equipment installed on superior equipment. if i accept the usage decision syst