You must Sign In to post a response.
  • Category: SQL Server

    How to write a sql function to include single quote in string ?


    Are you looking for a way to write a sql function to include single quote in string ? then read this thread to know how to write it



    if i pass this string as parameter 1,3,5,6

    i need to generate the string like this in Sql Functions

    i need to get the expected result should be like this

    ('1','3','5','6')
  • #736568

    Hi Prasad,

    Try this below function..


    CREATE FUNCTION DBO.DNSPrasad(@INPUT VARCHAR(50))
    RETURNS VARCHAR(50)AS
    BEGIN

    DECLARE @A VARCHAR(50)

    SET @A=(SELECT '('''+REPLACE(@INPUT,',',''',''')+''')')

    RETURN @A

    END

    SELECT DBO.DNSPrasad('1,3,5,6')




    Hope this helps you!if it is means mark this post!!

    Regards,
    Sri

  • #736569

    Hi Prasad,

    You can get your expected output without using function by this simple query..



    DECLARE @A VARCHAR(50)
    SET @A='1,3,5,6'
    SELECT '('''+REPLACE(@A,',',''',''')+''')'


    Hope this helps you!if it is means mark this post!!

    Regards,
    Sri

  • #736580
    You can try this too..

    Create function dbo.TestFn(@Input varchar(20))
    RETURNS VARCHAR(50)AS
    Begin
    set @Input = '('''+ REPLACE(@Input,',',''',''') +')'
    Return @Input
    End

    --select dbo.TestFn('1,3,5,6')

    (OR)

    declare @var varchar(20) = '1,3,5,6'
    select @var

    set @var = '('''+ REPLACE(@var,',',''',''') +')'
    select @var

    Regards,
    Manick


  • Sign In to post your comments