Oracle - How to get first Monday of the year? - sql

How to get first Monday of the year?
select TRUNC(date'2015-01-01','YYYY')
,NEXT_DAY(TRUNC(date'2015-01-01','YEAR')+1,'MONDAY')
from dual;
01-JAN-2015 05-JAN-2015

To account for the possibility of the year starting on a Monday, you need to go back to the last day of the previous year, before going forward to the next Monday; for the current year:
next_day(trunc(sysdate, 'YYYY') - 1, 'Monday')
SQL Fiddle demo. This gives the first Monday of 2007 as January 1st; without the -1 adjustment it would say the 8th.

Try this:
select next_day(round(sysdate,'yyyy'),'mon')
from dual;

Related

I want to extract the week based on sysdate in Oracle SQL, what function do i use

select datepart(year, '2017/08/25') as week;
I believe this is for mysql but does not work for oracle sql
Week of year (1-53) where week 1 starts on the first day of the year and continues to the seventh day of the year.
select to_char(trunc(sysdate),'WW') from dual;
Week of year (1-52 or 1-53) based on the ISO standard.
select to_char(trunc(sysdate),'IW') from dual;
Taken from Oracle docs

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.

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.

how to get date of 1st week of current month

I have to get/create date (date of first week) from the user input of day name
Examples-
if input saturday then date should be 7 (for current month 1st saturday)
if input sunday then date 1 (current month 1st sunday)
I am having to use lot of logic to get the date but couldn't get exact output
any suggestions on how to come up with the SQL query for such a function ?
If you are using SQL*plus in Oracle then the code will be as :
select next_day(sysdate,'&d')-7 from dual;
If any update required please do inform.
okay i have a link hereby where you can understand this and try to do
Your answer!!!
Try this.
SELECT DATEPART(dw,DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0))
input: if getdate() returns date in jan 2017
output: 1
input:if getdate() returns date in jan 2017
output: 4

SSRS BIDS 2008 Expression

What is the correct expression to use for todays date plus 1 year.
I assume it starts with Now()+ but im unsure from there
This page has lots of great examples, including:
=DateAdd(DateInterval.Month, 6, Parameters!StartDate.Value)
From that and the example before it, it looks like you want:
=DateAdd(DateInterval.Year, 1, Today())
this should be what your looking for:
--midnight last day of last month
select DateAdd(mm,-0,(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))))
--midnight last day of this month
select DateAdd(mm,+1,(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))))
--midnight last day of last month 1 year ago
select DateAdd(yy,-1,(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))))
--midnight last day of this month 1 year ago
select DateAdd(yy,-1,DateAdd(mm,+1,(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)))))