Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

Get every Sunday's date?
Hi,

Is any way to get every Sunday's date?
Any function, reference or something else?
Thanks for help!

Angi



Report this thread to moderator Post Follow-up to this message
Old Post
angi
04-07-06 12:24 PM


Re: Get every Sunday's date?
Assuming your SQL Server is set to have Sunday as the first day of the week,
the following will do the trick for all Sundays in 2006:

declare @date datetime

set @date = '1 Jan 2006'

while datepart ( dd, @date ) <> 1

-- Find the next Sunday, OK 1 Jan 2006 was one, so this does nothing

set @date = dateadd ( dd, 1, @date )



-- Now loop till we get to 1 Jan 2007

while @date < '1 Jan 2007'

begin

print @date

set @date = dateadd ( dd, 7, @date )

end



"angi" <angi@news.microsoft.com> wrote in message
news:et04YhjWGHA.5096@TK2MSFTNGP03.phx.gbl...
> Hi,
>
> Is any way to get every Sunday's date?
> Any function, reference or something else?
> Thanks for help!
>
> Angi
>



Report this thread to moderator Post Follow-up to this message
Old Post
Jan Van der Eecken
04-07-06 12:24 PM


Re: Get every Sunday's date?
angi  wrote:
> Hi,
>
> Is any way to get every Sunday's date?
> Any function, reference or something else?
> Thanks for help!
>
> Angi

Use a calendar table:
http://www.aspfaq.com/show.asp?id=2519

SELECT dt
FROM calendar
WHERE dayname = 'Sunday'
AND dt >= '20000101'
AND dt < '21990101' ;

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--


Report this thread to moderator Post Follow-up to this message
Old Post
David Portas
04-07-06 02:23 PM


Re: Get every Sunday's date?
Another way is to use a number table if you don't want to keep a
calendar table

--Create the number table once!
CREATE TABLE NumberPivot (NumberID INT PRIMARY KEY)

DECLARE @intLoopCounter INT
SELECT @intLoopCounter =0

WHILE @intLoopCounter <=10000
BEGIN
INSERT INTO NumberPivot
VALUES (@intLoopCounter)

SELECT @intLoopCounter = @intLoopCounter +1
END
GO


SET DATEFIRST 7 --Just in case!
-- Get Sunday's from 2000-01-01
DECLARE @dtmDate DATETIME
SELECT @dtmDate = '2000-01-01 00:00:00.000'

SELECT
 DATEADD(dd,numberID,
 @dtmDate),DATEPART(d
 w,DATEADD(dd,numberI
D,@dtmDate)),DAT
 ENAME(dw,DATEADD(dd,
numberID,@dtmDate))
FROM dbo.NumberPivot
WHERE NumberID < 10000
AND  DATEPART(dw,DATEADD(
 dd,numberID,@dtmDate
)) =1

or just this, if you know that you start from a Sunday

DECLARE @dtmDate DATETIME
SELECT @dtmDate = '2000-01-02 00:00:00.000'

SELECT  DATEADD(wk,numberID,
@dtmDate)
FROM dbo.NumberPivot
WHERE NumberID < 100


Denis the SQL Menace
http://sqlservercode.blogspot.com/


Report this thread to moderator Post Follow-up to this message
Old Post
SQL
04-07-06 02:23 PM


Re: Get every Sunday's date?
Great link, thanks David. Especially the part about Week of Year. Am facing
the same stupid problem again and again.


"David Portas" < REMOVE_BEFORE_REPLYI
NG_dportas@acm.org> wrote in message
news:1144417384.576842.78840@u72g2000cwu.googlegroups.com...
> angi wrote: 
>
> Use a calendar table:
> http://www.aspfaq.com/show.asp?id=2519
>
> SELECT dt
> FROM calendar
> WHERE dayname = 'Sunday'
>  AND dt >= '20000101'
>   AND dt < '21990101' ;
>
> --
> David Portas, SQL Server MVP
>
> Whenever possible please post enough code to reproduce your problem.
> Including CREATE TABLE and INSERT statements usually helps.
> State what version of SQL Server you are using and specify the content
> of any error messages.
>
> SQL Server Books Online:
> http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
> --
>



Report this thread to moderator Post Follow-up to this message
Old Post
Jan Van der Eecken
04-07-06 04:23 PM


Sponsored Links





Last Thread Next Thread
Post New Thread

MS SQL Server archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 08:08 PM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006