Gerrit Mantel
Programs SQL_TABLE_VALUED_FUNCTION - fun_time_array

Main program

Namefun_time_array
Type_descSQL_TABLE_VALUED_FUNCTION
CommentSQLServer User-Defined Function

PURPOSE
Return time array on a given date with variable interval.

Interval is defined by @unit and @interval

This function can be combined with function fun_date_array.
It is possible to get an array with dates + time interval.
See example with CROSS APPLY dbo.fun_time_array.

PARAMETERS
@date (DATETIME) is the date where array is on.

@unit (VARCHAR(20)) can be:
'Hour' or 'Hh' means Hour
'Minute','n' or 'mi' means Minute
'Second','s' or'ss' means Second
all other means Hour

@interval (INT) must be positive.
Values < 1 means 1.

@array (value DATETIME) is output table.

EXAMPLES
SELECT * FROM dbo.fun_time_array(GETDATE(),'Hour', 1);
SELECT * FROM dbo.fun_time_array(GETDATE(),'Minute', 15);
SELECT * FROM dbo.fun_time_array(GETDATE(),'Second', 30);

--Give an array with every hour per day for all dates in current month:
SELECT t2.value
FROM dbo.fun_date_array('Day', 'Month') t1
CROSS APPLY dbo.fun_time_array(t1.value,'Hour', 1) t2;

HISTORY
2015-12-02 - Created function
2016-10-13 - Added an example of CROSS APPLY with function fun_date_array
2024-02-09 - Changed history table

TAGS
<program>
<description>Return time array for a given date with variable interval</description>
<generic>1</generic>
<author>Gerrit Mantel</author>
<created>2015-12-02</created>
<lastmodified>2024-02-09</lastmodified>
</program>
DescriptionReturn time array for a given date with variable interval
Minversion
Generic1
AuthorGerrit Mantel
Created2015-12-02 00:00:00
Lastmodified2024-02-09 00:00:00
Xp_cmdshell0
Ole_automation0
Subprogram_cnt0
Sourcecode