Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help with Loops in SQL

Author  Topic 

KcinMan11358
Starting Member

4 Posts

Posted - 2010-11-29 : 10:29:03
Hey guys,
I need some help in getting this to work in SQL. Basically, I need to use loops and if statements. Here is what I need to get printed in sql

Month Days
1 31
2 28 or 29
3 31

12 31

also, i need to get something that results like this
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
up to 10000
Thank alot

Devart
Posting Yak Master

102 Posts

Posted - 2010-11-29 : 11:00:56
For example:

DECLARE @i int;
DECLARE @ix varchar(255);
DECLARE @table table (id_month int,day_count varchar(10))
SET @i=1;
while (cast(@i as int)<=12)
begin
set @ix = case when @i<10 then '0'+cast(@i as varchar(20)) else cast(@i as varchar(20)) end;
insert INTO @table
select
@i,
cast(day(dateadd(dd,-1,dateadd(MM,1,'2010'+@ix+'01'))) as VARCHAR(10)) + CASE WHEN @i=2 THEN ' or 29' ELSE '' END;

SET @i=@i+1;
end

SELECT * FROM @table;

Devart,
SQL Server Tools:
dbForge Schema Compare
dbForge Data Compare
dbForge Query Builder
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-11-29 : 11:29:02
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=30761


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-11-29 : 11:31:18
My last post was about your question for fibonacci but you have deleted that part of your question:
Edited by - KcinMan11358 on 11/29/2010 11:29:17




No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

KcinMan11358
Starting Member

4 Posts

Posted - 2010-11-29 : 11:35:21
Cheers Devart!!!

@webfred, looks like i am not the only one who wanted this solved lol
Go to Top of Page

KcinMan11358
Starting Member

4 Posts

Posted - 2010-11-29 : 11:40:23
Lol, sorry webfred.

By any chance do you how to limit so the result is less than 10000?
My answer is:
DECLARE @Fibonacci TABLE (Number INT NOT NULL)
INSERT @Fibonacci (Number) SELECT 0 UNION ALL SELECT 1

WHILE (SELECT COUNT(*) FROM @Fibonacci) < 20
BEGIN
INSERT @Fibonacci (Number)
SELECT SUM(Number)
FROM (SELECT TOP 2 Number FROM @Fibonacci ORDER BY Number DESC) AS X
END
SELECT * FROM @Fibonacci
Go to Top of Page
   

- Advertisement -