SELECT following Friday date - sql

I need to select next friday from current date.
For example:
Today SELECT
Monday 6/24/2013 6/28/2013
Wednesday 6/26/2013 6/28/2013
Friday 6/28/2013 6/28/2013
Saturday 6/29/2013 7/5/2013
I found a few solutions close to what I want but all they do is select fridat of the day's week.
I need to use this date as a Default Value in the table column. When a user inserts a record, this column should automatically be set to the folloing Friday. Say, instead of getdate() in the 'Default Value or Binding', I need to put this statement that selects following Friday.

select DATEADD(day,6-datepart(weekday,the_dt),the_dt)
+ Case when datepart(weekday,the_dt) = 7 then 7 else 0 end
from table

Related

Find number of days in a given week according to the month for a given date. in SQL

Consider the date "2022-07-02"
For the month July first week only have 3 days in it.
I need to find the number of days in the week for the given date.
In above date the week has 3 days where "2022-07-02" day reside.
Example 2 :
For month June in 2022 first week has 5 days in the week
Therefore if i declare a date as "2022-06-03" it should pass the number of days in the week as 5
I need a query to find the number of days for the specific week.
set datefirst 1 -- assumes Monday is set as start of week
declare #myDate date = getdate();
-- calculate the next last day of week
declare #nextSunday date = dateadd(day, 7 - datepart(weekday, #myDate), #myDate);
select case
-- advancing into the next month implies a partial week
-- datediff(month, #myDate, nextSunday) = 1 would be equivalent
when day(#nextSunday) < day(#myDate) then 7 - day(#nextSunday)
-- else see if still within first week
when day(#nextSunday) < 7 then day(#nextSunday)
else 7 end;
Within a query you might use it this way:
select case
when day(nextSunday) < day(dateColumn) then 7 - day(nextSunday)
when day(nextSunday) < 7 then day(nextSunday)
else 7 end
from
myData cross apply (
values (dateadd(day, 7 - datepart(weekday, dateColumn), dateColumn))
) v(nextSunday);
https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=ee5bfb52dabe31dd619cfd136689db59
If you don't want the shorthand form then just replace every instance of nextSunday in the final step with its full expression.
There's nothing in the logic that prevents this from working with another first day of week. I just chose a variable name that helped ellucidate this particular problem.

SQLite select date and increment selection when points to weekend

I want to increment days when I select a date in SQLite and it points to weekends,
here is the code.
SELECT strftime('%Y-%d-%m','now', '+9 days');
Assuming what you want Saturdays and Sundays to be the following Monday then the following may do what you wish :-
SELECT CASE
WHEN strftime('%w','now') = 0 THEN strftime('%Y-%d-%m','now','+1 days') /* add 1 day when Sunday */
WHEN strftime('%w','now')= 6 THEN strftime('%Y-%d-%m','now','+2 days') /* add 2 days when Saturday */
ELSE strftime('%Y-%d-%m','now')
END AS adjusted_date;
If not then as you can see %w returns the days of the week where 0 is Sunday and 6 is Saturday. and you can use CASE WHEN THEN ELSE clause to conditionally returns values.
Date And Time Functions

Get Work week or Number of Weeks T-SQL

How can I get the exact week number if my date range is from Friday to Thursday nextweek?
I used this code
datepart(wk,t.date) as workweek
but the week number is different, maybe because the format that I want to get is from Friday to Thursday nextweek. I hope somebody can answer. TIA!
SET DATEFIRST 5; -- Set Friday as first day of the week
Select datepart(wk,t.date) -1 as workweek
From yourTable as t

Calculating Saturday and Sunday Business Date when Friday or Monday are a Holiday

I am working on a calendar generator in MS SQL 2008 and need to generate the correct business day for each day in the month. Everything is working with this exception: If Friday or Monday are a Holiday, then I get an erroneous value for the Business Dates for that weekend (Saturday and Sunday). I already have a column that returns a value of 1 if a Monday (MHDY) is a Holiday, and a separate column for Friday as well (FHldy) and what I am attempting to do is assign a value of 1 in the MHDY and FHldy columns for Saturday and Sunday, if the Friday before or Monday after are holidays. Here is the portion of Code that produces the values mentioned above:
SELECT
MC.[Date]
,MC.[Year]
,MC.[Quarter]
,MC.[Month]
,MC.[Week]
,MC.[Day]
,MC.[DayOfYear]
,MC.[Weekday] -- This has first day of week as Monday
,MC.[KindOfDay]
,MC.[Description]
,CASE WHEN Weekday BETWEEN 1 and 6 THEN Weekday +1
WHEN Weekday = 7 THEN 1 END AS [DayofWeek]
-- This will convert first day of week to Sunday
, CASE WHEN MC.KindOfDay = 'Holiday' THEN 1 ELSE 0 END AS Holiday
, CASE WHEN MC.KindOfDay <> 'BusDay' THEN 1 ELSE 0 END AS NonBDay
, CASE WHEN MC.KindOfDay = 'BusDay' THEN 1 ELSE 0 END AS BDay
, CASE WHEN MC.KindOfDay = 'Holiday' AND MC.Weekday = 1 THEN 1 ELSE 0 END AS MHDY
, CASE WHEN MC.Date = DateAdd(Month, 1, MC.Date - Day(MC.Date) + 1) -1 THEN 1 ELSE 0 END AS LDoM
, CASE WHEN MC.KindOfDay = 'Holiday' AND MC.[Weekday] = 5 THEN 1 ELSE 0 END AS FHldy
INTO #AllC2
FROM #MasterCal MC
There is much more code to the Calendar than this portion, but this is where I am attempting to add this information.
Is there a way to modify the CASE Statement to say "If Monday is a Holiday, return a value of 1 for Saturday and Sunday to MHDY." The same would apply for Fridays.
So close... this is the last hurdle. I appreciate any help you can offer.
The solution to the problem turned out to be simple.
I added lines to the CASE statement in the #MasterCal table to label Weekends before Monday holidays as Holidays in the KindOfDay field. the I added lines in the next section to tell it what date to assign those weekends that were listed as Holidays. Problem solved.

Get 1st Day of Week (Sunday)

I have this formula in my column to calculate the start date of a given week :
dateadd(week,[Week]-(1),
dateadd(day,(-1),
dateadd(week,
datediff(week,(0),
CONVERT([varchar](4),[Year],(0))+'-01-01'),
(1))))
Where Week and Year are other fields like 38 and 2012
Problem is, it calculates the start date of week 38/2012 as a monday (17th Sept), I would like it to be a sunday instead (16th Sept) is this possible?
Many thanks.
This will return you the first day of the week, given a week number and a year, assuming that the first day of the week is a Sunday.
Standard exclusions apply, e.g. don't try year 1499.
declare #tbl table ([Week] int, [Year] int)
insert #tbl select 38,2012
union all select 1,2012
union all select 0,2012
union all select 1,2013
select DATEADD(
Week,
[Week]-1,
DATEADD(
Day,
(8-##datefirst)-DATEPART(dw, CAST([Year]*10000+101 AS VARCHAR(8))),
CAST([Year]*10000+101 AS VARCHAR(8))))
from #TBL
Result
2012-09-16 00:00:00.000
2012-01-01 00:00:00.000
2011-12-25 00:00:00.000
2012-12-30 00:00:00.000
Note that the Week number starts from 1, and if the week doesn't start on a Sunday, then the first day of that week could end up in an earlier year (row #4). Because the Weeks are relative, you can use Week 0, -1 etc and it will still give you a result (row #3), rightly or wrongly.
You may also notice I used a different method to create a date out of the year, just as an alternative.
The (8-##datefirst) portion of the query makes it robust regardless of your DATEFIRST setting.
If you want the first day of the week to be Sunday you could change your database to make it so, using the DATEFIRST setting.
function getFirstDateOfThisWeek(d)
{
var TempDate = new Date(d || new Date());
TempDate.setDate(TempDate.getDate() + (#Config.WeekStartOn - 1 - TempDate.getDay() - 7) % 7);
return TempDate;
}
var StartDate = getFirstDateOfThisWeek(new Date()); //1st date of this week