I need to convert a value list separated by semi-colons, including in front, into a regular value list with quotes and commas. There might only be one value, or there may be many values in the field.
I thought about replacing the ; with a comma, but then I still have a comma in front and behind, and I also need to add single quotes.
REPLACE(S_List, ';', ',')
I want ;a;b;c; to be 'a','b','c' or at least a,b,c but I don't know what to do with the beginning and end semicolons
With substring() and replace():
declare #slist varchar(100) = ';a;b;c;'
select substring(replace(#slist, ';', ''','''), 3, len(replace(#slist, ';', ''',''')) - 4)
See the demo.
Result:
'a','b','c'
Edit.
Use it like this in your table:
select
case when s_list like '%;%;%' then
substring(replace(s_list, ';', ''','''), 3, len(replace(s_list, ';', ''',''')) - 4)
else s_list
end
from tablename
See the demo.
You could try:
SELECT REPLACE(LEFT(S_List,1),';','''')
SELECT REPLACE(RIGHT(S_List,1),';','''')
SELECT REPLACE(S_List,';', ''',''')
If your values do not have spaces, you can use a trim() trick:
select '''' + replace(ltrim(rtrim(replace(s_list, ',', ' '))), ',', ''',''') + ''''
Related
The below query working for one string. However when I run at whole table data it's not working
select
lower( regexp_replace( nvl(column1,':'), '\\s+|[][!"#$%&\'()*+,.\\\\/:;<=>?#\^_`{|}~-]+', '')) as addres_line_1,
column1
from values('122 E 7th Street ');
output: 122e7thstreet
when I run similar query for the table, the white spaces are not fully removed.
output: 122e7th street
table level query:
select concat(
column1, ':',
column2, ':',
lower(regexp_replace(regexp_replace(nvl(ADDRESS_LINE_1,':'),'\\s|[][!"#$%&\'()*+,.\\\\/:;<=>?#\^_`{|}~-]+',''),'[ \t\r\n\v\f]+','')), ':',
lower(regexp_replace(nvl(ADDRESS_LINE_2,':'),'\\s|[][!"#$%&\'()*+,.\\\\/:;<=>?#\^_`{|}~-]+','')), ':',
lower(regexp_replace(nvl(ADDRESS_LINE_3,':'),'\\s|[][!"#$%&\'()*+,.\\\\/:;<=>?#\^_`{|}~-]+','')), ':',
lower(regexp_replace(nvl(PRIMARY_TOWN,':'),'\\s|[][!"#$%&\'()*+,.\\\\/:;<=>?#\^_`{|}~-]+','')), ':',
lower(regexp_replace(nvl(COUNTRY,':'),'\\s|[][!"#$%&\'()*+,.\\\\/:;<=>?#\^_`{|}~-]+','')), ':',
lower(regexp_replace(nvl(TERRITORY_CODE,':'),'\\s|[][!"#$%&\'()*+,.\\\\/:;<=>?#\^_`{|}~-]+','')), ':',
lower(regexp_replace(nvl(POSTAL_CODE,':'),'\\s|[][!"#$%&\'()*+,.\\\\/:;<=>?#\^_`{|}~-]+|\\s+','')), ':',
lower(regexp_replace(nvl(COUNTRY_CODE,':'),'\\s|[][!"#$%&\'()*+,.\\\\/:;<=>?#\^_`{|}~-]+',''))
) as ROLE_PLAYER_ADDRESS_HASH_KEY1
from address
Does using regex expressions like this one work work:
REGEXP_REPLACE(REGEXP_REPLACE(ADDRESS_LINE_1, '[^\\w]'),'_')
\w is any digit, letter, or underscore - hence the need for an outer REGEXP_REPLACE to remove underscores
So you have three different regex,
you single demostraition regex is:
'\\s+|[][!"#$%&\'()*+,.\\\\/:;<=>?#\^_`{|}~-]+'
then you use:
'\\s|[][!"#$%&\'()*+,.\\\\/:;<=>?#\^_`{|}~-]+'
for the table except for POSTAL_CODE which uses:
'\\s|[][!"#$%&\'()*+,.\\\\/:;<=>?#\^_`{|}~-]+|\\s+'
Thus do they work equally well on you example input, or other inputs you see failure on?
select
'\\s+|[][!"#$%&\'()*+,.\\\\/:;<=>?#\^_`{|}~-]+' as reg_1,
'\\s|[][!"#$%&\'()*+,.\\\\/:;<=>?#\^_`{|}~-]+' as reg_2,
'\\s|[][!"#$%&\'()*+,.\\\\/:;<=>?#\^_`{|}~-]+|\\s+' as reg3,
lower( regexp_replace( nvl(column1,':'), reg_1, '')) as addres_1,
lower( regexp_replace( nvl(column1,':'), reg_2, '')) as addres_2,
lower( regexp_replace( nvl(column1,':'), reg3, '')) as addres_3,
column1
from values
('122 E 7th Street '),
('122 E 7th Street '),
(' 122 E 7th Street ')
-- etc, etc
;
alrighty, so the token you have is C2 A0 \u00a0 the no-break-space token.
select
'\\s+|[][!"#$%&\'()*+,.\\\\/:;<=>?#\^_`{|}~-]+' as reg_1,
column1,
regexp_replace( column1, reg_1, '') as out
from values
('122 e 7th\u00a0street')
gives:
REG_1: \s+|[][!"#$%&'()*+,.\\/:;<=>?#^_`{|}~-]+
COLUMN1: 122 e 7th street
OUT: 122e7th street
and if you copy that output the space has converted to a normal 0x20 space.
So now we know what the input data is we just need to match it.
so you can use the TRANSLATE function to remove the unicode character via
select
column1
,regexp_replace( column1, '\\s+', '') as r1
,translate(column1,'\u00a0',' ') as t1 as t1
,regexp_replace( t1, '\\s+', '') as r
from values ('122 e 7th\u00a0street');
which means we can just put \u00a0 into the regex
regexp_replace( column1, '\\s+|\u00a0+', '')
works a charm!
I want to replace rightmost same characters from string.
e.g string is like in this format
"GGENXG00126""XLOXXXXX"
in sql but last consecutive X lenght is not fix.
I already Tried in SQL is
select REPLACE('"GGENXG00126""XLOXXXXX"', 'X', '')
but using this all 'X' is removed. I want to remove only rightmost same characters and output expected "GGENG00126""LO".
You can replace all x with spaces, RTRIM, then undo the replacements:
SELECT '"' + REPLACE(RTRIM(REPLACE(SUBSTRING(str, 2, LEN(str) - 2), 'X', ' ')), ' ', 'X') + '"'
FROM (VALUES
('"GGENXG00126""XLOXXXXX"')
) v(str)
-- "GGENXG00126""XLO"
An alternative idea using PATINDEX and REVERSE, to find the first character that isn't the final character in the string. (Assumes all strings are quoted):
SELECT REVERSE(STUFF(R.ReverseString,1,PATINDEX('%[^' + LEFT(R.ReverseString,1) + ']%',R.ReverseString)-1,'')) + '"'
FROM (VALUES('"GGENXG00126""XLOXXXXX"'))V(YourString)
CROSS APPLY (VALUES(STUFF(REVERSE(V.YourString),1,1,''))) R(ReverseString);
You can try this below option-
DECLARE #InputString VARCHAR(200) = 'GGENXG00126""XLOXXXXX'
SELECT
LEFT(
#InputString,
LEN(#InputString)+
1-
PATINDEX(
'%[^X]%',
REVERSE(#InputString)
)
)
Output is-
GGENXG00126""XLO
I tried almost everything to remove some unnecessary characters from sql string but it's not working:
declare #string as char(20)=' my '
select #string as OriginalString
select len(Replace((#string),CHAR(10),''))as StrLen1;
SELECT len(REPLACE(REPLACE(#string, CHAR(13), ' '), CHAR(10), '')) as StrLen2
Output Screen
Need a way to get this done.
Of the three types of whitespace you seem to mention, space, newline, and carriage return, your #string only has spaces. But you never actually remove space. If you want to remove all three of these types of whitespace, you can try the following:
SELECT
LEN(REPLACE(REPLACE(REPLACE(#string, CHAR(13), ''), CHAR(10), ''), ' ', '')) AS StrLen2
The output from the above query is 2, since after removing the whitespace we are left with the string 'my'.
Demo
declare #newline char(2)
set #newline= char(13)+char(10) -- CR + LF
update **Your Table Here**
-- ItemCode is column Name,Find and replace values CRLF = ''
Set ItemCode=REPLACE(ItemCode,#newline,'')
where RIGHT(ItemCode,2) = #newline -- Maybe no need
If you just want to get rid of spaces:
declare #string as char(20)=' my '
select LTRIM(RTRIM(#string))
If you're trying to trim all spaces try this :
Select RTRIM(LTRIM(#string))
DEMO
I have a data flow with over 150 columns and many of them are of data type string, I need to remove comma's and double quotes from the value of every text column because they are causing issues when I export the data to CSV, is there an easy way to do that other than doing it explicitly for every column in a derived column or script compnent?
In the script generator below, put all your column names from the CSV in the order that you want, and run it.
;With ColumnList as
(
Select 1 Id, 'FirstColumn' as ColumnName
Union Select 2, 'SecondColumn'
Union Select 3, 'ThirdColumn'
Union Select 4, 'FourthColumn'
Union Select 5, 'FifthColumn'
)
Select 'Trim (Replace (Replace (' + ColumnName + ', '','', ''''), ''"'', ''''))'
From ColumnList
Order BY Id
The Id column should contain a proper sequence (I would generate that in EXCEL. Here is the output
---------------------------------------------------------
Trim (Replace (Replace (FirstColumn, ',', ''), '"', ''))
Trim (Replace (Replace (SecondColumn, ',', ''), '"', ''))
Trim (Replace (Replace (ThirdColumn, ',', ''), '"', ''))
Trim (Replace (Replace (FourthColumn, ',', ''), '"', ''))
Trim (Replace (Replace (FifthColumn, ',', ''), '"', ''))
(5 row(s) affected)
You could just cut and paste from here into your SSIS dataflow.
I found a way to loop through columns in a script component, then i was able to check the column data type and do a replace function, here is the post i used.
I need to ensure that a given field does not have more than one space (I am not concerned about all white space, just space) between characters.
So
'single spaces only'
needs to be turned into
'single spaces only'
The below will not work
select replace('single spaces only',' ',' ')
as it would result in
'single spaces only'
I would really prefer to stick with native T-SQL rather than a CLR based solution.
Thoughts?
Even tidier:
select string = replace(replace(replace(' select single spaces',' ','<>'),'><',''),'<>',' ')
Output:
select single spaces
This would work:
declare #test varchar(100)
set #test = 'this is a test'
while charindex(' ',#test ) > 0
begin
set #test = replace(#test, ' ', ' ')
end
select #test
If you know there won't be more than a certain number of spaces in a row, you could just nest the replace:
replace(replace(replace(replace(myText,' ',' '),' ',' '),' ',' '),' ',' ')
4 replaces should fix up to 16 consecutive spaces (16, then 8, then 4, then 2, then 1)
If it could be significantly longer, then you'd have to do something like an in-line function:
CREATE FUNCTION strip_spaces(#str varchar(8000))
RETURNS varchar(8000) AS
BEGIN
WHILE CHARINDEX(' ', #str) > 0
SET #str = REPLACE(#str, ' ', ' ')
RETURN #str
END
Then just do
SELECT dbo.strip_spaces(myText) FROM myTable
This is somewhat brute force, but will work
CREATE FUNCTION stripDoubleSpaces(#prmSource varchar(max)) Returns varchar(max)
AS
BEGIN
WHILE (PATINDEX('% %', #prmSource)>0)
BEGIN
SET #prmSource = replace(#prmSource ,' ',' ')
END
RETURN #prmSource
END
GO
-- Unit test --
PRINT dbo.stripDoubleSpaces('single spaces only')
single spaces only
update mytable
set myfield = replace (myfield, ' ', ' ')
where charindex(' ', myfield) > 0
Replace will work on all the double spaces, no need to put in multiple replaces. This is the set-based solution.
It can be done recursively via the function:
CREATE FUNCTION dbo.RemSpaceFromStr(#str VARCHAR(MAX)) RETURNS VARCHAR(MAX) AS
BEGIN
RETURN (CASE WHEN CHARINDEX(' ', #str) > 0 THEN
dbo.RemSpaceFromStr(REPLACE(#str, ' ', ' ')) ELSE #str END);
END
then, for example:
SELECT dbo.RemSpaceFromStr('some string with many spaces') AS NewStr
returns:
NewStr
some string with many spaces
Or the solution based on method described by #agdk26 or #Neil Knight (but safer)
both examples return output above:
SELECT REPLACE(REPLACE(REPLACE('some string with many spaces'
, ' ', ' ' + CHAR(7)), CHAR(7) + ' ', ''), ' ' + CHAR(7), ' ') AS NewStr
--but it remove CHAR(7) (Bell) from string if exists...
or
SELECT REPLACE(REPLACE(REPLACE('some string with many spaces'
, ' ', ' ' + CHAR(7) + CHAR(7)), CHAR(7) + CHAR(7) + ' ', ''), ' ' + CHAR(7) + CHAR(7), ' ') AS NewStr
--but it remove CHAR(7) + CHAR(7) from string
How it works:
Caution:
Char/string used to replace spaces shouldn't exist on begin or end of string and stand alone.
Here is a simple function I created for cleaning any spaces before or after, and multiple spaces within a string. It gracefully handles up to about 108 spaces in a single stretch and as many blocks as there are in the string. You can increase that by factors of 8 by adding additional lines with larger chunks of spaces if you need to. It seems to perform quickly and has not caused any problems in spite of it's generalized use in a large application.
CREATE FUNCTION [dbo].[fnReplaceMultipleSpaces] (#StrVal AS VARCHAR(4000))
RETURNS VARCHAR(4000)
AS
BEGIN
SET #StrVal = Ltrim(#StrVal)
SET #StrVal = Rtrim(#StrVal)
SET #StrVal = REPLACE(#StrVal, ' ', ' ') -- 16 spaces
SET #StrVal = REPLACE(#StrVal, ' ', ' ') -- 8 spaces
SET #StrVal = REPLACE(#StrVal, ' ', ' ') -- 4 spaces
SET #StrVal = REPLACE(#StrVal, ' ', ' ') -- 2 spaces
SET #StrVal = REPLACE(#StrVal, ' ', ' ') -- 2 spaces (for odd leftovers)
RETURN #StrVal
END
Method #1
The first method is to replace extra spaces between words with an uncommon symbol combination as a temporary marker. Then you can replace the temporary marker symbols using the replace function rather than a loop.
Here is a code example that replaces text within a String variable.
DECLARE #testString AS VARCHAR(256) = ' Test text with random* spacing. Please normalize this spacing!';
SELECT REPLACE(REPLACE(REPLACE(#testString, ' ', '*^'), '^*', ''), '*^', ' ');
Execution Time Test #1: In ten runs of this replacement method, the average wait time on server replies was 1.7 milliseconds and total execution time was 4.6 milliseconds.
Execution Time Test #2: The average wait time on server replies was 1.7 milliseconds and total execution time was 3.7 milliseconds.
Method #2
The second method is not quite as elegant as the first, but also gets the job done. This method works by nesting four (or optionally more) replace statements that replace two blank spaces with one blank space.
DECLARE #testString AS VARCHAR(256) = ' Test text with random* spacing. Please normalize this spacing!';
SELECT REPLACE(REPLACE(REPLACE(REPLACE(#testString,' ',' '),' ',' '),' ',' '),' ',' ')
Execution Time Test #1: In ten runs of this replacement method, the average wait time on server replies was 1.9 milliseconds and total execution time was 3.8 milliseconds.
Execution Time Test #2: The average wait time on server replies was 1.8 milliseconds and total execution time was 4.8 milliseconds.
Method #3
The third method of replacing extra spaces between words is to use a simple loop. You can do a check on extra spaces in a while loop and then use the replace function to reduce the extra spaces with each iteration of the loop.
DECLARE #testString AS VARCHAR(256) = ' Test text with random* spacing. Please normalize this spacing!';
WHILE CHARINDEX(' ',#testString) > 0
SET #testString = REPLACE(#testString, ' ', ' ')
SELECT #testString
Execution Time Test #1: In ten runs of this replacement method, the average wait time on server replies was 1.8 milliseconds and total execution time was 3.4 milliseconds.
Execution Time Test #2: The average wait time on server replies was 1.9 milliseconds and total execution time was 2.8 milliseconds.
This is the solution via multiple replace, which works for any strings (does not need special characters, which are not part of the string).
declare #value varchar(max)
declare #result varchar(max)
set #value = 'alpha beta gamma delta xyz'
set #result = replace(replace(replace(replace(replace(replace(replace(
#value,'a','ac'),'x','ab'),' ',' x'),'x ',''),'x',''),'ab','x'),'ac','a')
select #result -- 'alpha beta gamma delta xyz'
You can try this:
select Regexp_Replace('single spaces only','( ){2,}', ' ') from dual;
Just Adding Another Method-
Replacing Multiple Spaces with Single Space WITHOUT Using REPLACE in SQL Server-
DECLARE #TestTable AS TABLE(input VARCHAR(MAX));
INSERT INTO #TestTable VALUES
('HAPPY NEWYEAR 2020'),
('WELCOME ALL !');
SELECT
CAST('<r><![CDATA[' + input + ']]></r>' AS XML).value('(/r/text())[1] cast as xs:token?','VARCHAR(MAX)')
AS Expected_Result
FROM #TestTable;
--OUTPUT
/*
Expected_Result
HAPPY NEWYEAR 2020
WELCOME ALL !
*/
Found this while digging for an answer:
SELECT REPLACE(
REPLACE(
REPLACE(
LTRIM(RTRIM('1 2 3 4 5 6'))
,' ',' '+CHAR(7))
,CHAR(7)+' ','')
,CHAR(7),'') AS CleanString
where charindex(' ', '1 2 3 4 5 6') > 0
The full answer (with explanation) was pulled from: http://techtipsbysatish.blogspot.com/2010/08/sql-server-replace-multiple-spaces-with.html
On second look, seems to be just a slightly different version of the selected answer.
Please Find below code
select trim(string_agg(value,' ')) from STRING_SPLIT(' single spaces only ',' ')
where value<>' '
This worked for me..
Hope this helps...
With the "latest" SQL Server versions (Compatibility level 130) you could also use string_split and string_agg.
string_split can return an ordinal column when provided with a third argument. (https://learn.microsoft.com/en-us/sql/t-sql/functions/string-split-transact-sql?view=sql-server-ver16#enable_ordinal). So we can preserve the order of the string_split.
Using a common table expression:
with cte(value) as (select value from string_split(' a b c d e ', ' ', 1) where value <> '' order by ordinal offset 0 rows)
select string_agg(value, ' ') from cte
a b c d e results in a b c d e
I use FOR XML PATH solution to replace multiple spaces into single space
The idea is to replace spaces with XML tags
Then split XML string into string fragments without XML tags
Finally concatenating those string values by adding single space characters between two
Here is how final UDF function can be called
select dbo.ReplaceMultipleSpaces(' Sample text with multiple space ')