Oracle Date TO_CHAR('Month DD, YYYY') has extra spaces in it - sql

When I do...
Select TO_CHAR (date_field, 'Month DD, YYYY')
from...
I get the following:
July 01, 2011
April 01, 2011
January 01, 2011
Why are there extra spaces between my month and day? Why doesn't it just put them next to each other?

Why are there extra spaces between my month and day? Why does't it just put them next to each other?
So your output will be aligned.
If you don't want padding use the format modifier FM:
SELECT TO_CHAR (date_field, 'fmMonth DD, YYYY')
FROM ...;
Reference: Format Model Modifiers

if you use 'Month' in to_char it right pads to 9 characters; you have to use the abbreviated 'MON', or to_char then trim and concatenate it to avoid this. See, http://www.techonthenet.com/oracle/functions/to_char.php
select trim(to_char(date_field, 'month')) || ' ' || to_char(date_field,'dd, yyyy')
from ...
or
select to_char(date_field,'mon dd, yyyy')
from ...

You should use fm element to delete blank spaces.
SELECT TO_CHAR(sysdate, 'fmDAY DD "de" MONTH "de" YYYY') CURRENT_DATE
FROM dual;

SQL> -- original . . .
SQL> select
2 to_char( sysdate, 'Day "the" Ddth "of" Month, yyyy' ) dt
3 from dual;
DT
----------------------------------------
Friday the 13th of May , 2016
SQL>
SQL> -- collapse repeated spaces . . .
SQL> select
2 regexp_replace(
3 to_char( sysdate, 'Day "the" Ddth "of" Month, yyyy' ),
4 ' * *', ' ') datesp
5 from dual;
DATESP
----------------------------------------
Friday the 13th of May , 2016
SQL>
SQL> -- and space before commma . . .
SQL> select
2 regexp_replace(
3 to_char( sysdate, 'Day "the" Ddth "of" Month, yyyy' ),
4 ' *(,*) *', '\1 ') datesp
5 from dual;
DATESP
----------------------------------------
Friday the 13th of May, 2016
SQL>
SQL> -- space before punctuation . . .
SQL> select
2 regexp_replace(
3 to_char( sysdate, 'Day "the" Ddth "of" Month, yyyy' ),
4 ' *([.,/:;]*) *', '\1 ') datesp
5 from dual;
DATESP
----------------------------------------
Friday the 13th of May, 2016

try this:-
select to_char(to_date('01/10/2017','dd/mm/yyyy'),'fmMonth fmDD,YYYY') from dual;
select to_char(sysdate,'fmMonth fmDD,YYYY') from dual;

select to_char(sysdate, 'DD-fmMONTH-YYYY') "Date" from Dual;
The above query result will be as given below.
Date
01-APRIL-2019

Related

Removing whitespace between comma seperated values [duplicate]

When I do...
Select TO_CHAR (date_field, 'Month DD, YYYY')
from...
I get the following:
July 01, 2011
April 01, 2011
January 01, 2011
Why are there extra spaces between my month and day? Why doesn't it just put them next to each other?
Why are there extra spaces between my month and day? Why does't it just put them next to each other?
So your output will be aligned.
If you don't want padding use the format modifier FM:
SELECT TO_CHAR (date_field, 'fmMonth DD, YYYY')
FROM ...;
Reference: Format Model Modifiers
if you use 'Month' in to_char it right pads to 9 characters; you have to use the abbreviated 'MON', or to_char then trim and concatenate it to avoid this. See, http://www.techonthenet.com/oracle/functions/to_char.php
select trim(to_char(date_field, 'month')) || ' ' || to_char(date_field,'dd, yyyy')
from ...
or
select to_char(date_field,'mon dd, yyyy')
from ...
You should use fm element to delete blank spaces.
SELECT TO_CHAR(sysdate, 'fmDAY DD "de" MONTH "de" YYYY') CURRENT_DATE
FROM dual;
SQL> -- original . . .
SQL> select
2 to_char( sysdate, 'Day "the" Ddth "of" Month, yyyy' ) dt
3 from dual;
DT
----------------------------------------
Friday the 13th of May , 2016
SQL>
SQL> -- collapse repeated spaces . . .
SQL> select
2 regexp_replace(
3 to_char( sysdate, 'Day "the" Ddth "of" Month, yyyy' ),
4 ' * *', ' ') datesp
5 from dual;
DATESP
----------------------------------------
Friday the 13th of May , 2016
SQL>
SQL> -- and space before commma . . .
SQL> select
2 regexp_replace(
3 to_char( sysdate, 'Day "the" Ddth "of" Month, yyyy' ),
4 ' *(,*) *', '\1 ') datesp
5 from dual;
DATESP
----------------------------------------
Friday the 13th of May, 2016
SQL>
SQL> -- space before punctuation . . .
SQL> select
2 regexp_replace(
3 to_char( sysdate, 'Day "the" Ddth "of" Month, yyyy' ),
4 ' *([.,/:;]*) *', '\1 ') datesp
5 from dual;
DATESP
----------------------------------------
Friday the 13th of May, 2016
try this:-
select to_char(to_date('01/10/2017','dd/mm/yyyy'),'fmMonth fmDD,YYYY') from dual;
select to_char(sysdate,'fmMonth fmDD,YYYY') from dual;
select to_char(sysdate, 'DD-fmMONTH-YYYY') "Date" from Dual;
The above query result will be as given below.
Date
01-APRIL-2019

