Removing whitespace between comma seperated values [duplicate] - 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

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;

SQL check day of week

How to check if it's a weekend in SQL?
I know I can convert sysdate to a number using this
SQL> select to_char(sysdate, 'd') from dual;
TO_CHAR(SYSDATE,'D')
But I'm not really sure how to check if today is a 6 or 7.
Do not use TO_CHAR with the D format model for this as it is dependant on the NLS_TERRITORY session parameter.
For example, when SYSDATE = 2018-09-10 (a Monday):
ALTER SESSION SET NLS_TERRITORY = 'France';
SELECT TO_CHAR( SYSDATE, 'D' ) FROM DUAL;
Outputs 1 but the same query in a different territory:
ALTER SESSION SET NLS_TERRITORY = 'America';
SELECT TO_CHAR( SYSDATE, 'D' ) FROM DUAL;
Outputs 2.
Instead, you can use TRUNC and the IW format model:
SELECT TRUNC( SYSDATE ) - TRUNC( SYSDATE, 'IW' ) FROM DUAL
Outputs 0 for Monday (and 1 for Tuesday ... 6 for Sunday) and is independent of the NLS_TERRITORY setting.
So you could filter this to give weekends as:
SELECT *
FROM DUAL
WHERE TRUNC( SYSDATE ) - TRUNC( SYSDATE, 'IW' ) IN ( 5, 6 )
or
SELECT *
FROM DUAL
WHERE SYSDATE - TRUNC( SYSDATE, 'IW' ) >= 5
If you want the days 1-indexed for consistency with your expected output from TO_CHAR (rather then 0-indexed) then just add 1 to the value.
sysdate is a pseudo column. You don't need to query it, you can evaluate it directly:
IF TO_CHAR(SYSDATE, 'D') IN ('6', '7') THEN
-- Do something
END IF;
I would avoid the ambiguous 'D' format as this varies between territories (the week starts after the weekend where I live), and use
if to_char(sysdate,'fmDY','nls_date_language=English') like 'S%'
then
Regarding the 'D' format, unfortunately to_char doesn't let you specify nls_territory inline, so without an explicit alter session command, it will rely on the session settings at runtime. I've seen production bugs due to this, where the same code worked in London but failed in New York.
And here's the logic in a reusable function, but flipped to ask "Is it a weekday?" (not a weekend). You could add a parameter for the start day of the weekdays (2 is the default in Oracle; Sunday is day #1).
CREATE OR REPLACE FUNCTION is_weekday (date_in IN DATE)
RETURN BOOLEAN
IS
BEGIN
RETURN TO_CHAR (date_in, 'D') BETWEEN 2 AND 6;
END;
/
DECLARE
l_date DATE := DATE '2018-09-10';
BEGIN
DBMS_OUTPUT.put_line ('If your weekend is Saturday and Sunday....');
FOR indx IN 1 .. 7
LOOP
DBMS_OUTPUT.put_line (
TO_CHAR (l_date, 'FMDay, Month DD YYYY')
|| ' is '
|| CASE WHEN NOT is_weekday (l_date) THEN 'not ' END
|| 'a weekday');
l_date := l_date + 1;
END LOOP;
END;
/
Try it in LiveSQL:
https://livesql.oracle.com/apex/livesql/file/content_G8NQSY6NP48NPJX96RLQ51SUE.html

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;

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

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