How to format date column to get the day name in Oracle SQL? - sql

I have a oracle db and there are two columns so i want to display day ex:(SUNDAY, MONDAY...) according to given date in db.
Table Name: TBL_HOLIDAY_MASTER:
Holiday_date Description
***********************************
22-NOV-15 Weekly Holiday
23-NOV-15 Working Day
24-NOV-15 Working Day
29-NOV-15 Weekly Holiday
30-NOV-15 Working Day
21-MAY-17 Weekly Holiday
18-AUG-19 Weekly Holiday
I want output Like:-
Holiday_date Description
*************************************
SUNDAY Weekly Holiday
MONDAY Working Day
TUESDAY Working Day
SUNDAY Weekly Holiday
MONDAY Working Day
SUNDAY Weekly Holiday
SUNDAY Weekly Holiday

You may achieve this with TO_CHAR function with DAY parameter, in your case it would be:
SELECT TO_CHAR(Holiday_date,'DAY') as Holiday_date, Description
FROM TBL_HOLIDAY_MASTER;

You need to use TO_CHAR and FMDAY format to get the day name. FM is required to remove the trailing blank spaces.
TO_CHAR(date_column, 'FMDAY', 'NLS_DATE_LANGUAGE=ENGLISH')
For example,
SQL> SELECT TO_CHAR(SYSDATE + LEVEL -1, 'FMDAY', 'NLS_DATE_LANGUAGE=ENGLISH') "DAYS"
2 FROM DUAL
3 CONNECT BY level <= 7;
DAYS
---------
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
SUNDAY
7 rows selected.

Related

Finding first Sunday of the month is not working

I need to find first Sunday of the month. tried with next_day function which produced proper result, but if the first day of the month is a Sunday then it doesn't work.
select next_day(trunc(sysdate,'MM'),'sun') first_sun
from dual
select next_day(trunc(sysdate,'MM'),'sun') first_sun
from dual
I'm running the query with the date sysdate='2019-09-10' which shows the 2nd Sunday instead of the 1st Sunday.
Just go back a day after truncating, so you find the first Sunday after the last day of the previous month:
select next_day(trunc(sysdate,'MM') - 1,'sun') first_sun from dual;
FIRST_SUN
----------
2019-09-01
You happen to land on month when Sunday is also the first day in month,
NEXT_DAY uses later than date, therefore you will have to check from previous day for example
select next_day(trunc(sysdate,'MM')-1,'SUNDAY') first_sun from dual;
NEXT_DAY returns the date of the first weekday named by char that is later than the date date.

How to query last Saturday and the one before in SQL?

I would like to query data, which is between some week days, as follows:
Last Saturday
The Sunday before it
The Saturday before that queried Sunday on 2.
The Sunday before that queried Saturday on 3.
The query would run automatically every Monday, so I would like to set a dynamic condition for it, so that it automatically picks that days, without depending on any day it will run on in the future.
So for example if today is Monday 01/08/2018:
would be 01/06/2018
would be 12/31/2017
would be 12/30/2017
would be 12/24/2017
I would like to set that conditions in the WHERE clause. For now I am querying it this way, with constant dates for example for last week data:
SELECT *
FROM thisTable
WHERE Date(operation_date) BETWEEN '2018-06-10' AND '2018-06-16'
DBMS: Amazon Redshift
The DATE_TRUNC Function is your friend. It truncates to a Monday.
SELECT
CURRENT_DATE AS today,
DATE_TRUNC('week', CURRENT_DATE) as most_recent_monday,
DATE_TRUNC('week', CURRENT_DATE) - 2 AS most_recent_saturday,
DATE_TRUNC('week', CURRENT_DATE) - 8 AS sunday_before_most_recent_saturday
Returns:
2018-06-21 | 2018-06-18 00:00:00 | 2018-06-16 00:00:00 | 2018-06-10 00:00:00
Note that it treats the date as midnight at the beginning of the day. So, you don't really want to query Sunday to Saturday. You actually want to query Sunday to Sunday (which really means midnight at the start of Sunday to midnight at the start of the next Sunday). This assumes your source date is a timestamp.
If your source date is purely a date, then you would want to use Sunday to Saturday.
If you want to query everything from "last week" (if your definition is Sunday to Sunday), and assuming a timestamp, use:
SELECT *
FROM thisTable
WHERE operation_date BETWEEN
-- Most recent Monday minus 8 days = Two Sundays ago
DATE_TRUNC('week', CURRENT_DATE) - 8 AND
-- Most recent Monday minus 1 day = Most recent Sunday
DATE_TRUNC('week', CURRENT_DATE) - 1
(Well, unless you're on a Sunday already, but that's your problem!)
If the date is a date, you'd have to adjust it a bit.
last Sat: date_trunc('week',getdate())-interval '2 day'
prev Sat: date_trunc('week',getdate())-interval '9 day'
this is for Monday-based week