display name of day in oracle

i want to display in output as : today is wednesday
my code:
select to_char(sysdate,'today is' 'day' ) from dual ;
it is not working.
You need to include the raw text you want in the output in double quotes in the format specification:
select to_char(sysdate, '"today is" day') from dual ;
Output (as of 2020-03-04)
today is wednesday
Demo on dbfiddle
Use concat() or the concatenation operator || to prepend the 'today is ' string.
SELECT concat('today is ', to_char(sysdate, 'day'))
FROM dual;
or
SELECT 'today is ' || to_char(sysdate, 'day')
FROM dual;

Not able to convert into date in oracle

I am trying to convert the date field, but the year is not correct.
My code is:
SELECT to_date((ADD_MONTHS(TRUNC(sysdate, 'MM'), -1) + 25), 'dd/mm/yyyy')
FROM dual
Expected output is like 26/02/2018, but I get 26/02/0018.
Do I miss anything?
You really shouldn't apply TO_DATE function on a DATE datatype value, which is what SYSDATE is.
Saying that you have to compare the result of your query with another DATE datatype value will be just fine, as you'll compare two dates.
As of incorrect format you get: in presentation layer, you'd use TO_CHAR function and format the result anyway you want; for example:
SQL> SELECT
2 to_char((ADD_MONTHS(TRUNC(sysdate, 'MM'), -1) + 25), 'dd/mm/yyyy') val_1,
3 to_char((ADD_MONTHS(TRUNC(sysdate, 'MM'), -1) + 25), 'mon dd yyyy') val_2
4 FROM dual;
VAL_1 VAL_2
---------- --------------------
26/02/2018 vel 26 2018
SQL>
Note that both values are strings, not dates. Moreover, see a strange VAL_2 month? It is in Croatian, but there's a way out of it:
SQL> SELECT
2 to_char((ADD_MONTHS(TRUNC(sysdate, 'MM'), -1) + 25),
3 'mon dd yyyy',
4 'nls_date_language = english') val_2
5 FROM dual;
VAL_2
--------------------
feb 26 2018
SQL>
or even
SQL> alter session set nls_date_language = english;
Session altered.
SQL> SELECT
2 to_char((ADD_MONTHS(TRUNC(sysdate, 'MM'), -1) + 25), 'mon dd yyyy') val_2
3 FROM dual;
VAL_2
--------------------
feb 26 2018
SQL>
but - once again - this is just to present data. There's nothing you should do with your code, except from removing TO_DATE function (optionally, you can alter session in order to see date the way you want it, but it won't change anything but visual appearance):
SQL> alter session set nls_date_format = 'dd/mm/yyyy';
Session altered.
SQL> select ADD_MONTHS(TRUNC(sysdate, 'MM'), -1) + 25 val_3
2 from dual;
VAL_3
----------
26/02/2018
SQL>
If you want the date in a particular format, use to_char():
SELECT to_char((ADD_MONTHS(TRUNC(sysdate, 'MM'), -1) + 25), 'dd/mm/yyyy') from dual
The result of:
SELECT ADD_MONTHS(TRUNC(sysdate, 'MM'), -1) + 25 from dual
is a date and it is correct. The problem is converting the date to a date.

