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









share|improve this question
























  • 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















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









share|improve this question
























  • 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













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









share|improve this question















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-server tsql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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.






share|improve this answer






























    up vote
    0
    down vote













    you can try using substring() and charindex() function



    select substring(EmployeeName,0,charindex(' ',EmployeeName))
    from tablename





    share|improve this answer




























      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






      share|improve this answer





















      • 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










      • 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


















      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





      share|improve this answer





















        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',
        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
        });


        }
        });














        draft saved

        draft discarded


















        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

























        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.






        share|improve this answer



























          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.






          share|improve this answer

























            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.






            share|improve this answer














            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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 19 at 11:56

























            answered Nov 19 at 11:51









            Zohar Peled

            51.6k73172




            51.6k73172
























                up vote
                0
                down vote













                you can try using substring() and charindex() function



                select substring(EmployeeName,0,charindex(' ',EmployeeName))
                from tablename





                share|improve this answer

























                  up vote
                  0
                  down vote













                  you can try using substring() and charindex() function



                  select substring(EmployeeName,0,charindex(' ',EmployeeName))
                  from tablename





                  share|improve this answer























                    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





                    share|improve this answer












                    you can try using substring() and charindex() function



                    select substring(EmployeeName,0,charindex(' ',EmployeeName))
                    from tablename






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 19 at 11:50









                    fa06

                    9,5781917




                    9,5781917






















                        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






                        share|improve this answer





















                        • 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










                        • 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















                        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






                        share|improve this answer





















                        • 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










                        • 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













                        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






                        share|improve this answer












                        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







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Nov 19 at 11:52









                        Zdravko Danev

                        10.3k12541




                        10.3k12541












                        • 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










                        • 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










                        • 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










                        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





                        share|improve this answer

























                          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





                          share|improve this answer























                            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





                            share|improve this answer












                            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






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 19 at 11:58









                            Rahul Neekhra

                            597627




                            597627






























                                draft saved

                                draft discarded




















































                                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.




                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                Ottavio Pratesi

                                Tricia Helfer

                                15 giugno