Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a string like

SQL
DECLARE @VAR VARCHAR(MAX)
SET @String = '1,1000,"1,1000",0,0,"1,125"'


I want it as '1,1000,"11000",0,0,"1125"'

is it possible?

I tried using the
SQL
Replace(SUBSTRING(@String, CHARINDEX('"', @String) + 1,LEN(REPLACE(@String,'"',''))-1),',','')


but it returning "1000",11000,0,0,"1125

please help
Posted
Updated 1-Apr-13 23:49pm
v3
Comments
Zoltán Zörgő 2-Apr-13 5:46am    
So you want to remove the string marker double quotes and the commas from within the strings?
And why are you doing this CSV conversion with T-SQL?
Do you have the possibility to install CLR UDF on the SQL Server?
Madhav Hatwalne 2-Apr-13 5:48am    
Actually i want to bulk insert in a file
just look @ my previous question: Read Comma in comma separated file[^]

You need to break it up into elements so that you only remove teh commas between double quotes:
SQL
DECLARE @STRING VARCHAR(MAX)
DECLARE @START VARCHAR(MAX)
DECLARE @MID VARCHAR(MAX)
DECLARE @END VARCHAR(MAX)
DECLARE @I int
SET @STRING = '1,1000,"1,1000",0,0,"1,125"'
SET @I = CHARINDEX('"', @STRING)
SET @START = SUBSTRING(@STRING, 0, @I)
SET @STRING = SUBSTRING(@STRING, @I + 1, LEN(@STRING))
SET @I = CHARINDEX('"', @STRING)
SET @MID = SUBSTRING(@STRING, 0, @I)
SET @END = SUBSTRING(@STRING, @I + 1, LEN(@STRING))
SET @MID = REPLACE(@MID, ',', '')
SET @STRING = @START + @MID + @END
You may have to add a WHILE loop if there is more than one quoted section - I leave that as an exercise for the reader! :laugh:
 
Share this answer
 
Comments
Karthik Harve 2-Apr-13 6:00am    
Best as always Griff. +5.!!!
Madhav Hatwalne 2-Apr-13 6:41am    
Thank you very much ! :)
OriginalGriff 2-Apr-13 7:05am    
You're welcome!
Create below function
SQL
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[ParseValues]
(@String varchar(8000), @Delimiter varchar(10) )
RETURNS @RESULTS TABLE (ID int identity(1,1), Val varchar(50))
AS
BEGIN
    DECLARE @Value varchar(100)
    WHILE @String is not null
    BEGIN
        SELECT @Value=CASE WHEN PATINDEX('%'+@Delimiter+'%',@String) >0 THEN LEFT(@String,PATINDEX('%'+@Delimiter+'%',@String)-1) ELSE @String END, @String=CASE WHEN PATINDEX('%'+@Delimiter+'%',@String) >0 THEN SUBSTRING(@String,PATINDEX('%'+@Delimiter+'%',@String)+LEN(@Delimiter),LEN(@String)) ELSE NULL END
        INSERT INTO @RESULTS (Val)
        SELECT @Value
    END
RETURN
END


Now execute below query...
SQL
DECLARE @String VARCHAR(MAX)
DECLARE @S VARCHAR(MAX)
SET @String = '1,1000,"1,1000",0,0,"1,125"'

	select @S = COALESCE(@S, '') +case when charindex('"',val)>0 then case when charindex('"',val) > 1 then '' else '"' end else '' end
				+ 
				replace(val,'"','')
				+ 
				case when charindex('"',val)>0 then case when charindex('"',val) > 1 then '",' else '' end else ',' end  
	from dbo.parsevalues(@string,',')
select substring(@s,0,len(@s))

Happy Coding!
:)
 
Share this answer
 
v2
Comments
Madhav Hatwalne 2-Apr-13 6:41am    
Thank you very much :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900