How to split string with inconsistent order format in SQL
I want to split the strings in the 'Scorer' column so that the scorer name is retained but not the score type (i.e. to remove the text within the brackets and the brackets to just leave the scorer name in that field).
Scorer
Ellis J.(Conversion Goal)
Ellis J.(Try)
Ellis J.(Conversion Goal)
Trueman J.(Try)
(Conversion Goal)Brough D.
(Try)McGillvary J.
(Try)McGillvary J.
(Penalty Goal)Brough D.
Ellis J.(Conversion Goal)
It should look like the below.
Scorer
Ellis J.
Ellis J.
Ellis J.
Trueman J.
Brough D.
McGillvary J.
McGillvary J.
Brough D.
Ellis J.
sql sql-server tsql
add a comment |
I want to split the strings in the 'Scorer' column so that the scorer name is retained but not the score type (i.e. to remove the text within the brackets and the brackets to just leave the scorer name in that field).
Scorer
Ellis J.(Conversion Goal)
Ellis J.(Try)
Ellis J.(Conversion Goal)
Trueman J.(Try)
(Conversion Goal)Brough D.
(Try)McGillvary J.
(Try)McGillvary J.
(Penalty Goal)Brough D.
Ellis J.(Conversion Goal)
It should look like the below.
Scorer
Ellis J.
Ellis J.
Ellis J.
Trueman J.
Brough D.
McGillvary J.
McGillvary J.
Brough D.
Ellis J.
sql sql-server tsql
1
Welcomde to stackoverflow. Please take a minute to take the tour, especially How to Ask. Sample data is best served as DDL + DML. Please edit your question to include it, your current attempt and your desired results. For more details, read this.
– Zohar Peled
Jan 20 at 12:24
add a comment |
I want to split the strings in the 'Scorer' column so that the scorer name is retained but not the score type (i.e. to remove the text within the brackets and the brackets to just leave the scorer name in that field).
Scorer
Ellis J.(Conversion Goal)
Ellis J.(Try)
Ellis J.(Conversion Goal)
Trueman J.(Try)
(Conversion Goal)Brough D.
(Try)McGillvary J.
(Try)McGillvary J.
(Penalty Goal)Brough D.
Ellis J.(Conversion Goal)
It should look like the below.
Scorer
Ellis J.
Ellis J.
Ellis J.
Trueman J.
Brough D.
McGillvary J.
McGillvary J.
Brough D.
Ellis J.
sql sql-server tsql
I want to split the strings in the 'Scorer' column so that the scorer name is retained but not the score type (i.e. to remove the text within the brackets and the brackets to just leave the scorer name in that field).
Scorer
Ellis J.(Conversion Goal)
Ellis J.(Try)
Ellis J.(Conversion Goal)
Trueman J.(Try)
(Conversion Goal)Brough D.
(Try)McGillvary J.
(Try)McGillvary J.
(Penalty Goal)Brough D.
Ellis J.(Conversion Goal)
It should look like the below.
Scorer
Ellis J.
Ellis J.
Ellis J.
Trueman J.
Brough D.
McGillvary J.
McGillvary J.
Brough D.
Ellis J.
sql sql-server tsql
sql sql-server tsql
edited Jan 20 at 12:46
Barbaros Özhan
13.3k71633
13.3k71633
asked Jan 20 at 12:19
LouisDLouisD
62
62
1
Welcomde to stackoverflow. Please take a minute to take the tour, especially How to Ask. Sample data is best served as DDL + DML. Please edit your question to include it, your current attempt and your desired results. For more details, read this.
– Zohar Peled
Jan 20 at 12:24
add a comment |
1
Welcomde to stackoverflow. Please take a minute to take the tour, especially How to Ask. Sample data is best served as DDL + DML. Please edit your question to include it, your current attempt and your desired results. For more details, read this.
– Zohar Peled
Jan 20 at 12:24
1
1
Welcomde to stackoverflow. Please take a minute to take the tour, especially How to Ask. Sample data is best served as DDL + DML. Please edit your question to include it, your current attempt and your desired results. For more details, read this.
– Zohar Peled
Jan 20 at 12:24
Welcomde to stackoverflow. Please take a minute to take the tour, especially How to Ask. Sample data is best served as DDL + DML. Please edit your question to include it, your current attempt and your desired results. For more details, read this.
– Zohar Peled
Jan 20 at 12:24
add a comment |
5 Answers
5
active
oldest
votes
The correct solution would be to fix the database structure by adding another column to the table for the score type. In fact, you should probably have a table for score types and add a foreign key to it from this table.
Assuming you can't change the database structure, this is better done at the presentation layer. Any programming language should enable you do do it quite easily. String manipulation is not SQL's strong suit.
That being said, it can certainly be done using pure T-SQL - with a simple common table expression to get the brackets indexes using charindex
, and a case expression with stuff
in the select statement.
First, create and populate sample table (Please save us this step in your future questions):
DECLARE @T AS TABLE
(
Scorer nvarchar(100)
);
INSERT INTO @T (Scorer) VALUES
('Ellis J.(Conversion Goal)'),
('Ellis J.(Try)'),
('Ellis J.(Conversion Goal)'),
('Trueman J.(Try)'),
('(Conversion Goal)Brough D.'),
('(Try)McGillvary J.'),
('(Try)McGillvary J.'),
('(Penalty Goal)Brough D.'),
('Ellis J.(Conversion Goal)'),
-- Note: I've added some edge cases to the sample data:
('a row with (brackets) in the middle'),
('Just an open bracket (forgot to close '),
('Just a close bracket forgot to open)'),
('no brackets at all'),
('brackets ) in reversed order (');
Then, the CTE:
WITH CTE AS
(
SELECT Scorer,
CHARINDEX('(', Scorer) As OpenBrackets,
CHARINDEX(')', Scorer) As CloseBrackets
FROM @T
)
The select statement:
SELECT CASE WHEN OpenBrackets > 0 AND CloseBrackets > OpenBrackets
THEN
STUFF(Scorer, OpenBrackets, CloseBrackets - OpenBrackets + 1, '')
ELSE
Scorer
END As Scorer
FROM CTE
Results:
Scorer
Ellis J.
Ellis J.
Ellis J.
Trueman J.
Brough D.
McGillvary J.
McGillvary J.
Brough D.
Ellis J.
a row with in the middle
Just an open bracket (forgot to close
Just a close bracket forgot to open)
no brackets at all
brackets ) in reversed order (
add a comment |
Below query works for you
SELECT LTRIM(RTRIM(REPLACE(Scorer, SUBSTRING(Scorer, CHARINDEX('(', Scorer), CHARINDEX(')', Scorer) - CHARINDEX('(', Scorer) + 1), '')))
FROM <TABLENAME>
add a comment |
These two pieces of information (the name and action) should not be in the same column. You should create a separate column for name and for action. And if the position of the action (before or after the name) is important, you might even need an additional column for that.
When you have migrated your data after that - in other words when you have cleaned up - you could still create a view or a computed column to output the scorer
the way you do now, for example
ALTER TABLE my_table ADD scorer AS athlete_name + ' (' + action + ')'
add a comment |
You could try:
SELECT Scorer
,CASE WHEN PATINDEX('%(%)%',Scorer) > 1
THEN LEFT(Scorer, PATINDEX('%(%)%',Scorer)-1)
ELSE RIGHT (Scorer, LEN(Scorer) - CHARINDEX(')',Scorer,1) )
END AS ColumnName
FROM ScoreTable
this should work assuming you only expect 1 instance if the pattern per row, but will work whether the "()" data is at the front or the back of the values
the solution assumes the scorer's name will be entirely to the left or right of the "(%)" pattern. you may need to tweak the code in the expression slightly to accommodate your case
– Gleb
Jan 20 at 13:15
add a comment |
You can use this query
with t(str) as
(
select 'Ellis J.(Conversion Goal)' union all
select '(Conversion Goal)Brough D.' union all
select ' (Try)McGillvary J.'
)
select (case when charindex('(', ltrim(str)) = 1 then
substring(str,charindex(')', str)+1,len(str))
else
left(str, charindex('(', str) - 1)
end) as "Scorers"
from t
Scorers
--------------
Ellis J.
Brough D.
McGillvary J.
by contribution of substring
, charindex
and left
functions together. ltrim
is used against probabilty of spaces left before (
character at the beginning of the string.
Rextester Demo
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%2f54276358%2fhow-to-split-string-with-inconsistent-order-format-in-sql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
The correct solution would be to fix the database structure by adding another column to the table for the score type. In fact, you should probably have a table for score types and add a foreign key to it from this table.
Assuming you can't change the database structure, this is better done at the presentation layer. Any programming language should enable you do do it quite easily. String manipulation is not SQL's strong suit.
That being said, it can certainly be done using pure T-SQL - with a simple common table expression to get the brackets indexes using charindex
, and a case expression with stuff
in the select statement.
First, create and populate sample table (Please save us this step in your future questions):
DECLARE @T AS TABLE
(
Scorer nvarchar(100)
);
INSERT INTO @T (Scorer) VALUES
('Ellis J.(Conversion Goal)'),
('Ellis J.(Try)'),
('Ellis J.(Conversion Goal)'),
('Trueman J.(Try)'),
('(Conversion Goal)Brough D.'),
('(Try)McGillvary J.'),
('(Try)McGillvary J.'),
('(Penalty Goal)Brough D.'),
('Ellis J.(Conversion Goal)'),
-- Note: I've added some edge cases to the sample data:
('a row with (brackets) in the middle'),
('Just an open bracket (forgot to close '),
('Just a close bracket forgot to open)'),
('no brackets at all'),
('brackets ) in reversed order (');
Then, the CTE:
WITH CTE AS
(
SELECT Scorer,
CHARINDEX('(', Scorer) As OpenBrackets,
CHARINDEX(')', Scorer) As CloseBrackets
FROM @T
)
The select statement:
SELECT CASE WHEN OpenBrackets > 0 AND CloseBrackets > OpenBrackets
THEN
STUFF(Scorer, OpenBrackets, CloseBrackets - OpenBrackets + 1, '')
ELSE
Scorer
END As Scorer
FROM CTE
Results:
Scorer
Ellis J.
Ellis J.
Ellis J.
Trueman J.
Brough D.
McGillvary J.
McGillvary J.
Brough D.
Ellis J.
a row with in the middle
Just an open bracket (forgot to close
Just a close bracket forgot to open)
no brackets at all
brackets ) in reversed order (
add a comment |
The correct solution would be to fix the database structure by adding another column to the table for the score type. In fact, you should probably have a table for score types and add a foreign key to it from this table.
Assuming you can't change the database structure, this is better done at the presentation layer. Any programming language should enable you do do it quite easily. String manipulation is not SQL's strong suit.
That being said, it can certainly be done using pure T-SQL - with a simple common table expression to get the brackets indexes using charindex
, and a case expression with stuff
in the select statement.
First, create and populate sample table (Please save us this step in your future questions):
DECLARE @T AS TABLE
(
Scorer nvarchar(100)
);
INSERT INTO @T (Scorer) VALUES
('Ellis J.(Conversion Goal)'),
('Ellis J.(Try)'),
('Ellis J.(Conversion Goal)'),
('Trueman J.(Try)'),
('(Conversion Goal)Brough D.'),
('(Try)McGillvary J.'),
('(Try)McGillvary J.'),
('(Penalty Goal)Brough D.'),
('Ellis J.(Conversion Goal)'),
-- Note: I've added some edge cases to the sample data:
('a row with (brackets) in the middle'),
('Just an open bracket (forgot to close '),
('Just a close bracket forgot to open)'),
('no brackets at all'),
('brackets ) in reversed order (');
Then, the CTE:
WITH CTE AS
(
SELECT Scorer,
CHARINDEX('(', Scorer) As OpenBrackets,
CHARINDEX(')', Scorer) As CloseBrackets
FROM @T
)
The select statement:
SELECT CASE WHEN OpenBrackets > 0 AND CloseBrackets > OpenBrackets
THEN
STUFF(Scorer, OpenBrackets, CloseBrackets - OpenBrackets + 1, '')
ELSE
Scorer
END As Scorer
FROM CTE
Results:
Scorer
Ellis J.
Ellis J.
Ellis J.
Trueman J.
Brough D.
McGillvary J.
McGillvary J.
Brough D.
Ellis J.
a row with in the middle
Just an open bracket (forgot to close
Just a close bracket forgot to open)
no brackets at all
brackets ) in reversed order (
add a comment |
The correct solution would be to fix the database structure by adding another column to the table for the score type. In fact, you should probably have a table for score types and add a foreign key to it from this table.
Assuming you can't change the database structure, this is better done at the presentation layer. Any programming language should enable you do do it quite easily. String manipulation is not SQL's strong suit.
That being said, it can certainly be done using pure T-SQL - with a simple common table expression to get the brackets indexes using charindex
, and a case expression with stuff
in the select statement.
First, create and populate sample table (Please save us this step in your future questions):
DECLARE @T AS TABLE
(
Scorer nvarchar(100)
);
INSERT INTO @T (Scorer) VALUES
('Ellis J.(Conversion Goal)'),
('Ellis J.(Try)'),
('Ellis J.(Conversion Goal)'),
('Trueman J.(Try)'),
('(Conversion Goal)Brough D.'),
('(Try)McGillvary J.'),
('(Try)McGillvary J.'),
('(Penalty Goal)Brough D.'),
('Ellis J.(Conversion Goal)'),
-- Note: I've added some edge cases to the sample data:
('a row with (brackets) in the middle'),
('Just an open bracket (forgot to close '),
('Just a close bracket forgot to open)'),
('no brackets at all'),
('brackets ) in reversed order (');
Then, the CTE:
WITH CTE AS
(
SELECT Scorer,
CHARINDEX('(', Scorer) As OpenBrackets,
CHARINDEX(')', Scorer) As CloseBrackets
FROM @T
)
The select statement:
SELECT CASE WHEN OpenBrackets > 0 AND CloseBrackets > OpenBrackets
THEN
STUFF(Scorer, OpenBrackets, CloseBrackets - OpenBrackets + 1, '')
ELSE
Scorer
END As Scorer
FROM CTE
Results:
Scorer
Ellis J.
Ellis J.
Ellis J.
Trueman J.
Brough D.
McGillvary J.
McGillvary J.
Brough D.
Ellis J.
a row with in the middle
Just an open bracket (forgot to close
Just a close bracket forgot to open)
no brackets at all
brackets ) in reversed order (
The correct solution would be to fix the database structure by adding another column to the table for the score type. In fact, you should probably have a table for score types and add a foreign key to it from this table.
Assuming you can't change the database structure, this is better done at the presentation layer. Any programming language should enable you do do it quite easily. String manipulation is not SQL's strong suit.
That being said, it can certainly be done using pure T-SQL - with a simple common table expression to get the brackets indexes using charindex
, and a case expression with stuff
in the select statement.
First, create and populate sample table (Please save us this step in your future questions):
DECLARE @T AS TABLE
(
Scorer nvarchar(100)
);
INSERT INTO @T (Scorer) VALUES
('Ellis J.(Conversion Goal)'),
('Ellis J.(Try)'),
('Ellis J.(Conversion Goal)'),
('Trueman J.(Try)'),
('(Conversion Goal)Brough D.'),
('(Try)McGillvary J.'),
('(Try)McGillvary J.'),
('(Penalty Goal)Brough D.'),
('Ellis J.(Conversion Goal)'),
-- Note: I've added some edge cases to the sample data:
('a row with (brackets) in the middle'),
('Just an open bracket (forgot to close '),
('Just a close bracket forgot to open)'),
('no brackets at all'),
('brackets ) in reversed order (');
Then, the CTE:
WITH CTE AS
(
SELECT Scorer,
CHARINDEX('(', Scorer) As OpenBrackets,
CHARINDEX(')', Scorer) As CloseBrackets
FROM @T
)
The select statement:
SELECT CASE WHEN OpenBrackets > 0 AND CloseBrackets > OpenBrackets
THEN
STUFF(Scorer, OpenBrackets, CloseBrackets - OpenBrackets + 1, '')
ELSE
Scorer
END As Scorer
FROM CTE
Results:
Scorer
Ellis J.
Ellis J.
Ellis J.
Trueman J.
Brough D.
McGillvary J.
McGillvary J.
Brough D.
Ellis J.
a row with in the middle
Just an open bracket (forgot to close
Just a close bracket forgot to open)
no brackets at all
brackets ) in reversed order (
edited Jan 20 at 13:21
answered Jan 20 at 12:48
Zohar PeledZohar Peled
54k73273
54k73273
add a comment |
add a comment |
Below query works for you
SELECT LTRIM(RTRIM(REPLACE(Scorer, SUBSTRING(Scorer, CHARINDEX('(', Scorer), CHARINDEX(')', Scorer) - CHARINDEX('(', Scorer) + 1), '')))
FROM <TABLENAME>
add a comment |
Below query works for you
SELECT LTRIM(RTRIM(REPLACE(Scorer, SUBSTRING(Scorer, CHARINDEX('(', Scorer), CHARINDEX(')', Scorer) - CHARINDEX('(', Scorer) + 1), '')))
FROM <TABLENAME>
add a comment |
Below query works for you
SELECT LTRIM(RTRIM(REPLACE(Scorer, SUBSTRING(Scorer, CHARINDEX('(', Scorer), CHARINDEX(')', Scorer) - CHARINDEX('(', Scorer) + 1), '')))
FROM <TABLENAME>
Below query works for you
SELECT LTRIM(RTRIM(REPLACE(Scorer, SUBSTRING(Scorer, CHARINDEX('(', Scorer), CHARINDEX(')', Scorer) - CHARINDEX('(', Scorer) + 1), '')))
FROM <TABLENAME>
answered Jan 20 at 12:45
Venkatesh RVenkatesh R
193115
193115
add a comment |
add a comment |
These two pieces of information (the name and action) should not be in the same column. You should create a separate column for name and for action. And if the position of the action (before or after the name) is important, you might even need an additional column for that.
When you have migrated your data after that - in other words when you have cleaned up - you could still create a view or a computed column to output the scorer
the way you do now, for example
ALTER TABLE my_table ADD scorer AS athlete_name + ' (' + action + ')'
add a comment |
These two pieces of information (the name and action) should not be in the same column. You should create a separate column for name and for action. And if the position of the action (before or after the name) is important, you might even need an additional column for that.
When you have migrated your data after that - in other words when you have cleaned up - you could still create a view or a computed column to output the scorer
the way you do now, for example
ALTER TABLE my_table ADD scorer AS athlete_name + ' (' + action + ')'
add a comment |
These two pieces of information (the name and action) should not be in the same column. You should create a separate column for name and for action. And if the position of the action (before or after the name) is important, you might even need an additional column for that.
When you have migrated your data after that - in other words when you have cleaned up - you could still create a view or a computed column to output the scorer
the way you do now, for example
ALTER TABLE my_table ADD scorer AS athlete_name + ' (' + action + ')'
These two pieces of information (the name and action) should not be in the same column. You should create a separate column for name and for action. And if the position of the action (before or after the name) is important, you might even need an additional column for that.
When you have migrated your data after that - in other words when you have cleaned up - you could still create a view or a computed column to output the scorer
the way you do now, for example
ALTER TABLE my_table ADD scorer AS athlete_name + ' (' + action + ')'
answered Jan 20 at 12:51
Gert-JanGert-Jan
446
446
add a comment |
add a comment |
You could try:
SELECT Scorer
,CASE WHEN PATINDEX('%(%)%',Scorer) > 1
THEN LEFT(Scorer, PATINDEX('%(%)%',Scorer)-1)
ELSE RIGHT (Scorer, LEN(Scorer) - CHARINDEX(')',Scorer,1) )
END AS ColumnName
FROM ScoreTable
this should work assuming you only expect 1 instance if the pattern per row, but will work whether the "()" data is at the front or the back of the values
the solution assumes the scorer's name will be entirely to the left or right of the "(%)" pattern. you may need to tweak the code in the expression slightly to accommodate your case
– Gleb
Jan 20 at 13:15
add a comment |
You could try:
SELECT Scorer
,CASE WHEN PATINDEX('%(%)%',Scorer) > 1
THEN LEFT(Scorer, PATINDEX('%(%)%',Scorer)-1)
ELSE RIGHT (Scorer, LEN(Scorer) - CHARINDEX(')',Scorer,1) )
END AS ColumnName
FROM ScoreTable
this should work assuming you only expect 1 instance if the pattern per row, but will work whether the "()" data is at the front or the back of the values
the solution assumes the scorer's name will be entirely to the left or right of the "(%)" pattern. you may need to tweak the code in the expression slightly to accommodate your case
– Gleb
Jan 20 at 13:15
add a comment |
You could try:
SELECT Scorer
,CASE WHEN PATINDEX('%(%)%',Scorer) > 1
THEN LEFT(Scorer, PATINDEX('%(%)%',Scorer)-1)
ELSE RIGHT (Scorer, LEN(Scorer) - CHARINDEX(')',Scorer,1) )
END AS ColumnName
FROM ScoreTable
this should work assuming you only expect 1 instance if the pattern per row, but will work whether the "()" data is at the front or the back of the values
You could try:
SELECT Scorer
,CASE WHEN PATINDEX('%(%)%',Scorer) > 1
THEN LEFT(Scorer, PATINDEX('%(%)%',Scorer)-1)
ELSE RIGHT (Scorer, LEN(Scorer) - CHARINDEX(')',Scorer,1) )
END AS ColumnName
FROM ScoreTable
this should work assuming you only expect 1 instance if the pattern per row, but will work whether the "()" data is at the front or the back of the values
answered Jan 20 at 12:52
GlebGleb
113
113
the solution assumes the scorer's name will be entirely to the left or right of the "(%)" pattern. you may need to tweak the code in the expression slightly to accommodate your case
– Gleb
Jan 20 at 13:15
add a comment |
the solution assumes the scorer's name will be entirely to the left or right of the "(%)" pattern. you may need to tweak the code in the expression slightly to accommodate your case
– Gleb
Jan 20 at 13:15
the solution assumes the scorer's name will be entirely to the left or right of the "(%)" pattern. you may need to tweak the code in the expression slightly to accommodate your case
– Gleb
Jan 20 at 13:15
the solution assumes the scorer's name will be entirely to the left or right of the "(%)" pattern. you may need to tweak the code in the expression slightly to accommodate your case
– Gleb
Jan 20 at 13:15
add a comment |
You can use this query
with t(str) as
(
select 'Ellis J.(Conversion Goal)' union all
select '(Conversion Goal)Brough D.' union all
select ' (Try)McGillvary J.'
)
select (case when charindex('(', ltrim(str)) = 1 then
substring(str,charindex(')', str)+1,len(str))
else
left(str, charindex('(', str) - 1)
end) as "Scorers"
from t
Scorers
--------------
Ellis J.
Brough D.
McGillvary J.
by contribution of substring
, charindex
and left
functions together. ltrim
is used against probabilty of spaces left before (
character at the beginning of the string.
Rextester Demo
add a comment |
You can use this query
with t(str) as
(
select 'Ellis J.(Conversion Goal)' union all
select '(Conversion Goal)Brough D.' union all
select ' (Try)McGillvary J.'
)
select (case when charindex('(', ltrim(str)) = 1 then
substring(str,charindex(')', str)+1,len(str))
else
left(str, charindex('(', str) - 1)
end) as "Scorers"
from t
Scorers
--------------
Ellis J.
Brough D.
McGillvary J.
by contribution of substring
, charindex
and left
functions together. ltrim
is used against probabilty of spaces left before (
character at the beginning of the string.
Rextester Demo
add a comment |
You can use this query
with t(str) as
(
select 'Ellis J.(Conversion Goal)' union all
select '(Conversion Goal)Brough D.' union all
select ' (Try)McGillvary J.'
)
select (case when charindex('(', ltrim(str)) = 1 then
substring(str,charindex(')', str)+1,len(str))
else
left(str, charindex('(', str) - 1)
end) as "Scorers"
from t
Scorers
--------------
Ellis J.
Brough D.
McGillvary J.
by contribution of substring
, charindex
and left
functions together. ltrim
is used against probabilty of spaces left before (
character at the beginning of the string.
Rextester Demo
You can use this query
with t(str) as
(
select 'Ellis J.(Conversion Goal)' union all
select '(Conversion Goal)Brough D.' union all
select ' (Try)McGillvary J.'
)
select (case when charindex('(', ltrim(str)) = 1 then
substring(str,charindex(')', str)+1,len(str))
else
left(str, charindex('(', str) - 1)
end) as "Scorers"
from t
Scorers
--------------
Ellis J.
Brough D.
McGillvary J.
by contribution of substring
, charindex
and left
functions together. ltrim
is used against probabilty of spaces left before (
character at the beginning of the string.
Rextester Demo
edited Jan 20 at 14:11
answered Jan 20 at 12:46
Barbaros ÖzhanBarbaros Özhan
13.3k71633
13.3k71633
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.
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%2f54276358%2fhow-to-split-string-with-inconsistent-order-format-in-sql%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
1
Welcomde to stackoverflow. Please take a minute to take the tour, especially How to Ask. Sample data is best served as DDL + DML. Please edit your question to include it, your current attempt and your desired results. For more details, read this.
– Zohar Peled
Jan 20 at 12:24