473,411 Members | 1,949 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,411 software developers and data experts.

Getting specific dates in SQL

I've been looking at some code posted here and have been trying to dissect all the date functions to see what they return but am still having trouble. I'm trying to get the date of the 1st thursday in a given month and if that date falss on either the 1st or 3rd, I need the date of the next thursday.

Example:
Month of May, the 1st thursday is 5/1/2008
I need the next thursday, which is 5/8/2008.

Thanks in advance,
Brian
Jun 25 '08 #1
2 1506
deepuv04
227 Expert 100+
hi,
I tried something which will give you all the dates comes on the give day.

My logic is i have taken the current date into @date,
2. Then taken @stardate as subtracted days from @date.
3. taken @enddate by adding one month to the @startdate.
4. takel the dates which has datename as given day between startdate and enddate.

Expand|Select|Wrap|Line Numbers
  1.  
  2. declare @date datetime,
  3.         @day int
  4.  
  5. declare @startdate datetime,
  6.         @enddate datetime
  7. set @date = getdate()
  8.  
  9. set @day = datepart(day,getDate())-1
  10.  
  11. set @startdate = DATEADD(day,-@day,getDate())
  12. set @enddate =  DATEADD(month,1,@startdate)
  13.  
  14. SELECT Date 
  15. FROM
  16.     ( Select dateadd(day,number,@startdate) AS Date
  17.       from master.dbo.spt_values
  18.       where master.dbo.spt_values.type='p'
  19.             AND dateadd(day,number,@startdate)<=(@enddate)
  20.      ) AS T
  21. WHERE DATENAME(dw, Date) = 'Thursday'
  22.  
  23.  
  24.  
change the datename as 'Sunday', 'Monday' as per the requirement

thanks
Jun 25 '08 #2
Thanks, I think that may actually work. For my project I'm trying to automate the generation of a due date for bills. Since we pay our bills on thursdays I needed to get the next date for the next thursday. With a little modification I can use this to check for the next thursday if I pass it a date.

Thanks again.

hi,
I tried something which will give you all the dates comes on the give day.

My logic is i have taken the current date into @date,
2. Then taken @stardate as subtracted days from @date.
3. taken @enddate by adding one month to the @startdate.
4. takel the dates which has datename as given day between startdate and enddate.

Expand|Select|Wrap|Line Numbers
  1.  
  2. declare @date datetime,
  3.         @day int
  4.  
  5. declare @startdate datetime,
  6.         @enddate datetime
  7. set @date = getdate()
  8.  
  9. set @day = datepart(day,getDate())-1
  10.  
  11. set @startdate = DATEADD(day,-@day,getDate())
  12. set @enddate =  DATEADD(month,1,@startdate)
  13.  
  14. SELECT Date 
  15. FROM
  16.     ( Select dateadd(day,number,@startdate) AS Date
  17.       from master.dbo.spt_values
  18.       where master.dbo.spt_values.type='p'
  19.             AND dateadd(day,number,@startdate)<=(@enddate)
  20.      ) AS T
  21. WHERE DATENAME(dw, Date) = 'Thursday'
  22.  
  23.  
  24.  
change the datename as 'Sunday', 'Monday' as per the requirement

thanks
Jun 26 '08 #3

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

Similar topics

2
by: David Mitchell | last post by:
I have tried using the following code to count the specific number of each weekday but get a compile error "User defined type not defined" which I think relates to the first line of the function: -...
2
by: Jerry | last post by:
Hi, Access 2003. I would like to query specific dates or years. I need to be able to selct the dates in a query, ie aug 12 2004, aug 11th 2004, or 2001, 2002 and 2003. Any help is most appreciated
4
by: jwa6 | last post by:
I have a user/pc specific problem in access. This doesn't occur on any other ( windows xp pc) that I have used the ..mbd on. This involves a query that's using a range of dates as a parm. This...
7
by: No bother | last post by:
I have a table which has, among other fields, a date field. I want to get a count of records where certain criteria are met for, say, three days in a row. For example: NumWidgets Date...
2
by: tausif | last post by:
hi myself using ACCESS as backend i want to retrieve dates from table placedorder to show reports between specific dates during runtime so i wrote query rs.Open "Select * from placedorder ...
3
by: rednarjess | last post by:
Dear All; I have table in vehicle.php file that contains vehicle information(plate number, vehicle model, reg. and exp. dates). I pprovided each record with href(more) if he want to know more about...
0
by: jonasi | last post by:
Hi, I would like to use several colors to display some specific dates in my monthcalendar. By example, some dates will be displayed in red color, others in green color... Have you got any...
1
by: kigoobe | last post by:
Well guys, I am storing data in the mysql which when extracted gives this - Array ( => Array ( => 26 => 2007-11-01 => 16:01:00 ...
1
by: Malcolm Greene | last post by:
The locale module provides the ability to format dates, currency and numbers according to a specific locale. Is there a corresponding module for parsing locale's output to convert locale...
8
by: Innocent2104 | last post by:
Hi there, The script below displays the attached output but as shown, it skips certain days and i need to include these to calculate my avg balance for a certain month, i.e.Nov. How do i update the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.