PLSQL - How to find Monday and Friday of the week of a given date

I have spent days trying to figure this out to no avail, so hopefully someone can help me. I have a queried date set which contains several fields including a column of dates. What I want to do is create a new field in my query that tells what the Monday and Friday is for the week of that row's particular date.
So for example; if the date in one of my rows is "1/16/18",
the new field should indicate "1/15/18 - 1/19/18".
So basically I need to be able to extract the Monday date (1/15/18) and the Friday date (1/19/18) of the week of 1/16/18 and then concatenate the two with a dash ( - ) in between. I need to do this for every row.
How on earth do I do this? I've been struggling just to figure out how to find the Monday or Friday of the given date...
Assuming that your column is of type date, you can use trunc to get the first day of the week (monday) and then add 4 days to get the friday.
For example:
with yourTable(d) as (select sysdate from dual)
select trunc(d, 'iw'), trunc(d, 'iw') + 4
from yourTable
To format the date as a string in the needed format, you can use to_char; for example:
with yourTable(d) as (select sysdate from dual)
select to_char(trunc(d, 'iw'), 'dd/mm/yy') ||'-'|| to_char(trunc(d, 'iw') + 4, 'dd/mm/yy')
from yourTable
gives
15/01/2018-19/01/18
There may be a simpler, canonical Oracle method to this but you can still reduce it to a simple calculation on your own either way. I'm going to assume you're dealing with only dates falling Monday through Friday. If you do need to deal with weekend dates then you might have to be more explicit about which logical week they should be attached to.
<date> - (to_char(<date>, 'D') - 2) -- Monday
<date> + (6 - to_char(<date>, 'D')) -- Friday
In principle all you need to do is add/subtract the appropriate number of days based on the current day of week (from 1 - 7). There are some implicit casts going on in there and it would probably be wise to handle those better. You might also want to check into NLS settings to make sure you can rely on to_char() using Sunday as the first day of week.
https://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm
You can also use the NEXT_DAY function, as in:
SELECT TRUNC(NEXT_DAY(SYSDATE, 'MON')) - INTERVAL '7' DAY AS PREV_MONDAY,
TRUNC(NEXT_DAY(SYSDATE, 'FRI')) AS NEXT_FRIDAY
FROM DUAL;
Note that using the above, on weekends the Monday will be the Monday preceding the current date, and the Friday will be the Friday following the current date, i.e. there will be 11 days between the two days.
You can also use
SELECT TRUNC(NEXT_DAY(SYSDATE, 'MON')) - INTERVAL '7' DAY AS PREV_MONDAY,
TRUNC(NEXT_DAY(SYSDATE, 'MON')) - INTERVAL '3' DAY AS NEXT_FRIDAY
FROM DUAL;
in which case the Monday and Friday will always be from the same week, but if SYSDATE is on a weekend the Monday and Friday returned will be from the PREVIOUS week.

If actual date is Friday, also show rows at saturday and sunday Oracle DB

Normally I only show a record, if the actual date is one year later than the date in the database. How can I check if that day is a friday and then show also the records with the date of the saturday or sunday?
For example: Friday the 13th before one year. I will also show records from 14th(saturday) and 15th(sunday)
where myDate.arrival < TRUNC (SYSDATE) -365)
That's my actual statement.
You need a condition in case. Check the example below for reference:
WHERE txnday in
CASE to_char(sysdate, 'Day')
WHEN 'Friday' THEN // Include condition for Sat and Sun as well
ELSE // Include condition for only that day
END
Where Mydate.Arrival < Trunc (Sysdate) - 365
Or (to_char(Mydate.Arrival, 'D') = 6 AND Mydate.Arrival < Trunc (Sysdate) -363)
This way, you can check if that day is a Friday (6th day of week) and then select records whose Arrival value is less than sysdate - 363 (2 more days - Saturday and Sunday - included).

SQLite query question

I have 1 table:
table tb1 (
_id integer primary key autoincrement
,busnum text not null
, servdate date not null
);
I need a query which will get me all entries which have a "servdate" for the current week (the week starts on monday.)
So for example:
if I run the query on wednesday the 24th novemeber, it will get me all entries for monday 22nd, tuesday 23rd and wednesday 24th.
If I ran the query on sunday 28th, it will get me all entries for the full week (mon - sun), starting monday 22nd - sunday 28th.
If I run the query on monday, then it will just get me all entries for that day.
Thanks in advance. (thanks to admin for formatting my question)
Use the modifier weekday 1 (from the date time functions documentation) :
WHERE servdate BETWEEN date('now', 'Weekday 1', '-7 days') AND date('now')