Solved

Day of Week function in TSQL

Posted on 2004-04-08
9
9,667 Views
Last Modified: 2012-06-21
I'm attempting to write a function that will provide me with next day of a given date.  For instance, if the date I have is April 8 2004 (thursday) and I need the first monday following that date.

The problem I've run into is that I can't seem to find a way of detecting what the weekday of a date is.

I've gone this far...

Create Function dbo.GetNextDay(@SDate DateTime, @Day Int)
Returns DateTime
AS
Begin
      Declare @Ret DateTime,
            @DayDif Int

      --HERE I NEED TO SET @DAYDIF TO THE NUMBER OF DAYS BETWEEN THE CURRENT DAY AND THE DAY I'M LOOKING FOR

      Set @Ret = DateAdd(@SDate,@DayDif)

      Return @Ret
End
Go


Thanks in advance.
0
Comment
Question by:JackieLee
  • 5
  • 3
9 Comments
 
LVL 6

Expert Comment

by:billy21
ID: 10782411
You use DatePart as so...

select DatePart(dw,'2 nov 2003')
0
 
LVL 6

Expert Comment

by:billy21
ID: 10782423
Also the @@DATEFIRST global tells you the first day of week.  It can be set too

Set @@DATEFIRST = 1 --sets the first day of week to Monday.
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 10782437
this solution works whith any langage settings

--with  @day = 1 for Monday, 2 for Wednesday, and so on through 7 for Sunday.
set @datedif = (15+@day-@@datefirst-datepart(dw, getdate()))%7

Hilaire
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 10782448
Create Function dbo.GetNextDay(@SDate DateTime, @Day Int)
Returns DateTime
AS
Begin
     Declare @Ret DateTime,
          @DayDif Int
     set @DayDif = (15+@day-@@datefirst-datepart(dw, getdate()))%7
     Set @Ret = DateAdd(@SDate,@DayDif)
     Return @Ret
End
Go
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 6

Expert Comment

by:billy21
ID: 10782455
Hilaire,

Should getdate() not be @Sdate?
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 10782459
sorry, tested with getdate(), but I think you need if with @SDate instead

Create Function dbo.GetNextDay(@SDate DateTime, @Day Int)
Returns DateTime
AS
Begin
     Declare @Ret DateTime,
          @DayDif Int
     set @DayDif = (15+@day-@@datefirst-datepart(dw, @SDate))%7
     Set @Ret = DateAdd(@SDate,@DayDif)
     Return @Ret
End
Go
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 10782465
Thanks billy21,
I think ours posts collided ; )
0
 
LVL 26

Accepted Solution

by:
Hilaire earned 250 total points
ID: 10782491
BTW, the whole thing could write

Create Function dbo.GetNextDay(@SDate DateTime, @Day Int)
Returns DateTime
AS
Begin
     Return DateAdd(d, (15+@day-@@datefirst-datepart(dw, @SDate))%7, @SDate)
End
Go

Note I changed the dateadd, the parameters order was wrong and the "d," was missing
0
 
LVL 1

Author Comment

by:JackieLee
ID: 10782501
Thanks Hilaire and Billy.  I chose the more complete/better response.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now