Search
 
SCRIPT & CODE EXAMPLE
 

SQL

generate series sybase

/* Sybase 9 / Sql Anyware */
CREATE PROCEDURE generate_series(in @BeginData DateTime, in @EndDate DateTime, in @DatePart varchar(32) DEFAULT '1 day')
RESULT (item Datetime)
BEGIN
   DECLARE LOCAL TEMPORARY TABLE @Serie (
      item Datetime
   )ON COMMIT PRESERVE ROWS;
    
   declare @arg varchar(32); 
   declare @len integer; 
   declare @pos integer; 
    
   declare @Data DateTime;
   declare @DatePartType varchar(11);
   declare @DatePartIncrement integer;
    
   SET @arg = TRIM(@DatePart);
   SET @pos = CHARINDEX(' ', @arg );
   SET @len = LENGTH(@arg);
   SET @DatePartIncrement = CAST(LEFT(@arg, @pos) as integer);
   SET @DatePartType = RIGHT(@arg, @len - @pos);
   SET @Data = @BeginData;

   WHILE (@Data <= @EndDate) LOOP
      print('Data ' ||  @Data);
      INSERT INTO @Serie(item) VALUES( @Data );
      SET @Data = CASE 100 
      WHEN SIMILAR( @DatePartType, 'year' ) THEN dateadd(year , @DatePartIncrement, @Data )
      WHEN SIMILAR( @DatePartType, 'quarter' ) THEN dateadd(quarter , @DatePartIncrement, @Data )
      WHEN SIMILAR( @DatePartType, 'month' ) THEN dateadd(month , @DatePartIncrement, @Data )
      WHEN SIMILAR( @DatePartType, 'day' ) THEN dateadd(day , @DatePartIncrement, @Data )
      WHEN SIMILAR( @DatePartType, 'week' ) THEN dateadd(week, @DatePartIncrement, @Data )
      WHEN SIMILAR( @DatePartType, 'hour' ) THEN dateadd(hour , @DatePartIncrement, @Data )
      WHEN SIMILAR( @DatePartType, 'minute' ) THEN dateadd(minute , @DatePartIncrement, @Data )
      WHEN SIMILAR( @DatePartType, 'second' ) THEN dateadd(second , @DatePartIncrement, @Data )
      WHEN SIMILAR( @DatePartType, 'millisecond' ) THEN dateadd(millisecond , @DatePartIncrement, @Data )
      ELSE dateadd( day, @DatePartIncrement, @Data ) END                         
   END LOOP;   
    
   SELECT * FROM @Serie;
   -- SELECT * FROM vw_generate_series_TEST
END;
/* View com Testes */
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 (OK=QT) THEN '  OK  ' ELSE 'FALHOU' END Status FROM generate_series(BeginDateArg, EndDateArg, DatePartArg) A 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 (OK=QT) THEN '  OK  ' ELSE 'FALHOU' END Status FROM generate_series(BeginDateArg, EndDateArg, DatePartArg) A 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 (OK=QT) THEN '  OK  ' ELSE 'FALHOU' END Status FROM generate_series(BeginDateArg, EndDateArg, DatePartArg) A 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 (OK=QT) THEN '  OK  ' ELSE 'FALHOU' END Status FROM generate_series(BeginDateArg, EndDateArg, DatePartArg) A 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 (OK=QT) THEN '  OK  ' ELSE 'FALHOU' END Status FROM generate_series(BeginDateArg, EndDateArg, DatePartArg) A 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 (OK=QT) THEN '  OK  ' ELSE 'FALHOU' END Status FROM generate_series(BeginDateArg, EndDateArg, DatePartArg) A 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 (OK=QT) THEN '  OK  ' ELSE 'FALHOU' END Status FROM generate_series(BeginDateArg, EndDateArg, DatePartArg) A 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 (OK=QT) THEN '  OK  ' ELSE 'FALHOU' END Status FROM generate_series(BeginDateArg, EndDateArg, DatePartArg) A union
SELECT 9 as Test, '2010-01-01 23:00:00.001' as BeginDateArg, '2010-01-01 23:00:01.000' as EndDateArg, '1 millisecond' as DatePartArg, 1000 OK, Count(*) QT, CASE WHEN (OK=QT) THEN '  OK  ' ELSE 'FALHOU' END Status FROM generate_series(BeginDateArg, EndDateArg, DatePartArg) A 
ORDER BY 1 
Comment

PREVIOUS NEXT
Code Example
Sql :: mysqkldump devilbox 
Sql :: hidden error sql codeigniter 3 
Sql :: sqlyog clear cache query 
Sql :: oracle compile trigger 
Sql :: check if table is Temporal 
Sql :: mssql replace first occurrence 
Sql :: find most frequent word in sql server 
Sql :: show database not empty tables postgres 
Sql :: calculer pourcentage mysql 
Sql :: sqlcmd no headers 
Sql :: many to many flask-sqlalchemy 
Sql :: sqlplus change user 
Sql :: uuid sqlalcomany 
Sql :: create fulltext index mysql 
Sql :: Syntax error or access violation: 1075 Incorrect table def inition; there can be only one auto column and it must be defined as a key 
Sql :: sql revert migration 
Sql :: mysql even numbers 
Sql :: linux upload database to mysql 
Sql :: postgresql alter column data type from integer to integer array 
Sql :: mysql calcular idade 
Sql :: SQL Comments Within Statements 
Sql :: find in set in postgresql 
Sql :: oracle cache matching 
Sql :: mamp mysql password 
Sql :: show create table in postgresql 
Sql :: sql query for displaying age from oldest to youngest 
Sql :: creating database with - 
Sql :: sql update subtract value 
Sql :: arithmetic expression in sql 
Sql :: SQL Add Multiple Columns in a Table 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =