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.
| 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 sqlMonth Days1 312 28 or 293 31…12 31also, i need to get something that results like this0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181up to 10000Thank 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; endSELECT * FROM @table;Devart,SQL Server Tools:dbForge Schema ComparedbForge Data ComparedbForge Query Builder |
 |
|
|
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. |
 |
|
|
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. |
 |
|
|
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 |
 |
|
|
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 1WHILE (SELECT COUNT(*) FROM @Fibonacci) < 20BEGIN INSERT @Fibonacci (Number) SELECT SUM(Number) FROM (SELECT TOP 2 Number FROM @Fibonacci ORDER BY Number DESC) AS XENDSELECT * FROM @Fibonacci |
 |
|
|
|
|
|
|
|