Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

SQL-CTE and IF clause in Function

$
0
0
Hi Experts ,
I have create function which works fine .
But I need to modify it ,which comes to me as a challenge .
 

Below is my Function Which works

CREATE FUNCTION RETURN_DATES1 (@Date_part VARCHAR(10),@ST_DT DATE ,@END_DT DATE)
RETURNS  TABLE
AS
  RETURN
  (  
                                 WITH CTE AS
                                   ( 
                                                 SELECT @ST_DT ST_DATE,@END_DT END_DT,DATEADD(DD,1,@ST_DT)DTS
                                                  UNION ALL
                                                SELECT @ST_DT,@END_DT,DATEADD(DD,1,DTS)FROM CTE WHERE DTS !=@END_DT
                                   )
                   SELECT @ST_DT DTS
                   UNION ALL
                   SELECT  DTS FROM CTE  
 )
/*  
SELECT * FROM DBO.RETURN_DATES1 ('D','2010-01-01','2014-01-10')option (maxrecursion 0)
*/
/*Now I need to Modify This ..
If @date_part='D' then above mentioned CTE ,If @date_part='yyyy' then I want to use below CTE i.e 
 WITH CTE AS
                                   ( 
                                                 SELECT @ST_DT ST_DATE,@END_DT END_DT,DATEADD(YYYY,1,@ST_DT)DTS
                                                  UNION ALL
                                                SELECT @ST_DT,@END_DT,DATEADD(YYYY,1,DTS)FROM CTE WHERE DTS !=@END_DT
                                   )
                   SELECT year(@ST_DT) DTS
                   UNION ALL
                   SELECT  year(DTS) FROM CTE 
*/
/*How can I make this Change in aboveFunction,How can I use If in this,I tried but someSyntex error*/help ?



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>