String Split Ignore Last delimiter if no data
I am string splitting some values that are comma delimited into rows.
However some values have an extra comma on the end.
Example
Userid | Value
1 | A,B,C,D,
2 | F,H
Code
select value
from string_split('A,B,C,D,',',')
Current Output
UserId | Value
1 | A
1 | B
1 | C
1 | D
1 |
Is there any way to make the string split function ignore the final comma if no data follows it?
Desired Output
UserId | Value
1 | A
1 | B
1 | C
1 | D
Using MSSQL
sql sql-server split
|
show 3 more comments
I am string splitting some values that are comma delimited into rows.
However some values have an extra comma on the end.
Example
Userid | Value
1 | A,B,C,D,
2 | F,H
Code
select value
from string_split('A,B,C,D,',',')
Current Output
UserId | Value
1 | A
1 | B
1 | C
1 | D
1 |
Is there any way to make the string split function ignore the final comma if no data follows it?
Desired Output
UserId | Value
1 | A
1 | B
1 | C
1 | D
Using MSSQL
sql sql-server split
9
Just addWHERE value <> ''
?
– MatBailie
Nov 22 at 22:55
2
That also removes NULL values
. What do you mean by that ?string_split
value column will not have null value. And If you pass aNULL
value tostring_split
, it will not return any rows.
– Squirrel
Nov 23 at 0:07
1
So your strings could look like "A,B,,,E,F," and in this instance you'd only want to remove the last blank? If so, maybe just do a check whetherRIGHT(Value, 1) = ','
and use a substring of the value, otherwise use the full string.
– ZLK
Nov 23 at 0:47
1
Don't forget: var split = String.Split("1,2,3", StringSplitOptions.RemoveEmptyEntries)
– Jack
Nov 23 at 1:38
1
You'd stop having issues like this if you stopped storing multiple values in strings in the first place. SQL Server has types designed for holding multiple values. Ignoring them and choosing to use strings (especially with T-SQL's poor string formatting support) seems a little odd.
– Damien_The_Unbeliever
Nov 23 at 10:39
|
show 3 more comments
I am string splitting some values that are comma delimited into rows.
However some values have an extra comma on the end.
Example
Userid | Value
1 | A,B,C,D,
2 | F,H
Code
select value
from string_split('A,B,C,D,',',')
Current Output
UserId | Value
1 | A
1 | B
1 | C
1 | D
1 |
Is there any way to make the string split function ignore the final comma if no data follows it?
Desired Output
UserId | Value
1 | A
1 | B
1 | C
1 | D
Using MSSQL
sql sql-server split
I am string splitting some values that are comma delimited into rows.
However some values have an extra comma on the end.
Example
Userid | Value
1 | A,B,C,D,
2 | F,H
Code
select value
from string_split('A,B,C,D,',',')
Current Output
UserId | Value
1 | A
1 | B
1 | C
1 | D
1 |
Is there any way to make the string split function ignore the final comma if no data follows it?
Desired Output
UserId | Value
1 | A
1 | B
1 | C
1 | D
Using MSSQL
sql sql-server split
sql sql-server split
edited Nov 22 at 23:05
asked Nov 22 at 22:51
Vine
506
506
9
Just addWHERE value <> ''
?
– MatBailie
Nov 22 at 22:55
2
That also removes NULL values
. What do you mean by that ?string_split
value column will not have null value. And If you pass aNULL
value tostring_split
, it will not return any rows.
– Squirrel
Nov 23 at 0:07
1
So your strings could look like "A,B,,,E,F," and in this instance you'd only want to remove the last blank? If so, maybe just do a check whetherRIGHT(Value, 1) = ','
and use a substring of the value, otherwise use the full string.
– ZLK
Nov 23 at 0:47
1
Don't forget: var split = String.Split("1,2,3", StringSplitOptions.RemoveEmptyEntries)
– Jack
Nov 23 at 1:38
1
You'd stop having issues like this if you stopped storing multiple values in strings in the first place. SQL Server has types designed for holding multiple values. Ignoring them and choosing to use strings (especially with T-SQL's poor string formatting support) seems a little odd.
– Damien_The_Unbeliever
Nov 23 at 10:39
|
show 3 more comments
9
Just addWHERE value <> ''
?
– MatBailie
Nov 22 at 22:55
2
That also removes NULL values
. What do you mean by that ?string_split
value column will not have null value. And If you pass aNULL
value tostring_split
, it will not return any rows.
– Squirrel
Nov 23 at 0:07
1
So your strings could look like "A,B,,,E,F," and in this instance you'd only want to remove the last blank? If so, maybe just do a check whetherRIGHT(Value, 1) = ','
and use a substring of the value, otherwise use the full string.
– ZLK
Nov 23 at 0:47
1
Don't forget: var split = String.Split("1,2,3", StringSplitOptions.RemoveEmptyEntries)
– Jack
Nov 23 at 1:38
1
You'd stop having issues like this if you stopped storing multiple values in strings in the first place. SQL Server has types designed for holding multiple values. Ignoring them and choosing to use strings (especially with T-SQL's poor string formatting support) seems a little odd.
– Damien_The_Unbeliever
Nov 23 at 10:39
9
9
Just add
WHERE value <> ''
?– MatBailie
Nov 22 at 22:55
Just add
WHERE value <> ''
?– MatBailie
Nov 22 at 22:55
2
2
That also removes NULL values
. What do you mean by that ? string_split
value column will not have null value. And If you pass a NULL
value to string_split
, it will not return any rows.– Squirrel
Nov 23 at 0:07
That also removes NULL values
. What do you mean by that ? string_split
value column will not have null value. And If you pass a NULL
value to string_split
, it will not return any rows.– Squirrel
Nov 23 at 0:07
1
1
So your strings could look like "A,B,,,E,F," and in this instance you'd only want to remove the last blank? If so, maybe just do a check whether
RIGHT(Value, 1) = ','
and use a substring of the value, otherwise use the full string.– ZLK
Nov 23 at 0:47
So your strings could look like "A,B,,,E,F," and in this instance you'd only want to remove the last blank? If so, maybe just do a check whether
RIGHT(Value, 1) = ','
and use a substring of the value, otherwise use the full string.– ZLK
Nov 23 at 0:47
1
1
Don't forget: var split = String.Split("1,2,3", StringSplitOptions.RemoveEmptyEntries)
– Jack
Nov 23 at 1:38
Don't forget: var split = String.Split("1,2,3", StringSplitOptions.RemoveEmptyEntries)
– Jack
Nov 23 at 1:38
1
1
You'd stop having issues like this if you stopped storing multiple values in strings in the first place. SQL Server has types designed for holding multiple values. Ignoring them and choosing to use strings (especially with T-SQL's poor string formatting support) seems a little odd.
– Damien_The_Unbeliever
Nov 23 at 10:39
You'd stop having issues like this if you stopped storing multiple values in strings in the first place. SQL Server has types designed for holding multiple values. Ignoring them and choosing to use strings (especially with T-SQL's poor string formatting support) seems a little odd.
– Damien_The_Unbeliever
Nov 23 at 10:39
|
show 3 more comments
1 Answer
1
active
oldest
votes
STRING_SPLIT Function doesn't support for lower version of sql server so first create a function to split the given string and join the function with your select query.Here is below sample for your expected result.
Created User defined Function
CREATE FUNCTION [dbo].[Udf_StringSplit]
(
@Userid INT,
@Value VARCHAR(1000)
)
RETURNS @Result TABLE(
Userid INT,
Value VARCHAR(10)
)
AS BEGIN
DECLARE @Data AS TABLE
(
Userid INT,
Value VARCHAR(100)
)
INSERT INTO @Data(Userid,Value)
SELECT @Userid, @Value
INSERT INTO @Result(Userid,Value)
SELECT Userid,
Split.a.value('.','nvarchar(1000)') AS Value
FROM
(
SELECT Userid,
CAST('<S>'+REPLACE(@Value,',','</S><S>')+'</S>' AS XML) Value
FROM @Data
) AS A
CROSS APPLY Value.nodes('S') AS Split(a)
WHERE Userid=@Userid AND Split.a.value('.','nvarchar(1000)') <>''
RETURN
END
GO
Sample data table
DECLARE @Data AS TABLE(Userid INT , Value VARCHAR(100))
INSERT INTO @Data
SELECT 1,'A,B,C,D,' UNION ALL
SELECT 2,'F,H'
Sql script to get the expected result
SELECT d.Userid,
f.Value
FROM @Data d
CROSS APPLY [dbo].[Udf_StringSplit] (d.Userid,d.Value) AS f
WHERE d.Userid=1
GO
Result
Userid Value
------------
1 A
1 B
1 C
1 D
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438812%2fstring-split-ignore-last-delimiter-if-no-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
STRING_SPLIT Function doesn't support for lower version of sql server so first create a function to split the given string and join the function with your select query.Here is below sample for your expected result.
Created User defined Function
CREATE FUNCTION [dbo].[Udf_StringSplit]
(
@Userid INT,
@Value VARCHAR(1000)
)
RETURNS @Result TABLE(
Userid INT,
Value VARCHAR(10)
)
AS BEGIN
DECLARE @Data AS TABLE
(
Userid INT,
Value VARCHAR(100)
)
INSERT INTO @Data(Userid,Value)
SELECT @Userid, @Value
INSERT INTO @Result(Userid,Value)
SELECT Userid,
Split.a.value('.','nvarchar(1000)') AS Value
FROM
(
SELECT Userid,
CAST('<S>'+REPLACE(@Value,',','</S><S>')+'</S>' AS XML) Value
FROM @Data
) AS A
CROSS APPLY Value.nodes('S') AS Split(a)
WHERE Userid=@Userid AND Split.a.value('.','nvarchar(1000)') <>''
RETURN
END
GO
Sample data table
DECLARE @Data AS TABLE(Userid INT , Value VARCHAR(100))
INSERT INTO @Data
SELECT 1,'A,B,C,D,' UNION ALL
SELECT 2,'F,H'
Sql script to get the expected result
SELECT d.Userid,
f.Value
FROM @Data d
CROSS APPLY [dbo].[Udf_StringSplit] (d.Userid,d.Value) AS f
WHERE d.Userid=1
GO
Result
Userid Value
------------
1 A
1 B
1 C
1 D
add a comment |
STRING_SPLIT Function doesn't support for lower version of sql server so first create a function to split the given string and join the function with your select query.Here is below sample for your expected result.
Created User defined Function
CREATE FUNCTION [dbo].[Udf_StringSplit]
(
@Userid INT,
@Value VARCHAR(1000)
)
RETURNS @Result TABLE(
Userid INT,
Value VARCHAR(10)
)
AS BEGIN
DECLARE @Data AS TABLE
(
Userid INT,
Value VARCHAR(100)
)
INSERT INTO @Data(Userid,Value)
SELECT @Userid, @Value
INSERT INTO @Result(Userid,Value)
SELECT Userid,
Split.a.value('.','nvarchar(1000)') AS Value
FROM
(
SELECT Userid,
CAST('<S>'+REPLACE(@Value,',','</S><S>')+'</S>' AS XML) Value
FROM @Data
) AS A
CROSS APPLY Value.nodes('S') AS Split(a)
WHERE Userid=@Userid AND Split.a.value('.','nvarchar(1000)') <>''
RETURN
END
GO
Sample data table
DECLARE @Data AS TABLE(Userid INT , Value VARCHAR(100))
INSERT INTO @Data
SELECT 1,'A,B,C,D,' UNION ALL
SELECT 2,'F,H'
Sql script to get the expected result
SELECT d.Userid,
f.Value
FROM @Data d
CROSS APPLY [dbo].[Udf_StringSplit] (d.Userid,d.Value) AS f
WHERE d.Userid=1
GO
Result
Userid Value
------------
1 A
1 B
1 C
1 D
add a comment |
STRING_SPLIT Function doesn't support for lower version of sql server so first create a function to split the given string and join the function with your select query.Here is below sample for your expected result.
Created User defined Function
CREATE FUNCTION [dbo].[Udf_StringSplit]
(
@Userid INT,
@Value VARCHAR(1000)
)
RETURNS @Result TABLE(
Userid INT,
Value VARCHAR(10)
)
AS BEGIN
DECLARE @Data AS TABLE
(
Userid INT,
Value VARCHAR(100)
)
INSERT INTO @Data(Userid,Value)
SELECT @Userid, @Value
INSERT INTO @Result(Userid,Value)
SELECT Userid,
Split.a.value('.','nvarchar(1000)') AS Value
FROM
(
SELECT Userid,
CAST('<S>'+REPLACE(@Value,',','</S><S>')+'</S>' AS XML) Value
FROM @Data
) AS A
CROSS APPLY Value.nodes('S') AS Split(a)
WHERE Userid=@Userid AND Split.a.value('.','nvarchar(1000)') <>''
RETURN
END
GO
Sample data table
DECLARE @Data AS TABLE(Userid INT , Value VARCHAR(100))
INSERT INTO @Data
SELECT 1,'A,B,C,D,' UNION ALL
SELECT 2,'F,H'
Sql script to get the expected result
SELECT d.Userid,
f.Value
FROM @Data d
CROSS APPLY [dbo].[Udf_StringSplit] (d.Userid,d.Value) AS f
WHERE d.Userid=1
GO
Result
Userid Value
------------
1 A
1 B
1 C
1 D
STRING_SPLIT Function doesn't support for lower version of sql server so first create a function to split the given string and join the function with your select query.Here is below sample for your expected result.
Created User defined Function
CREATE FUNCTION [dbo].[Udf_StringSplit]
(
@Userid INT,
@Value VARCHAR(1000)
)
RETURNS @Result TABLE(
Userid INT,
Value VARCHAR(10)
)
AS BEGIN
DECLARE @Data AS TABLE
(
Userid INT,
Value VARCHAR(100)
)
INSERT INTO @Data(Userid,Value)
SELECT @Userid, @Value
INSERT INTO @Result(Userid,Value)
SELECT Userid,
Split.a.value('.','nvarchar(1000)') AS Value
FROM
(
SELECT Userid,
CAST('<S>'+REPLACE(@Value,',','</S><S>')+'</S>' AS XML) Value
FROM @Data
) AS A
CROSS APPLY Value.nodes('S') AS Split(a)
WHERE Userid=@Userid AND Split.a.value('.','nvarchar(1000)') <>''
RETURN
END
GO
Sample data table
DECLARE @Data AS TABLE(Userid INT , Value VARCHAR(100))
INSERT INTO @Data
SELECT 1,'A,B,C,D,' UNION ALL
SELECT 2,'F,H'
Sql script to get the expected result
SELECT d.Userid,
f.Value
FROM @Data d
CROSS APPLY [dbo].[Udf_StringSplit] (d.Userid,d.Value) AS f
WHERE d.Userid=1
GO
Result
Userid Value
------------
1 A
1 B
1 C
1 D
edited Nov 23 at 10:57
answered Nov 23 at 10:34
Sreenu131
1,300138
1,300138
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438812%2fstring-split-ignore-last-delimiter-if-no-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
9
Just add
WHERE value <> ''
?– MatBailie
Nov 22 at 22:55
2
That also removes NULL values
. What do you mean by that ?string_split
value column will not have null value. And If you pass aNULL
value tostring_split
, it will not return any rows.– Squirrel
Nov 23 at 0:07
1
So your strings could look like "A,B,,,E,F," and in this instance you'd only want to remove the last blank? If so, maybe just do a check whether
RIGHT(Value, 1) = ','
and use a substring of the value, otherwise use the full string.– ZLK
Nov 23 at 0:47
1
Don't forget: var split = String.Split("1,2,3", StringSplitOptions.RemoveEmptyEntries)
– Jack
Nov 23 at 1:38
1
You'd stop having issues like this if you stopped storing multiple values in strings in the first place. SQL Server has types designed for holding multiple values. Ignoring them and choosing to use strings (especially with T-SQL's poor string formatting support) seems a little odd.
– Damien_The_Unbeliever
Nov 23 at 10:39