Search
 
SCRIPT & CODE EXAMPLE
 

SQL

generate series sqlserver 2005

/* SqlServer 2005 Standard */
CREATE FUNCTION [dbo].[generate_series] ( 
    @BeginData datetime, 
    @EndDate datetime, 
    @DatePart varchar(32) = '1 day'
 )
RETURNS @Serie TABLE ( item datetime ) AS BEGIN
   declare @arg varchar(32); 
   declare @len integer; 
   declare @pos integer; 
    
   declare @Data datetime;
   declare @DatePartType varchar(11);
   declare @DatePartIncrement integer;
    
   SET @arg = lTRIM(rTRIM(@DatePart));
   SET @pos = CHARINDEX(' ', @arg );
   SET @len = LEN(@arg);
   SET @DatePartIncrement = CAST(LEFT(@arg, @pos) as integer);
   SET @DatePartType = lower(RIGHT(@arg, @len - @pos));
   SET @Data = @BeginData;

   WHILE (@Data <= @EndDate) 
   BEGIN
      INSERT INTO @Serie(item) VALUES( @Data );
      SET @Data = CASE @DatePartType 
      WHEN 'year'        THEN dateadd(year,        @DatePartIncrement, @Data )
      WHEN 'quarter'     THEN dateadd(quarter,     @DatePartIncrement, @Data )
      WHEN 'month'       THEN dateadd(month,       @DatePartIncrement, @Data )
      WHEN 'day'         THEN dateadd(day,         @DatePartIncrement, @Data )
      WHEN 'week'        THEN dateadd(week,        @DatePartIncrement, @Data )
      WHEN 'weekday'     THEN dateadd(weekday,     @DatePartIncrement, @Data )
      WHEN 'hour'        THEN dateadd(hour,        @DatePartIncrement, @Data )
      WHEN 'minute'      THEN dateadd(minute,      @DatePartIncrement, @Data )
      WHEN 'second'      THEN dateadd(second,      @DatePartIncrement, @Data )
      ELSE dateadd( day, @DatePartIncrement, @Data ) END                         
   END;   
   
   RETURN
   -- SELECT * FROM vw_generate_series_TEST
END
/* View de Tests */
ALTER VIEW vw_generate_series_TEST AS
SELECT 
    1 as Test, 
    '2001-01-01 00:00:00.000' as BeginDateArg, 
    '2010-12-31 00:00:00.000' as EndDateArg, 
    '1 year       ' as DatePartArg, 
    10   OK, 
    Count(*) QT, 
    CASE WHEN (10  =Count(*)) THEN '  OK  ' ELSE 'FALHOU' END Status 
FROM lib.dbo.generate_series('2001-01-01 00:00:00.000', '2010-12-31 00:00:00.000', '1 year       ') 
union
SELECT 
    2 as Test, 
    '2010-01-01 00:00:00.000' as BeginDateArg, 
    '2010-12-31 00:00:00.000' as EndDateArg, 
    '1 quarter    ' as DatePartArg,  
    4   OK, 
    Count(*) QT, 
    CASE WHEN ( 4  =Count(*)) THEN '  OK  ' ELSE 'FALHOU' END Status 
FROM lib.dbo.generate_series('2010-01-01 00:00:00.000', '2010-12-31 00:00:00.000', '1 quarter    ') 
union
SELECT 
    3 as Test, 
    '2010-01-01 00:00:00.000' as BeginDateArg, 
    '2010-12-31 00:00:00.000' as EndDateArg, 
    '1 month      ' as DatePartArg, 
    12   OK, 
    Count(*) QT, 
    CASE WHEN (12  =Count(*)) THEN '  OK  ' ELSE 'FALHOU' END Status 
FROM lib.dbo.generate_series('2010-01-01 00:00:00.000', '2010-12-31 00:00:00.000', '1 month      ') 
union
SELECT 
    4 as Test, 
    '2010-01-01 00:00:00.000' as BeginDateArg, 
    '2010-01-31 00:00:00.000' as EndDateArg, 
    '1 day        ' as DatePartArg, 
    31   OK, 
    Count(*) QT, 
    CASE WHEN (31  =Count(*)) THEN '  OK  ' ELSE 'FALHOU' END Status 
