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
5 8783
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.
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
Try something like this: - Declare @Date datetime, @FirstOfNextMonth datetime, @FirstWed datetime
-
--Set @Date = '2008-04-09 17:54:22.650'
-
Set @Date = getdate()
-
-
Select @FirstOfNextMonth = dateadd(m, 1, @Date)
-
-
Select @FirstOfNextMonth = convert(datetime,convert(varchar(10),year(dateadd(m, 1, @Date))) + '-' + convert(varchar(10), month(dateadd(m, 1, @Date))) + '-01 00:00')
-
-
-
IF Datepart(dw, @FirstOfNextMonth) > 4
-
BEGIN
-
select @FirstWed = dateadd(ww, 1, dateadd(dd, -1 * (Datepart(dw, @FirstOfNextMonth) - 4),@FirstOfNextMonth))
-
END
-
ELSE IF Datepart(dw, @FirstOfNextMonth) < 4
-
select @FirstWed = dateadd(dd, 4 - Datepart(dw, @FirstOfNextMonth),@FirstOfNextMonth)
-
ELSE IF Datepart(dw, @FirstOfNextMonth) = 4
-
select @FirstWed = @FirstOfNextMonth
-
-
--Find next Thursday after last Wednesday
-
select dateadd(dd, -6, @FirstWed)
Good Luck.
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. -
-
declare @mydate as datetime
-
-
set @mydate = '06/09/2008'
-
-
select DATEADD(mm, DATEDIFF(mm,0,@mydate ), 0) as FirstDayofMonth,
-
-
case
-
when datepart(dw,DATEADD(mm, DATEDIFF(mm,0,@mydate ), 0)) between 1 and 4 then
-
dateadd(dd,1,dateadd(dd,(-1 * ((datepart(dw,DATEADD(mm, DATEDIFF(mm,0,@mydate ), 0)) ) + 3)),DATEADD(mm, DATEDIFF(mm,0,@mydate ), 0) ))
-
when datepart(dw,DATEADD(mm, DATEDIFF(mm,0,@mydate ), 0)) between 5 and 7 then
-
dateadd(dd,1,dateadd(dd,(-1 * ((datepart(dw,DATEADD(mm, DATEDIFF(mm,0,@mydate ), 0)) ) - 4)),DATEADD(mm, DATEDIFF(mm,0,@mydate ), 0) ))
-
end as DateIWant
-
-
-- CK
Excellent! I tested several months and they all worked. I will build that into my stored procedure.
Thank you so much!
Chad
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
| |