2

Thanks to advise me for the below issue:

I am using below query to fetch the value of a column:

Select OptionList = case  when isnull(AS_CIS_Code,'') <> '' then AS_CIS_Code   
   else ''  
   end 
from added_services

AS_CIS_Code column is of varchar(10) in added_services table. It contains values like 'AB', 'ABC', 'GHKIK', 'UYTIOPJ' and so on which represents different codes.

Now I have to select these codes after modifying the above query so that '_' is appended after each character.

Like it should be fetched as 'A_B_', 'A_B_C_', 'G_H_K_I_K_', 'U_Y_T_I_O_P_J_'.

How should I implement it? Using a temp table will down the performance for one column only, so should I use while loop or please suggest me better alternatives.

2
  • Can't you do it in UI? Commented Jul 24, 2014 at 8:55
  • No I have to do it in DB itself, I am sending this value using view to another application. Commented Jul 24, 2014 at 9:05

4 Answers 4

3

Try this:

DECLARE @Input VARCHAR(100) = 'TESTING'
DECLARE @Pos INT = LEN(@Input)
WHILE @Pos > 1
BEGIN

    SET @Input = STUFF(@Input,@Pos,0,'_')
    SET @Pos = @Pos - 1
END

SELECT @Input

Output

T_E_S_T_I_N_G

UDF

CREATE FUNCTION PadStr(@Data VARCHAR(100)) RETURNS VARCHAR(200)
AS
BEGIN
    DECLARE @Input VARCHAR(200) = @Data 
    DECLARE @Pos INT = LEN(@Input)
    WHILE @Pos > 1
    BEGIN

        SET @Input = STUFF(@Input,@Pos,0,'_')
        SET @Pos = @Pos - 1
    END

    RETURN @Input + '_'
END

Output

SELECT dbo.PadStr('TESTING')      -- T_E_S_T_I_N_G_
Sign up to request clarification or add additional context in comments.

Comments

0

You can split the string using a numbers table and the rebuild it using for xml path().

select isnull(C.Value, '') as AS_CIS_Code 
from added_services as A
  cross apply (
              select substring(A.AS_CIS_Code, T.N, 1)+'_'
              from (values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10)) as T(N)
              where T.N <= len(A.AS_CIS_Code)
              order by T.N
              for xml path('')
              ) as C(Value)

SQL Fiddle

Comments

0

Try this

Create Function Code_Pad
(
    @code varchar(max) 
)
returns varchar(max)
as
begin

Declare @coding varchar(max) =''
Declare @i int = 1

      WHILE(LEN(@Coding)<=2*len(@code)-1)
      begin
        Select @coding = @coding+SUBSTRING(@code,@i,1)+'_'
        set @i=@i+1
      end
return @coding
end

End of Function

Select OptionList = case  when isnull(AS_CIS_Code,'') <> '' then dbo.Code_Pad(AS_CIS_Code)   else ''  
   end 
from added_services

Comments

0

May not be best solution, but works in one query using recursion

;WITH valCTE(Replaced,ToBeReplaced,Position)
AS
(
    SELECT CAST(LEFT(AS_CIS_Code,1) + '_' AS VARCHAR(20))
          ,SUBSTRING(AS_CIS_Code,2,LEN(AS_CIS_Code)-1)
          ,1
    FROM added_services

    UNION ALL
    SELECT CAST(Replaced + LEFT(ToBeReplaced,1) + '_' AS VARCHAR(20))
        ,SUBSTRING(ToBeReplaced,2,LEN(ToBeReplaced)-1)
        ,Position+1
    FROM valCTE
    WHERE LEN(ToBeReplaced)>0 
)
SELECT TOP 1 LEFT(Replaced,LEN(Replaced)-1) -- remove last _ 
FROM valCTE
ORDER BY Position DESC

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.