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 |
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2008-10-15 : 06:17:21
|
hi,this code is stuck only on one day, whereas i want to make it for whole year. year, month, day, hour_of_dayanyone see an error? :Ddeclare @int intdeclare @hour intset @int = 15 --230set @hour = 24while @int >= -1 -- -600 begin while @hour >= 0 --<= 24 and > 0 begin --insert into s_cas select left(convert(smalldatetime, getdate()- @int, 20), 11) as date ,year(getdate()- @int) as y ,month(getdate()- @int) as m ,day(getdate()- @int) as d ,datediff(week, dateadd(year, datediff(year, 0, (getdate()-@int)), 0), getdate()-@int+6) as w ,@hour as h ,cast(year(getdate()-@int)as nvarchar(4)) + '-' +cast(month(getdate()-@int) as nvarchar(2)) + '-' +cast(day(getdate()-@int) as nvarchar(2)) + '-' + cast(@hour as nvarchar(2)) as [y-m-d-h] set @hour = @hour - 1 end set @int = @int - 1end |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-10-15 : 07:45:10
|
[code]SELECT STUFF(CONVERT(CHAR(12), CAST([y-m-d] AS DATETIME), 107), 7, 1, '') AS [date], y, m, d, 1 + (24 * DATEPART(DAYOFYEAR, CAST([y-m-d] AS DATETIME)) + CASE h WHEN 24 THEN 23 ELSE h END) / 168 AS w, h, [y-m-d] + [-h] AS [y-m-d-h]FROM ( SELECT DATEPART(YEAR, GETDATE()) AS y, m.Number AS m, d.Number AS d, h.Number AS h, DATENAME(YEAR, GETDATE()) + '-' + REPLACE(STR(m.Number, 2), ' ', '0') + '-' + REPLACE(STR(d.Number, 2), ' ', '0') AS [y-m-d], '-' + REPLACE(STR(h.Number, 2), ' ', '0') AS [-h] FROM master..spt_values AS m INNER JOIN master..spt_values AS d ON d.Type = 'P' INNER JOIN master..spt_values AS h ON h.Type = 'P' WHERE m.Type = 'P' AND m.Number BETWEEN 1 AND 12 AND d.Number BETWEEN 1 AND 31 AND h.Number BETWEEN 0 AND 24 ) AS dWHERE ISDATE([y-m-d]) = 1ORDER BY [y-m-d-h][/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2008-10-15 : 17:43:59
|
| thank you peso. nice codingp.s.: whad does sp_values hold? what is idea of this stored procedure and why you used it?thank you |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2008-10-15 : 18:28:07
|
| spt_values is a system table, not a stored procedure.Peter is using spt_values as an easy way to generate the sequential numbers for months, days and hours.Another way to generate the numbers would be to use the function on this link:Number Table Functionhttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=47685You could also use the function on this link to generate the date part of you table and cross join it with the values 0 to 23 to create the time part.Date Table Function F_TABLE_DATEhttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61519CODO ERGO SUM |
 |
|
|
|
|
|
|
|