TheBugSlayer
Programmer
I have the need to generate all combinations of hours, minutes and seconds for an audit viewer app. I love CTEs so I came up with this:
and challenged my coworkers to suggest equivalent or better. One of them came up with this:
which is much simpler IMO and does not require understanding of recursice Common Table Expressions.
There is someone on this forum who LOVES number tables...probaby George, SQLBill and the other guy whose name I forget
Do you know any other way?
Regards.
MCP SQL Server 2000, MCTS SQL Server 2005, MCTS SQL Server 2008 (DBD, DBA)
Code:
WITH CTE_Hours
AS
(
SELECT 0 AS HourCol
UNION ALL
SELECT HourCol + 1
FROM CTE_Hours
WHERE HourCol + 1 <= 23
)
,
CTE_Minutes
AS
(
SELECT 0 AS MinuteCol
UNION ALL
SELECT MinuteCol + 1
FROM CTE_Minutes
WHERE MinuteCol + 1 <= 59
)
SELECT HourCol, CTE_Minutes.MinuteCol, Sec.MinuteCol AS SecondCol
FROM CTE_Hours, CTE_Minutes, CTE_Minutes AS Sec
ORDER BY HourCol, MinuteCol, SecondCol
and challenged my coworkers to suggest equivalent or better. One of them came up with this:
Code:
SELECT h.number Hours,
m.number Minutes,
s.number Seconds
FROM master..spt_values h
CROSS JOIN master..spt_Values m
CROSS JOIN master..spt_values s
WHERE h.number < 24
AND s.number < 60
AND m.number < 60
AND m.type = 'p'
AND s.type = 'p'
AND h.type = 'p'
ORDER BY h.number ,
m.number ,
s.number
There is someone on this forum who LOVES number tables...probaby George, SQLBill and the other guy whose name I forget
Do you know any other way?
Regards.
MCP SQL Server 2000, MCTS SQL Server 2005, MCTS SQL Server 2008 (DBD, DBA)