Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove leading zeroes from day and month values in Oracle, when parsing to string using to_char function?

Tags:

I want to retrieve a date without the leading zeroes in front of the day and month values in a select statement. If I execute the following query

 select to_char(sysdate, 'dd.mm.yyyy') from dual; 

I will get 21.03.2014 as a result. Moreover, if today was, for example, 7th of March, 2014, I would get 07.03.2014. How can I get rid of these leading zeroes?

like image 518
Yulian Avatar asked Mar 21 '14 07:03

Yulian


People also ask

How do you remove leading zeros from a string in Oracle?

TRIM enables you to trim leading or trailing characters (or both) from a character string. If trim_character or trim_source is a character literal, then you must enclose it in single quotes. If you specify LEADING , then Oracle Database removes any leading characters equal to trim_character .

What is the use of TO_CHAR in Oracle?

TO_CHAR (number) converts n to a value of VARCHAR2 datatype, using the optional number format fmt . The value n can be of type NUMBER , BINARY_FLOAT , or BINARY_DOUBLE . If you omit fmt , then n is converted to a VARCHAR2 value exactly long enough to hold its significant digits.

What does TO_CHAR return in Oracle?

TO_CHAR (character) converts NCHAR , NVARCHAR2 , CLOB , or NCLOB data to the database character set. The value returned is always VARCHAR2 .


2 Answers

select   to_char(sysdate,'DD.MM.YY') -- Without Fill Mode ,        to_char(sysdate-20,'fmDD.fmMM.YY')  -- With Fill Mode, 20 days ago   from dual; 

Returns

21.03.14    | 1.3.14 

FM Fill mode.

In a datetime format element of a TO_CHAR function, this modifier suppresses blanks in subsequent character elements (such as MONTH) and suppresses leading zeroes for subsequent number elements (such as MI) in a date format model. Without FM, the result of a character element is always right padded with blanks to a fixed length, and leading zeroes are always returned for a number element. With FM, which suppresses blank padding, the length of the return value may vary.

like image 136
Rob van Laarhoven Avatar answered Oct 02 '22 08:10

Rob van Laarhoven


Try this:

select to_char(to_date('20-oct-2000'),'fmmm/dd/fmrr') from dual

The above query will remove the leading zeroes from day and month values in Oracle.

like image 33
sen Avatar answered Oct 02 '22 09:10

sen