Showing posts with label Code Snipplets. Show all posts
Showing posts with label Code Snipplets. Show all posts

Friday, February 18, 2011

Split Function in Sql Server

Input: 
Row Data:  100,200,300,4000
SplitOn : ','

Output as Table
-----------------
Id      Data
-----------
1        100
2        200
3        300
4        4000


ALTER FUNCTION [dbo].[Split]
(
    @RowData nvarchar(2000),
    @SplitOn nvarchar(5)

RETURNS @RtnValue table
(
    Id int identity(1,1),
    Data nvarchar(100)
)
AS 
BEGIN
    Declare @Cnt int
    Set @Cnt = 1


    While (Charindex(@SplitOn,@RowData)>0)
    Begin
        Insert Into @RtnValue (data)
        Select
            Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))


        Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
        Set @Cnt = @Cnt + 1
    End
   
    Insert Into @RtnValue (data)
    Select Data = ltrim(rtrim(@RowData))


    Return
END

Monday, February 7, 2011

Function to convert Html code to proper character.

Input   : it's where ' is a html code of '(oppostrope).
Output : it'z

ALTER FUNCTION [dbo].[HTMLDecode]
(@txt NVARCHAR(4000))
RETURNS NVARCHAR(4000)
AS
BEGIN
DECLARE @Start INT
DECLARE @HTMLCode NVARCHAR(5)
DECLARE @DecodeTxt NVARCHAR(4000)
SET @DecodeTxt = @txt
SET @Start = 32
WHILE @Start<=126
BEGIN
SET @HTMLCode = '&#' + CAST(@Start AS NVARCHAR(5))+';'
SELECT @DecodeTxt=REPLACE(@DecodeTxt,@HTMLCode,CAST(NCHAR(@Start) AS NVARCHAR(5)))
SET @Start = @Start + 1
END
RETURN @DecodeTxt
END

Note:
From 32  to 126 is the ascii character number so for each there is a equvilent character.
For each the character between 32 to 126 there is html value starting as &#32; to &#126; what we are doing is we are replacing this html value to the equivalent character.

For any questions you can mail me..