Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

SQL query puzzle

 
   Database Help (Home) -> Programming RSS
Next:  Drop down list  
Author Message
sqlman

External


Since: Nov 26, 2008
Posts: 2



(Msg. 1) Posted: Wed Nov 26, 2008 12:58 pm
Post subject: SQL query puzzle
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Here is a puzzle-like task I am trying to solve:

Get SQL SELECT statement which will give a list of all dates in a
given year. Optionally: only include Mondays.

Any ideas?

Thanks!

 >> Stay informed about: SQL query puzzle 
Back to top
Login to vote
David Portas1

External


Since: Aug 26, 2004
Posts: 37



(Msg. 2) Posted: Wed Nov 26, 2008 1:13 pm
Post subject: Re: SQL query puzzle [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 26 Nov, 20:58, sqlman wrote:
> Here is a puzzle-like task I am trying to solve:
>
> Get SQL SELECT statement which will give a list of all dates in a
> given year.  Optionally: only include Mondays.
>
> Any ideas?
>
> Thanks!


SELECT dt
FROM Calendar
WHERE dt >= '20080101' AND dt < '20090101';

SELECT dt
FROM Calendar
WHERE dayofweek = 'Monday'
AND dt >= '20080101' AND dt < '20090101';

--
David Portas

 >> Stay informed about: SQL query puzzle 
Back to top
Login to vote
Mikhail Berlyant

External


Since: Jan 14, 2008
Posts: 26



(Msg. 3) Posted: Wed Nov 26, 2008 1:36 pm
Post subject: Re: SQL query puzzle [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In 2005, 2008

declare @y char(4), @d varchar(20)
select @y = '2008', @d = 'Monday'

;with days as (
select 0 as d union all
select d + 1 from days where d < 370
)
select convert(varchar(10), dateadd(d, d, @y + '0101'), 101) from days
where year(dateadd(d, d, @y + '0101')) = @y
and datename(dw,dateadd(d, d, @y + '0101')) = @d
option (maxrecursion 400);

Mikhail

"sqlman" wrote in message

> Here is a puzzle-like task I am trying to solve:
>
> Get SQL SELECT statement which will give a list of all dates in a
> given year. Optionally: only include Mondays.
>
> Any ideas?
>
> Thanks!
 >> Stay informed about: SQL query puzzle 
Back to top
Login to vote
Bob

External


Since: Feb 08, 2005
Posts: 182



(Msg. 4) Posted: Wed Nov 26, 2008 1:59 pm
Post subject: RE: SQL query puzzle [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

If you don't have a calendar table you do this in SQL2005:
DECLARE @year CHAR(4)

SET @year = 2007

;WITH cte AS
(
SELECT 0 x, CAST( @year + '0101' AS DATETIME ) y
UNION ALL
SELECT x + 1, DATEADD( day, x, @year + '0101' )
FROM cte
WHERE x < 365 + CASE WHEN @year % 4 = 0 THEN 0 ELSE -1 END
)
SELECT x, y, DATENAME( weekday, x ) z, DATEPART( weekday, x ) a
FROM cte
OPTION ( MAXRECURSION 366 )
GO

I'll leave you to do the optional bit as I don't like Mondays ;p
 >> Stay informed about: SQL query puzzle 
Back to top
Login to vote
sqlman

External


Since: Nov 26, 2008
Posts: 2



(Msg. 5) Posted: Wed Nov 26, 2008 2:30 pm
Post subject: Re: SQL query puzzle [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Wed, 26 Nov 2008 13:13:39 -0800 (PST), David Portas
wrote:

>On 26 Nov, 20:58, sqlman wrote:
>> Here is a puzzle-like task I am trying to solve:
>>
>> Get SQL SELECT statement which will give a list of all dates in a
>> given year.  Optionally: only include Mondays.
>>
>> Any ideas?
>>
>> Thanks!
>
>
>SELECT dt
>FROM Calendar
>WHERE dt >= '20080101' AND dt < '20090101';
>
>SELECT dt
>FROM Calendar
>WHERE dayofweek = 'Monday'
>AND dt >= '20080101' AND dt < '20090101';

Indeed, quite easy if you have a Calendar table. Thanks!

Although I'd prefer to use the following instead:

SELECT dt
FROM Calendar
WHERE Y = 2008

SELECT dt
FROM Calendar
WHERE dayofweek = 'Monday'
AND Y = 2008
 >> Stay informed about: SQL query puzzle 
Back to top
Login to vote
Raymond D'Anjou

External


Since: Apr 11, 2008
Posts: 18



(Msg. 6) Posted: Wed Nov 26, 2008 4:33 pm
Post subject: Re: SQL query puzzle [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

If you're wondering where David's table called Calendar is coming from,
follow this link...
http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-...auxilia
You can adapt the example table to your needs.

"sqlman" wrote in message

> Here is a puzzle-like task I am trying to solve:
>
> Get SQL SELECT statement which will give a list of all dates in a
> given year. Optionally: only include Mondays.
>
> Any ideas?
>
> Thanks!
 >> Stay informed about: SQL query puzzle 
Back to top
Login to vote
Kevin

External


Since: Jan 11, 2008
Posts: 53



(Msg. 7) Posted: Fri Dec 04, 2009 6:04 am
Post subject: RE: SQL query puzzle [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

You can also do this if you have no calendar table:

DECLARE @year char(4)
SET @year = 2007

SELECT cast(@year as datetime) + spt.Number
FROM master..spt_values spt
WHERE spt.Type='P' and year(cast(@year as datetime) + spt.Number) = @year

"Bob" wrote:

> If you don't have a calendar table you do this in SQL2005:
> DECLARE @year CHAR(4)
>
> SET @year = 2007
>
> ;WITH cte AS
> (
> SELECT 0 x, CAST( @year + '0101' AS DATETIME ) y
> UNION ALL
> SELECT x + 1, DATEADD( day, x, @year + '0101' )
> FROM cte
> WHERE x < 365 + CASE WHEN @year % 4 = 0 THEN 0 ELSE -1 END
> )
> SELECT x, y, DATENAME( weekday, x ) z, DATEPART( weekday, x ) a
> FROM cte
> OPTION ( MAXRECURSION 366 )
> GO
>
> I'll leave you to do the optional bit as I don't like Mondays ;p
>
>
 >> Stay informed about: SQL query puzzle 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Please help me with the sql puzzle query - create table demo(id int) go insert into demo select 10 &#117;nion all select 20 &#117;nion all select 30 &#117;nion all select 40 &#117;nion all select 50 &#117;nion all go i need the output like the following... id -- 10 30 60 100...

SQL Puzzle - Here is a p&#118;zzle. Example Create Table #Temp1(lngDataID int, chrString1 varchar(80)) INSERT #TEMP1 SELECT 1,'ABCDDCBAABCDDCBABCDDCCDDCBAABCDDCBAABCDDCBABCDDCCDDCBABCDDCBAABCDDCBABCDDCCDDCBA' &#85;NION SELECT..

A small puzzle - Hey I'm abit stuck at this problem and could use some help. I'm trying to find 2 queries. (note that 2006-09-06 shouldnt be listed since its a range 2006-09-01 and 2006-09-05 in desired result) also note that C will not be displayed in either result..

Small puzzle - Hello Im having a small problem with finding numbers in a sequence depending on a "flag". Not sure howto explain it, but I'm trying to find the lowest missing number in sequence, IF the flag is 1, else find the next value where flag = 1. Note...

Index puzzle - Hi all. I have 2 identical queries (simple ones). Then only difference is that I replaced the hardcoded F_KundeID with a declared integer. Also: One query with hardcoded value. One query using a variable @F_KundeID (integer). The executionplan are..
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]