FROM lib.dbo.generate_series('2010-01-01 00:00:00.000', '2010-01-31 00:00:00.000', '1 day        ') 
union
SELECT 
    5 as Test, 
    '2010-01-01 00:00:00.000' as BeginDateArg, 
    '2010-01-31 00:00:00.000' as EndDateArg, 
    '1 week       ' as DatePartArg, 
    5    OK, 
    Count(*) QT, 
    CASE WHEN (5   =Count(*)) THEN '  OK  ' ELSE 'FALHOU' END Status 
FROM lib.dbo.generate_series('2010-01-01 00:00:00.000', '2010-01-31 00:00:00.000', '1 week    ') 
union
SELECT 
    6 as Test, 
    '2010-01-01 00:00:00.000' as BeginDateArg, 
    '2010-01-01 23:59:59.000' as EndDateArg, 
    '1 hour       ' as DatePartArg, 
    24   OK, 
    Count(*) QT, 
    CASE WHEN (24  =Count(*)) THEN '  OK  ' ELSE 'FALHOU' END Status 
FROM lib.dbo.generate_series('2010-01-01 00:00:00.000', '2010-01-01 23:59:59.000', '1 hour       ') 
union
SELECT 
    7 as Test, 
    '2010-01-01 23:00:00.000' as BeginDateArg, 
    '2010-01-01 23:59:59.000' as EndDateArg, 
    '1 minute     ' as DatePartArg, 
    60   OK, 
    Count(*) QT, 
    CASE WHEN (60  =Count(*)) THEN '  OK  ' ELSE 'FALHOU' END Status 
FROM lib.dbo.generate_series('2010-01-01 23:00:00.000', '2010-01-01 23:59:59.000', '1 minute     ') 
union
SELECT 
    8 as Test, 
    '2010-01-01 23:00:01.000' as BeginDateArg, 
    '2010-01-01 23:01:00.000' as EndDateArg, 
    '1 second     ' as DatePartArg, 
    60   OK, 
    Count(*) QT, 
    CASE WHEN (60  =Count(*)) THEN '  OK  ' ELSE 'FALHOU' END Status 
FROM lib.dbo.generate_series('2010-01-01 23:00:01.000', '2010-01-01 23:01:00.000', '1 second     ')
Comment

PREVIOUS NEXT
Code Example
Sql :: suse status MySQL 
Sql :: how to connect pgadmin with excel 
Sql :: localhost ERROR 2006 (HY000) at line 1163: MySQL server has gone away 
Sql :: SQL Query Orderby Two or More Columns and by number of characters 
Sql :: Mysql Install Ubuntu with native password 
Sql :: connect google bigquery connect using sqirrel 
Sql :: sql alchemy get last n elements 
Sql :: how to reset mysql table auto_increment 
Sql :: can we compare two timestamps in mysql 
Sql :: sql to move data from one table to another 
Sql :: plsql regex match whole word 
Sql :: select-groups-of-row-only-having-specific-value 
Sql :: Failed to initialize, mariadb service is unhealthy. 
Sql :: ORA-01400 
Sql :: how to type a blank discord messgae 
Sql :: reset sql primary key 
Sql :: laravel How to find the password of mysql on desktop docker in laravel 
Sql :: mysql max number not returning correct value 
Sql :: how to check if sql query is correct online 
Sql :: insert thai language sql server 
Sql :: sqlite ignore index 
Sql :: database schema for mcqs type exam in sql 
Sql :: sql server o arquivo solicitado não pode ser baixado 
Sql :: sql truncate number 
Sql :: % Wildcard in SQL 
Sql :: stored procedure vs view 
Sql :: pl/sql how to delete a trigger 
Sql :: multiple tricky query in sql server 
Csharp :: lockstate untiy 
Csharp :: regions unity 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =