November 2, 2022 at 4:13 am
I need to add a single quote in each of the alternate characters. The input string could be a variable length.
As an example:
Input String: 5,1,6,1,69,1
output: '5',1,'6',1,'69',1
Input String: 5,1,6,1,3,2,5,3,69,1
output: '5',1,'6',1,'3',2,'5',3,'69',1
November 2, 2022 at 3:33 pm
drop table if exists #some_table;
go
create table #some_table (
input_string varchar(200) not null);
insert #some_table(input_string) values
('5,1,6,1,69,1'),
('5,1,6,1,3,2,5,3,69,1');
select string_agg(iif((ss.ordinal-1)%2=0, concat('''', ss.[value], ''''), ss.[value]), ',')
within group (order by ss.ordinal) [output]
from #some_table st
cross apply string_split(st.input_string, ',', 1) ss
group by st.input_string;
Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können
November 2, 2022 at 3:49 pm
The enable_ordinal argument for STRING_SPLIT is not implemented in 2019.
November 2, 2022 at 3:59 pm
Does this work? I don't know if there will ever be spaces, but I removed them anyway.
DECLARE @InputString VARCHAR(1000) = '5, 1,6, 1,69,1, 123456, 9'
SELECT REPLACE(CONCAT('''', REPLACE(@InputString, ',', ''','''), ''''), ' ', '')
In case of preceding or trailing commas.
DECLARE @InputString VARCHAR(1000) = '5, 1,6, 1,69,1, 123456, 9,'
SELECT REPLACE(CONCAT('''', REPLACE(TRIM(',' FROM @InputString), ',', ''','''), ''''), ' ', '')
November 2, 2022 at 4:11 pm
The enable_ordinal argument for STRING_SPLIT is not implemented in 2019.
It's true. It's available in Azure SQL compatibility level 150 (and above) and SQL Server 2022. There are alternatives such as:
Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können
November 2, 2022 at 6:53 pm
How about a cte?
drop table if exists #some_table;
go
create table #some_table (
input_string varchar(200) not null);
insert #some_table(input_string) values
('5,1,6,1,69,1'),
('5,1,6,1,3,2,5,3,69,1'),
('5 ,2,36,1,69,1')
;
;with cte as (select input_string, case when RowNum %2 = 0 then [value] else Concat('''',[value],'''') end as NewValue from (
select input_string, ltrim(rtrim([value])) as [Value], row_number() over (partition by st.input_string order by st.input_string) as RowNum
from #some_table st
cross apply string_split(st.input_string, ',') ss) x)
SELECT input_string,
STUFF(
( SELECT ',' + NewValue
FROM cte t2
WHERE t1.input_string=t2.input_string
FOR XML PATH('')
),
1,
1,''
) AS NewValue
FROM cte t1
GROUP BY input_String
For better, quicker answers, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
November 2, 2022 at 10:44 pm
I need to add a single quote in each of the alternate characters. The input string could be a variable length.
As an example:
Input String: 5,1,6,1,69,1
output: '5',1,'6',1,'69',1
Input String: 5,1,6,1,3,2,5,3,69,1
output: '5',1,'6',1,'3',2,'5',3,'69',1
If you explain why this strange requirement exists, I'll provide the exact code necessary to accomplish it.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 2, 2022 at 10:57 pm
skmoh2 wrote:I need to add a single quote in each of the alternate characters. The input string could be a variable length.
As an example:
Input String: 5,1,6,1,69,1
output: '5',1,'6',1,'69',1
Input String: 5,1,6,1,3,2,5,3,69,1
output: '5',1,'6',1,'3',2,'5',3,'69',1
If you explain why this strange requirement exists, I'll provide the exact code necessary to accomplish it.
My guess is that when you put a single quote in front of a number in an Excel cell then the cell is formatted as text not a number.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy