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

How to get Last Thursday Date

rsrinivasan
221 100+
Hi all,

I need a query that will return last thursday date dynamically. Can any one send it to me?

Thanks in advance,
Jun 26 '08 #1
1 12269
deepuv04
227 Expert 100+
Hi,
If you are looking for the last Thursday
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  
  4. declare @date datetime,
  5.         @day int
  6.  
  7.  
  8.  
  9. declare @startdate datetime,
  10.  
  11.         @enddate datetime
  12.  
  13. --set @date = getdate()
  14.  
  15.  
  16.  
  17. set @day = datepart(day,getDate())-1
  18.  
  19.  
  20.  
  21. set @startdate = DATEADD(day,-30,getDate())
  22.  
  23. set @enddate =  getdate()
  24.  
  25.  
  26.  
  27. SELECT TOP 1 Date
  28.  
  29. FROM
  30.  
  31.     ( Select dateadd(day,number,@startdate) AS Date
  32.  
  33.       from master.dbo.spt_values
  34.  
  35.       where master.dbo.spt_values.type='p'
  36.  
  37.             AND dateadd(day,number,@startdate)<=(@enddate)
  38.  
  39.      ) AS T
  40.  
  41. WHERE DATENAME(dw, Date) = 'Thursday' AND
  42.       Date <= GetDate() 
  43. ORDER BY Date desc
  44.  
If you are looking for last Thursday in a month:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  
  4. declare @date datetime,
  5.         @day int
  6.  
  7.  
  8.  
  9. declare @startdate datetime,
  10.  
  11.         @enddate datetime
  12.  
  13. set @date = getdate()
  14.  
  15.  
  16.  
  17. set @day = datepart(day,@date)-1
  18.  
  19.  
  20.  
  21. set @startdate = DATEADD(day,-@day,@date)
  22.  
  23. set @enddate =  DATEADD(month,1,@startdate)
  24.  
  25.  
  26.  
  27. SELECT TOP 1 Date
  28.  
  29. FROM
  30.  
  31.     ( Select dateadd(day,number,@startdate) AS Date
  32.  
  33.       from master.dbo.spt_values
  34.  
  35.       where master.dbo.spt_values.type='p'
  36.  
  37.             AND dateadd(day,number,@startdate)<=(@enddate)
  38.  
  39.      ) AS T
  40.  
  41. WHERE DATENAME(dw, Date) = 'Thursday' 
  42.  
  43. ORDER BY Date desc
  44.  
thanks
Jun 26 '08 #2

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

Similar topics

11
by: Dennis Marks | last post by:
There seems to be a major program with the automatic display of the last modified date. Using the javascript "document.lastModified" sometimes returns the correct date and sometimes 1 Jan 1970...
3
by: yxq | last post by:
Hello, The XP Desktop clean wizard can get the last access time of desktop shortcut, i found that the info come from ...
5
by: Steel | last post by:
Hi at all it is the first time that I use PHP and I need only to modify the last modified date of a file I maked a FIRST little script like this: $FileName="myfile.txt"; $Today...
4
by: IdleBrain | last post by:
Hello All, Is it possible to obtain the last Modified date for the source code from within the application? If yes, please let me know how it is done.
3
by: MarcJoseph | last post by:
I have a database that is shared my multiple users who enter and update records on a weekly basis. Is there a way I can add a field to my main data table that will automatically generate the...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I format the Last-Modified date with javascript?...
7
by: mariyana | last post by:
How can we get the last accessed date and the last modified date of a file using VB6
1
by: myemail.an | last post by:
Hi all, I am a novice to Access, and was wondering if I could get some help on a problem I can't solve. I have a database with customer payments, structured like this: customer code type of...
5
by: perhapscwk | last post by:
How to update stored cookies value without change the cookies expired date? when I login on monday, I have use "remember me for 1 week" to store my login in cookies with my last login...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.