473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Calculating Variable Date in TSQL

I am looking for a TSQL code that would calculate a specific date each month that varies based on the following condition:

I need the result that would return the date of the Thursday after the Last Wednesday of each month.

I'm guessing a code that would be (Last Wednesday of Month) + 1 Day.

My problem is I can find ways to calculate a day within a week, the last day or first day of a month, add/subtract as needed from that. But calculating the last Wednesday of a month I can't figure out how to do.

As an example the dates I would need returned at 1/31/08, 2/28/08, 3/27/08, 5/1/08, 5/29/08 ......

This is my first time here so hopefully this is in the correct place and the correct format. If not please let me know and accept my apoligies from the newbie. I appreciate any advice that may be provided.

Thanks,
Chad
Jan 9 '08 #1
5 8783
iburyak
1,017 Expert 512MB
What I would do is to find first Thursday of the next month and then subtract 1 week from that date or 7 days which is the same.
If you need help let me know.


Good Luck.
Jan 9 '08 #2
Thank you for the reply. However that would not always work. For example the first Thursday after April is May 1st, which is in fact the day I want. For most of the months it would work. But to be honest I don't know the code to figure the first Thursday or the last Wednesday.

I think I need to convert the date of the last day of the month to a day of the week (dw) and then calculate from there.

For example if the last day of the month falls on a Sunday then the Thursday would be that date -3. If the last day of the month falls on a Wednesday I would add 1 day.

the dw datepart conversion is
1=Sunday (-3)
2=Monday (-4)
3=Tuesday (-5)
4=Wednesday (+1)
5=Thursday (+/- 0)
6=Friday (-1)
7=Saturday (-2)

So I "think" I'm getting closer. But would I do an IF statement for those variables?

Thanks,
Chad
Jan 9 '08 #3
iburyak
1,017 Expert 512MB
Try something like this:

Expand|Select|Wrap|Line Numbers
  1. Declare @Date datetime, @FirstOfNextMonth datetime, @FirstWed datetime
  2. --Set @Date = '2008-04-09 17:54:22.650'
  3. Set @Date = getdate()
  4.  
  5. Select @FirstOfNextMonth = dateadd(m, 1, @Date)
  6.  
  7. Select @FirstOfNextMonth = convert(datetime,convert(varchar(10),year(dateadd(m, 1, @Date))) + '-' + convert(varchar(10), month(dateadd(m, 1, @Date))) + '-01 00:00')   
  8.  
  9.  
  10. IF Datepart(dw, @FirstOfNextMonth) > 4  
  11.   BEGIN  
  12.       select @FirstWed = dateadd(ww, 1, dateadd(dd, -1 * (Datepart(dw, @FirstOfNextMonth) - 4),@FirstOfNextMonth))  
  13.   END  
  14. ELSE IF Datepart(dw, @FirstOfNextMonth) < 4  
  15.     select @FirstWed =  dateadd(dd, 4 - Datepart(dw, @FirstOfNextMonth),@FirstOfNextMonth)  
  16. ELSE IF Datepart(dw, @FirstOfNextMonth) = 4  
  17.     select @FirstWed = @FirstOfNextMonth 
  18.  
  19. --Find next Thursday after last Wednesday
  20. select dateadd(dd, -6, @FirstWed)

Good Luck.
Jan 9 '08 #4
ck9663
2,878 Expert 2GB
let me see if i got your requirement right. you need the date following the last wednesday of the previous month. as you said more like:

set GiveMeThisDate = LastWednesdayOfPreviousMonth() + 1

If I'm wrong, don not proceed :)

If I'm right, try this, run this query. try changing the value of the variable @mydate. test for various months.

Expand|Select|Wrap|Line Numbers
  1.  
  2. declare @mydate as datetime
  3.  
  4. set @mydate = '06/09/2008'
  5.  
  6. select DATEADD(mm, DATEDIFF(mm,0,@mydate ), 0) as FirstDayofMonth, 
  7.  
  8.     case 
  9.     when datepart(dw,DATEADD(mm, DATEDIFF(mm,0,@mydate ), 0)) between 1 and 4 then
  10.          dateadd(dd,1,dateadd(dd,(-1 * ((datepart(dw,DATEADD(mm, DATEDIFF(mm,0,@mydate ), 0)) ) + 3)),DATEADD(mm, DATEDIFF(mm,0,@mydate ), 0) ))
  11.     when datepart(dw,DATEADD(mm, DATEDIFF(mm,0,@mydate ), 0)) between 5 and 7 then
  12.          dateadd(dd,1,dateadd(dd,(-1 * ((datepart(dw,DATEADD(mm, DATEDIFF(mm,0,@mydate ), 0)) ) - 4)),DATEADD(mm, DATEDIFF(mm,0,@mydate ), 0) ))
  13.     end as DateIWant
  14.  
  15.  
-- CK
Jan 10 '08 #5
Excellent! I tested several months and they all worked. I will build that into my stored procedure.

Thank you so much!

Chad
Jan 10 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Ron Adam | last post by:
Hi, I'm having fun learning Python and want to say thanks to everyone here for a great programming language. Below is my first Python program (not my first program) and I'd apreciate any...
2
by: Steve | last post by:
Hi; I have been writing a lot of short tsql scripts to fix a lot of tiny database issues. I was wondering if a could make an array of strings in tsql that I could process in a loop, something...
18
by: mountain man | last post by:
Greetings to all database professionals and laymen, Let us make a bold assumption that we have developed a software tool for the SQL Server environment which simply acts as an interface between...
1
by: Tony Williams | last post by:
I have a table with two fields, txtvalue (a number field) and txtmonth ( a date/time field). I want to create a report that shows the difference in value between the value in txtvalue in one value...
2
by: Gustavo G. Rondina | last post by:
It is possible to caclulate every year's easter using simple mathematical operations. Here is a code that does the trick: http://www.brlivre.org/c/easter.c I found the math scheme in an...
1
by: David | last post by:
Hi, I have a problem with returning a value from an external function..... My asp page is basically a list taken from a database. A date record is written from the DB, then all the...
2
by: ncsthbell | last post by:
I am having problems getting the end date to calculate correctly. I start with Quarter '03/02', (YY/QTR), for this it means it is for the 2nd qtr of 2003. My goal is to get the begin & end dates...
25
by: Blaize | last post by:
Hi, I'm having an issue trying to calculate time. Its okay if the value does not exceed 24 hours otherwise I get a date and hours listed. For example, I have a loop which looks through a table...
8
by: =?Utf-8?B?QWw=?= | last post by:
I am working in vb2005. how can I calculate business days (not including holidays and weekends) between 2 dates? thanks Al
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.