How to retrieve one text string from one column which contains two text string?
up vote
-3
down vote
favorite
Let us consider below a table.
Input Table
Emp_ID EmployeeName
1 Mark hos
2 jhon carte
3 Mike hold
4 Mark Danny
5 Stacy hodegf
How to retrieve Output like below
Output Table
Mark
jhon
Mike
Mark
Stacy
sql
add a comment |
up vote
-3
down vote
favorite
Let us consider below a table.
Input Table
Emp_ID EmployeeName
1 Mark hos
2 jhon carte
3 Mike hold
4 Mark Danny
5 Stacy hodegf
How to retrieve Output like below
Output Table
Mark
jhon
Mike
Mark
Stacy
sql
Please tag your question with the database you are using.
– Gordon Linoff
Nov 19 at 11:49
What have you already tried in order to achieve this?
– Martin Parkin
Nov 19 at 11:49
add a comment |
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
Let us consider below a table.
Input Table
Emp_ID EmployeeName
1 Mark hos
2 jhon carte
3 Mike hold
4 Mark Danny
5 Stacy hodegf
How to retrieve Output like below
Output Table
Mark
jhon
Mike
Mark
Stacy
sql
Let us consider below a table.
Input Table
Emp_ID EmployeeName
1 Mark hos
2 jhon carte
3 Mike hold
4 Mark Danny
5 Stacy hodegf
How to retrieve Output like below
Output Table
Mark
jhon
Mike
Mark
Stacy
sql
sql
edited Nov 19 at 11:59
Rahul Neekhra
597627
597627
asked Nov 19 at 11:47
Yogesh Danve
1
1
Please tag your question with the database you are using.
– Gordon Linoff
Nov 19 at 11:49
What have you already tried in order to achieve this?
– Martin Parkin
Nov 19 at 11:49
add a comment |
Please tag your question with the database you are using.
– Gordon Linoff
Nov 19 at 11:49
What have you already tried in order to achieve this?
– Martin Parkin
Nov 19 at 11:49
Please tag your question with the database you are using.
– Gordon Linoff
Nov 19 at 11:49
Please tag your question with the database you are using.
– Gordon Linoff
Nov 19 at 11:49
What have you already tried in order to achieve this?
– Martin Parkin
Nov 19 at 11:49
What have you already tried in order to achieve this?
– Martin Parkin
Nov 19 at 11:49
add a comment |
4 Answers
4
active
oldest
votes
up vote
2
down vote
You can use Left with CharIndex:
First, Create and populate sample table (Please save us this step in your future questions)
DECLARE @T AS TABLE
(
Emp_ID int,
EmployeeName varchar(20)
)
INSERT INTO @T (Emp_Id, EmployeeName ) VALUES
(1, 'Mark hos'),
(2, 'jhon carte'),
(3, 'Mike hold'),
(4, 'Mark Danny'),
(5, 'Stacy hodegf'),
(6, 'NoSpaceHere')
The query:
SELECT Emp_Id, LEFT(EmployeeName, CHARINDEX(' ', EmployeeName +' ')) As FirstName
FROM @T
Results:
Emp_Id FirstName
1 Mark
2 jhon
3 Mike
4 Mark
5 Stacy
6 NoSpaceHere
Please note that I've added a trailing space to the EmployeeName in the charindex function to return the entire EmployeeName in case there is no space there.
add a comment |
up vote
0
down vote
you can try using substring() and charindex() function
select substring(EmployeeName,0,charindex(' ',EmployeeName))
from tablename
add a comment |
up vote
0
down vote
you can use SUBSTRING, CHARINDEX as the other answers suggest, or you can take a look at STRING_SPLIT which is probably a better solution:
https://docs.microsoft.com/en-us/sql/t-sql/functions/string-split-transact-sql?view=sql-server-2017
What makes you thinkstring_splitwould be a better solution?
– Zohar Peled
Nov 19 at 11:57
seems cleaner, nothing wrong with SUBSTRING
– Zdravko Danev
Nov 19 at 12:06
How would it be cleaner? I would love to see a cleaner solution to this particular problem using string_split.
– Zohar Peled
Nov 19 at 12:09
add a comment |
up vote
0
down vote
CREATE TABLE #TempEmp
(
Emp_ID INT,
EmployeeName VARCHAR(100)
)
INSERT INTO #TempEmp
SELECT 1,'Mark hos' UNION
SELECT 2,'jhon carte' UNION
SELECT 3,'Mike hold' UNION
SELECT 4,'Mark Danny' UNION
SELECT 5,'Stacy hodegf'
SELECT
SUBSTRING(EmployeeName, 1, CASE WHEN CHARINDEX(' ', EmployeeName)>0 THEN CHARINDEX(' ', EmployeeName) - 1 ELSE LEN(EmployeeName) END ) AS Firstname FROM #TempEmp
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You can use Left with CharIndex:
First, Create and populate sample table (Please save us this step in your future questions)
DECLARE @T AS TABLE
(
Emp_ID int,
EmployeeName varchar(20)
)
INSERT INTO @T (Emp_Id, EmployeeName ) VALUES
(1, 'Mark hos'),
(2, 'jhon carte'),
(3, 'Mike hold'),
(4, 'Mark Danny'),
(5, 'Stacy hodegf'),
(6, 'NoSpaceHere')
The query:
SELECT Emp_Id, LEFT(EmployeeName, CHARINDEX(' ', EmployeeName +' ')) As FirstName
FROM @T
Results:
Emp_Id FirstName
1 Mark
2 jhon
3 Mike
4 Mark
5 Stacy
6 NoSpaceHere
Please note that I've added a trailing space to the EmployeeName in the charindex function to return the entire EmployeeName in case there is no space there.
add a comment |
up vote
2
down vote
You can use Left with CharIndex:
First, Create and populate sample table (Please save us this step in your future questions)
DECLARE @T AS TABLE
(
Emp_ID int,
EmployeeName varchar(20)
)
INSERT INTO @T (Emp_Id, EmployeeName ) VALUES
(1, 'Mark hos'),
(2, 'jhon carte'),
(3, 'Mike hold'),
(4, 'Mark Danny'),
(5, 'Stacy hodegf'),
(6, 'NoSpaceHere')
The query:
SELECT Emp_Id, LEFT(EmployeeName, CHARINDEX(' ', EmployeeName +' ')) As FirstName
FROM @T
Results:
Emp_Id FirstName
1 Mark
2 jhon
3 Mike
4 Mark
5 Stacy
6 NoSpaceHere
Please note that I've added a trailing space to the EmployeeName in the charindex function to return the entire EmployeeName in case there is no space there.
add a comment |
up vote
2
down vote
up vote
2
down vote
You can use Left with CharIndex:
First, Create and populate sample table (Please save us this step in your future questions)
DECLARE @T AS TABLE
(
Emp_ID int,
EmployeeName varchar(20)
)
INSERT INTO @T (Emp_Id, EmployeeName ) VALUES
(1, 'Mark hos'),
(2, 'jhon carte'),
(3, 'Mike hold'),
(4, 'Mark Danny'),
(5, 'Stacy hodegf'),
(6, 'NoSpaceHere')
The query:
SELECT Emp_Id, LEFT(EmployeeName, CHARINDEX(' ', EmployeeName +' ')) As FirstName
FROM @T
Results:
Emp_Id FirstName
1 Mark
2 jhon
3 Mike
4 Mark
5 Stacy
6 NoSpaceHere
Please note that I've added a trailing space to the EmployeeName in the charindex function to return the entire EmployeeName in case there is no space there.
You can use Left with CharIndex:
First, Create and populate sample table (Please save us this step in your future questions)
DECLARE @T AS TABLE
(
Emp_ID int,
EmployeeName varchar(20)
)
INSERT INTO @T (Emp_Id, EmployeeName ) VALUES
(1, 'Mark hos'),
(2, 'jhon carte'),
(3, 'Mike hold'),
(4, 'Mark Danny'),
(5, 'Stacy hodegf'),
(6, 'NoSpaceHere')
The query:
SELECT Emp_Id, LEFT(EmployeeName, CHARINDEX(' ', EmployeeName +' ')) As FirstName
FROM @T
Results:
Emp_Id FirstName
1 Mark
2 jhon
3 Mike
4 Mark
5 Stacy
6 NoSpaceHere
Please note that I've added a trailing space to the EmployeeName in the charindex function to return the entire EmployeeName in case there is no space there.
edited Nov 19 at 11:56
answered Nov 19 at 11:51
Zohar Peled
51.6k73172
51.6k73172
add a comment |
add a comment |
up vote
0
down vote
you can try using substring() and charindex() function
select substring(EmployeeName,0,charindex(' ',EmployeeName))
from tablename
add a comment |
up vote
0
down vote
you can try using substring() and charindex() function
select substring(EmployeeName,0,charindex(' ',EmployeeName))
from tablename
add a comment |
up vote
0
down vote
up vote
0
down vote
you can try using substring() and charindex() function
select substring(EmployeeName,0,charindex(' ',EmployeeName))
from tablename
you can try using substring() and charindex() function
select substring(EmployeeName,0,charindex(' ',EmployeeName))
from tablename
answered Nov 19 at 11:50
fa06
9,5781917
9,5781917
add a comment |
add a comment |
up vote
0
down vote
you can use SUBSTRING, CHARINDEX as the other answers suggest, or you can take a look at STRING_SPLIT which is probably a better solution:
https://docs.microsoft.com/en-us/sql/t-sql/functions/string-split-transact-sql?view=sql-server-2017
What makes you thinkstring_splitwould be a better solution?
– Zohar Peled
Nov 19 at 11:57
seems cleaner, nothing wrong with SUBSTRING
– Zdravko Danev
Nov 19 at 12:06
How would it be cleaner? I would love to see a cleaner solution to this particular problem using string_split.
– Zohar Peled
Nov 19 at 12:09
add a comment |
up vote
0
down vote
you can use SUBSTRING, CHARINDEX as the other answers suggest, or you can take a look at STRING_SPLIT which is probably a better solution:
https://docs.microsoft.com/en-us/sql/t-sql/functions/string-split-transact-sql?view=sql-server-2017
What makes you thinkstring_splitwould be a better solution?
– Zohar Peled
Nov 19 at 11:57
seems cleaner, nothing wrong with SUBSTRING
– Zdravko Danev
Nov 19 at 12:06
How would it be cleaner? I would love to see a cleaner solution to this particular problem using string_split.
– Zohar Peled
Nov 19 at 12:09
add a comment |
up vote
0
down vote
up vote
0
down vote
you can use SUBSTRING, CHARINDEX as the other answers suggest, or you can take a look at STRING_SPLIT which is probably a better solution:
https://docs.microsoft.com/en-us/sql/t-sql/functions/string-split-transact-sql?view=sql-server-2017
you can use SUBSTRING, CHARINDEX as the other answers suggest, or you can take a look at STRING_SPLIT which is probably a better solution:
https://docs.microsoft.com/en-us/sql/t-sql/functions/string-split-transact-sql?view=sql-server-2017
answered Nov 19 at 11:52
Zdravko Danev
10.3k12541
10.3k12541
What makes you thinkstring_splitwould be a better solution?
– Zohar Peled
Nov 19 at 11:57
seems cleaner, nothing wrong with SUBSTRING
– Zdravko Danev
Nov 19 at 12:06
How would it be cleaner? I would love to see a cleaner solution to this particular problem using string_split.
– Zohar Peled
Nov 19 at 12:09
add a comment |
What makes you thinkstring_splitwould be a better solution?
– Zohar Peled
Nov 19 at 11:57
seems cleaner, nothing wrong with SUBSTRING
– Zdravko Danev
Nov 19 at 12:06
How would it be cleaner? I would love to see a cleaner solution to this particular problem using string_split.
– Zohar Peled
Nov 19 at 12:09
What makes you think
string_split would be a better solution?– Zohar Peled
Nov 19 at 11:57
What makes you think
string_split would be a better solution?– Zohar Peled
Nov 19 at 11:57
seems cleaner, nothing wrong with SUBSTRING
– Zdravko Danev
Nov 19 at 12:06
seems cleaner, nothing wrong with SUBSTRING
– Zdravko Danev
Nov 19 at 12:06
How would it be cleaner? I would love to see a cleaner solution to this particular problem using string_split.
– Zohar Peled
Nov 19 at 12:09
How would it be cleaner? I would love to see a cleaner solution to this particular problem using string_split.
– Zohar Peled
Nov 19 at 12:09
add a comment |
up vote
0
down vote
CREATE TABLE #TempEmp
(
Emp_ID INT,
EmployeeName VARCHAR(100)
)
INSERT INTO #TempEmp
SELECT 1,'Mark hos' UNION
SELECT 2,'jhon carte' UNION
SELECT 3,'Mike hold' UNION
SELECT 4,'Mark Danny' UNION
SELECT 5,'Stacy hodegf'
SELECT
SUBSTRING(EmployeeName, 1, CASE WHEN CHARINDEX(' ', EmployeeName)>0 THEN CHARINDEX(' ', EmployeeName) - 1 ELSE LEN(EmployeeName) END ) AS Firstname FROM #TempEmp
add a comment |
up vote
0
down vote
CREATE TABLE #TempEmp
(
Emp_ID INT,
EmployeeName VARCHAR(100)
)
INSERT INTO #TempEmp
SELECT 1,'Mark hos' UNION
SELECT 2,'jhon carte' UNION
SELECT 3,'Mike hold' UNION
SELECT 4,'Mark Danny' UNION
SELECT 5,'Stacy hodegf'
SELECT
SUBSTRING(EmployeeName, 1, CASE WHEN CHARINDEX(' ', EmployeeName)>0 THEN CHARINDEX(' ', EmployeeName) - 1 ELSE LEN(EmployeeName) END ) AS Firstname FROM #TempEmp
add a comment |
up vote
0
down vote
up vote
0
down vote
CREATE TABLE #TempEmp
(
Emp_ID INT,
EmployeeName VARCHAR(100)
)
INSERT INTO #TempEmp
SELECT 1,'Mark hos' UNION
SELECT 2,'jhon carte' UNION
SELECT 3,'Mike hold' UNION
SELECT 4,'Mark Danny' UNION
SELECT 5,'Stacy hodegf'
SELECT
SUBSTRING(EmployeeName, 1, CASE WHEN CHARINDEX(' ', EmployeeName)>0 THEN CHARINDEX(' ', EmployeeName) - 1 ELSE LEN(EmployeeName) END ) AS Firstname FROM #TempEmp
CREATE TABLE #TempEmp
(
Emp_ID INT,
EmployeeName VARCHAR(100)
)
INSERT INTO #TempEmp
SELECT 1,'Mark hos' UNION
SELECT 2,'jhon carte' UNION
SELECT 3,'Mike hold' UNION
SELECT 4,'Mark Danny' UNION
SELECT 5,'Stacy hodegf'
SELECT
SUBSTRING(EmployeeName, 1, CASE WHEN CHARINDEX(' ', EmployeeName)>0 THEN CHARINDEX(' ', EmployeeName) - 1 ELSE LEN(EmployeeName) END ) AS Firstname FROM #TempEmp
answered Nov 19 at 11:58
Rahul Neekhra
597627
597627
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%2f53374002%2fhow-to-retrieve-one-text-string-from-one-column-which-contains-two-text-string%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
Please tag your question with the database you are using.
– Gordon Linoff
Nov 19 at 11:49
What have you already tried in order to achieve this?
– Martin Parkin
Nov 19 at 11:49