SQL language programming using decode in SELECT statement

I have two parameters called START_DATE and END_DATE.
If the start date and end date are entered , i would like to use the TO_CHAR to convert to a different format and display ":start_date - :end_date" both in the format 'MON DD, YYYY')
However, if the parameters are not entered i would like to use the TO_CHAR function to display information for the previous month i.e ( MAR 01, 2017- MAR 31,2017).
Here's the code below. Can someone identify my error and assist?
SELECT DECODE (TO_CHAR(:START_DATE, 'MON DD, YYYY',(trunc(trunc(sysdate,'MONTH')-1,'MONTH'))),'MON DD, YYYY')
|| ' - ' || DECODE (TO_CHAR(:END_DATE, 'MON DD, YYYY',(trunc(sysdate,'MONTH')-1)'MON DD, YYYY') BILLPERIOD
How do i write this in sql using the decode function?
I highly recommend you use a CASE statement instead. More control, more readable, cleaner, etc. In my humble opinion of course.
SELECT
CASE
-- If one or both dates are NULL, revert to last month
WHEN start_date IS NULL OR end_date IS NULL THEN
to_char(trunc(trunc(sysdate,'MM')-1,'MM'), 'MON DD, YYYY') || ' - ' || to_char(trunc(sysdate,'MM')-1, 'MON DD, YYYY')
WHEN start_date IS NOT NULL AND end_date IS NOT NULL AND
(start_date < end_date) THEN
to_char(start_date, 'MON DD, YYYY') || ' - ' || to_char(end_date, 'MON DD, YYYY')
-- ** ALWAYS EXPECT THE UNEXPECTED! **
-- Like start_date >= end_date
ELSE '** DATE ERROR **'
END as billperiod
FROM dual;
EDIT: Added a check for start_date must be < end_date.
If you can use CASE in your project try:
SELECT
CASE START_DATE
WHEN NULL THEN TO_CHAR(PREVIOUS_DATE, 'MON DD, YYYY')
ELSE TO_CHAR(START_DATE, 'MON DD, YYYY')
END || ','
CASE END_DATE
WHEN NULL THEN TO_CHAR(PREVIOUS_DATE, 'MON DD, YYYY')
ELSE TO_CHAR(END_DATE, 'MON DD, YYYY')
END "Formated Date"
FROM BILLPERIOD;

How to change date format to 'YYYY/DD/MM'?

i got financial year 1st month April and 1st week date using (:weekno) from this query
select TO_CHAR(ADD_MONTHS((Select ((TRUNC (Trunc(sysdate,'yyyy')+( :weekno)*7,'IW'))+1) fdt from duaL),+3))
from dual
Output got:03-APR-17
but i want output this format 'YYYY/DD/MM' 04/03/2017
You are using dual twice. There is no need for that.
Use to_char with yyyy/mm/dd format to get 2017/03/04:
select
to_char(
add_months(
trunc(
trunc(sysdate,'YY') + :week_no * 7, 'IW'
) + 1, 3
), 'yyyy/mm/dd'
) dt
from dual;
Use to_char with dd/mm/yyyy format to get 04/03/2017:
select
to_char(
add_months(
trunc(
trunc(sysdate,'YY') + :week_no * 7, 'IW'
) + 1, 3
), 'mm/dd/yyyy'
) dt
from dual;
If you need date value:
select to_date(TO_CHAR(ADD_MONTHS((Select ((TRUNC (Trunc(sysdate,'yyyy')+( :weekno)*7,'IW'))+1) fdt from duaL),+3)),'yyyy/mm/dd') from dual
If need string value:
select TO_CHAR(ADD_MONTHS((Select ((TRUNC (Trunc(sysdate,'yyyy')+( :weekno)*7,'IW'))+1) fdt from duaL),+3),'yyyy/mm/dd')from dual