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
 generate time table (time and date)

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_day

anyone see an error? :D


declare @int int
declare @hour int

set @int = 15 --230
set @hour = 24

while @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 - 1
end

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 d
WHERE ISDATE([y-m-d]) = 1
ORDER BY [y-m-d-h][/code]


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2008-10-15 : 17:43:59
thank you peso. nice coding

p.s.: whad does sp_values hold? what is idea of this stored procedure and why you used it?

thank you
Go to Top of Page

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 Function
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=47685


You 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_DATE
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61519


CODO ERGO SUM
Go to Top of Page
   

- Advertisement -