SQL SERVER TUTORIAL IN DEPTH

The blog is to help users about sql server

Others

LightBlog

Breaking

Tuesday 12 November 2013

How the function is used trickily to get the desired result

ACTUAL DATA 





ALTER FUNCTION [dbo].[fn_Split_month]
(
       @Data varchar(max)
) 
RETURNS varchar(30)
AS 
BEGIN
       Declare @Month varchar(15),@Year varchar(4), @RtnValue varchar(30),@index int
       if(len(@Data)>0 and @Data<>'')
       begin
              SET @index = Charindex(':',@Data)
              SELECT @Month = month_text from tbl_month_master where month = ltrim(rtrim(Substring(@Data,1,Charindex(':',@Data)-1)))

              SET @Year = LTRIM(RTRIM(SUBSTRING(@Data,@index+1,LEN(@Data))))
                    
              set @RtnValue =  @Month + ', ' + @Year
       End
       Else
       begin
              set @RtnValue=''
       End

       Return @RtnValue
END

SELECT [dbo].[fn_Split_month] ('12:2009')


No comments:

Post a Comment

 test