Remove numbers from string sql server












14















I have a large database in which I want to do a part string search. The user will enter characters: JoeBloggs.



For arguments sake if I had a name Joe 23 Blo Ggs 4 in the database. I want to remove everything in the name other than A-Z.



I have the REPLACE(Name, ' ','') function to remove spaces and the UPPER() function to capitalize the name.



Is there a more efficient fast way maybe by terms of regex to replace anything other than A-Z. I cannot change the values in the database.



Thanks in advance










share|improve this question























  • You say you can't change the data. Can you add a computed column to the existing table? Or add a new table with a foreign key and your computed value?

    – Laurence
    Nov 5 '12 at 21:04











  • @Laurence - Yea I suppose I can do this but can we do this in a temporary table in a stored procedure? If not then I can request this computed column, all i need is the function to do the replace. Thanks for your prompt response

    – CR41G14
    Nov 5 '12 at 21:07











  • I'd not recommend using regexps if performance matters. The way you use is obvious but still a good one!

    – vyakhir
    Nov 5 '12 at 21:13











  • @DmitryVyakhirev - There could be some rouge characters like ' " @ /, it is a messy database so will I need to do multiple REPLACES to get rid of every instance of a bad character

    – CR41G14
    Nov 5 '12 at 21:19
















14















I have a large database in which I want to do a part string search. The user will enter characters: JoeBloggs.



For arguments sake if I had a name Joe 23 Blo Ggs 4 in the database. I want to remove everything in the name other than A-Z.



I have the REPLACE(Name, ' ','') function to remove spaces and the UPPER() function to capitalize the name.



Is there a more efficient fast way maybe by terms of regex to replace anything other than A-Z. I cannot change the values in the database.



Thanks in advance










share|improve this question























  • You say you can't change the data. Can you add a computed column to the existing table? Or add a new table with a foreign key and your computed value?

    – Laurence
    Nov 5 '12 at 21:04











  • @Laurence - Yea I suppose I can do this but can we do this in a temporary table in a stored procedure? If not then I can request this computed column, all i need is the function to do the replace. Thanks for your prompt response

    – CR41G14
    Nov 5 '12 at 21:07











  • I'd not recommend using regexps if performance matters. The way you use is obvious but still a good one!

    – vyakhir
    Nov 5 '12 at 21:13











  • @DmitryVyakhirev - There could be some rouge characters like ' " @ /, it is a messy database so will I need to do multiple REPLACES to get rid of every instance of a bad character

    – CR41G14
    Nov 5 '12 at 21:19














14












14








14


4






I have a large database in which I want to do a part string search. The user will enter characters: JoeBloggs.



For arguments sake if I had a name Joe 23 Blo Ggs 4 in the database. I want to remove everything in the name other than A-Z.



I have the REPLACE(Name, ' ','') function to remove spaces and the UPPER() function to capitalize the name.



Is there a more efficient fast way maybe by terms of regex to replace anything other than A-Z. I cannot change the values in the database.



Thanks in advance










share|improve this question














I have a large database in which I want to do a part string search. The user will enter characters: JoeBloggs.



For arguments sake if I had a name Joe 23 Blo Ggs 4 in the database. I want to remove everything in the name other than A-Z.



I have the REPLACE(Name, ' ','') function to remove spaces and the UPPER() function to capitalize the name.



Is there a more efficient fast way maybe by terms of regex to replace anything other than A-Z. I cannot change the values in the database.



Thanks in advance







sql sql-server sql-server-2008






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 5 '12 at 21:00









CR41G14CR41G14

3,17653156




3,17653156













  • You say you can't change the data. Can you add a computed column to the existing table? Or add a new table with a foreign key and your computed value?

    – Laurence
    Nov 5 '12 at 21:04











  • @Laurence - Yea I suppose I can do this but can we do this in a temporary table in a stored procedure? If not then I can request this computed column, all i need is the function to do the replace. Thanks for your prompt response

    – CR41G14
    Nov 5 '12 at 21:07











  • I'd not recommend using regexps if performance matters. The way you use is obvious but still a good one!

    – vyakhir
    Nov 5 '12 at 21:13











  • @DmitryVyakhirev - There could be some rouge characters like ' " @ /, it is a messy database so will I need to do multiple REPLACES to get rid of every instance of a bad character

    – CR41G14
    Nov 5 '12 at 21:19



















  • You say you can't change the data. Can you add a computed column to the existing table? Or add a new table with a foreign key and your computed value?

    – Laurence
    Nov 5 '12 at 21:04











  • @Laurence - Yea I suppose I can do this but can we do this in a temporary table in a stored procedure? If not then I can request this computed column, all i need is the function to do the replace. Thanks for your prompt response

    – CR41G14
    Nov 5 '12 at 21:07











  • I'd not recommend using regexps if performance matters. The way you use is obvious but still a good one!

    – vyakhir
    Nov 5 '12 at 21:13











  • @DmitryVyakhirev - There could be some rouge characters like ' " @ /, it is a messy database so will I need to do multiple REPLACES to get rid of every instance of a bad character

    – CR41G14
    Nov 5 '12 at 21:19

















You say you can't change the data. Can you add a computed column to the existing table? Or add a new table with a foreign key and your computed value?

– Laurence
Nov 5 '12 at 21:04





You say you can't change the data. Can you add a computed column to the existing table? Or add a new table with a foreign key and your computed value?

– Laurence
Nov 5 '12 at 21:04













@Laurence - Yea I suppose I can do this but can we do this in a temporary table in a stored procedure? If not then I can request this computed column, all i need is the function to do the replace. Thanks for your prompt response

– CR41G14
Nov 5 '12 at 21:07





@Laurence - Yea I suppose I can do this but can we do this in a temporary table in a stored procedure? If not then I can request this computed column, all i need is the function to do the replace. Thanks for your prompt response

– CR41G14
Nov 5 '12 at 21:07













I'd not recommend using regexps if performance matters. The way you use is obvious but still a good one!

– vyakhir
Nov 5 '12 at 21:13





I'd not recommend using regexps if performance matters. The way you use is obvious but still a good one!

– vyakhir
Nov 5 '12 at 21:13













@DmitryVyakhirev - There could be some rouge characters like ' " @ /, it is a messy database so will I need to do multiple REPLACES to get rid of every instance of a bad character

– CR41G14
Nov 5 '12 at 21:19





@DmitryVyakhirev - There could be some rouge characters like ' " @ /, it is a messy database so will I need to do multiple REPLACES to get rid of every instance of a bad character

– CR41G14
Nov 5 '12 at 21:19












6 Answers
6






active

oldest

votes


















30














1st option -



You can nest REPLACE() functions up to 32 levels deep. It runs fast.



REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE
(REPLACE (@str, '0', ''),
'1', ''),
'2', ''),
'3', ''),
'4', ''),
'5', ''),
'6', ''),
'7', ''),
'8', ''),
'9', '')


2nd option --
do the reverse of -



Removing nonnumerical data out of a number + SQL



3rd option - if you want to use regex



then
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=27205






share|improve this answer

































    11














    This one works for me



    CREATE Function [dbo].[RemoveNumericCharacters](@Temp VarChar(1000))
    Returns VarChar(1000)
    AS
    Begin

    Declare @NumRange as varchar(50) = '%[0-9]%'
    While PatIndex(@NumRange, @Temp) > 0
    Set @Temp = Stuff(@Temp, PatIndex(@NumRange, @Temp), 1, '')

    Return @Temp
    End


    and you can use it like so



    SELECT dbo.[RemoveNumericCharacters](Name) FROM TARGET_TABLE





    share|improve this answer































      4














      Try below for your query. where val is your string or column name.



      CASE WHEN PATINDEX('%[a-z]%', REVERSE(val)) > 1
      THEN LEFT(val, LEN(val) - PATINDEX('%[a-z]%', REVERSE(val)) + 1)
      ELSE '' END





      share|improve this answer


























      • I've replaced ELSE '' END with ELSE val END so it also returns when no numbers are in it.

        – KoalaBear
        Dec 10 '18 at 6:56



















      2














      One more approach using Recursive CTE..



      declare @string varchar(100)
      set @string ='te165st1230004616161616'

      ;With cte
      as
      (
      select @string as string,0 as n
      union all
      select cast(replace(string,n,'') as varchar(100)),n+1
      from cte
      where n<9
      )
      select top 1 string from cte
      order by n desc


      **Output:**
      test





      share|improve this answer































        0














        Quoting part of @Jatin answer with some modifications,



        use this in your where statement:



            SELECT * FROM .... etc.
        Where
        REPLACE
        (REPLACE
        (REPLACE
        (REPLACE
        (REPLACE
        (REPLACE
        (REPLACE
        (REPLACE
        (REPLACE
        (REPLACE (Name, '0', ''),
        '1', ''),
        '2', ''),
        '3', ''),
        '4', ''),
        '5', ''),
        '6', ''),
        '7', ''),
        '8', ''),
        '9', '') = P_SEARCH_KEY





        share|improve this answer































          -1














          Not tested, but you can do something like this:



          Create Function dbo.AlphasOnly(@s as varchar(max)) Returns varchar(max) As
          Begin
          Declare @Pos int = 1
          Declare @Ret varchar(max) = null
          If @s Is Not Null
          Begin
          Set @Ret = ''
          While @Pos <= Len(@s)
          Begin
          If SubString(@s, @Pos, 1) Like '[A-Za-z]'
          Begin
          Set @Ret = @Ret + SubString(@s, @Pos, 1)
          End
          Set @Pos = @Pos + 1
          End
          End
          Return @Ret
          End


          The key is to use this as a computed column and index it. It doesn't really matter how fast you make this function if the database has to execute it against every row in your large table every time you run the query.






          share|improve this answer
























          • very slow solution, fast is required

            – CR41G14
            Nov 5 '12 at 21:36











          • If you insist on doing Select name from test where dbo.AlphasOnly(name) = 'JoeBloggs' then this will be slow even if the function takes 0 time. You still pay the cost of reading all the rows off the disk (assuming when you say large table you mean your database doesn't fit in memory). To make it fast you need to find a way of indexing it, which is why I suggest a computed column.

            – Laurence
            Nov 5 '12 at 22:45











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


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13240298%2fremove-numbers-from-string-sql-server%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          6 Answers
          6






          active

          oldest

          votes








          6 Answers
          6






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          30














          1st option -



          You can nest REPLACE() functions up to 32 levels deep. It runs fast.



          REPLACE
          (REPLACE
          (REPLACE
          (REPLACE
          (REPLACE
          (REPLACE
          (REPLACE
          (REPLACE
          (REPLACE
          (REPLACE (@str, '0', ''),
          '1', ''),
          '2', ''),
          '3', ''),
          '4', ''),
          '5', ''),
          '6', ''),
          '7', ''),
          '8', ''),
          '9', '')


          2nd option --
          do the reverse of -



          Removing nonnumerical data out of a number + SQL



          3rd option - if you want to use regex



          then
          http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=27205






          share|improve this answer






























            30














            1st option -



            You can nest REPLACE() functions up to 32 levels deep. It runs fast.



            REPLACE
            (REPLACE
            (REPLACE
            (REPLACE
            (REPLACE
            (REPLACE
            (REPLACE
            (REPLACE
            (REPLACE
            (REPLACE (@str, '0', ''),
            '1', ''),
            '2', ''),
            '3', ''),
            '4', ''),
            '5', ''),
            '6', ''),
            '7', ''),
            '8', ''),
            '9', '')


            2nd option --
            do the reverse of -



            Removing nonnumerical data out of a number + SQL



            3rd option - if you want to use regex



            then
            http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=27205






            share|improve this answer




























              30












              30








              30







              1st option -



              You can nest REPLACE() functions up to 32 levels deep. It runs fast.



              REPLACE
              (REPLACE
              (REPLACE
              (REPLACE
              (REPLACE
              (REPLACE
              (REPLACE
              (REPLACE
              (REPLACE
              (REPLACE (@str, '0', ''),
              '1', ''),
              '2', ''),
              '3', ''),
              '4', ''),
              '5', ''),
              '6', ''),
              '7', ''),
              '8', ''),
              '9', '')


              2nd option --
              do the reverse of -



              Removing nonnumerical data out of a number + SQL



              3rd option - if you want to use regex



              then
              http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=27205






              share|improve this answer















              1st option -



              You can nest REPLACE() functions up to 32 levels deep. It runs fast.



              REPLACE
              (REPLACE
              (REPLACE
              (REPLACE
              (REPLACE
              (REPLACE
              (REPLACE
              (REPLACE
              (REPLACE
              (REPLACE (@str, '0', ''),
              '1', ''),
              '2', ''),
              '3', ''),
              '4', ''),
              '5', ''),
              '6', ''),
              '7', ''),
              '8', ''),
              '9', '')


              2nd option --
              do the reverse of -



              Removing nonnumerical data out of a number + SQL



              3rd option - if you want to use regex



              then
              http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=27205







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Aug 1 '18 at 22:35









              Brien Foss

              2,43631226




              2,43631226










              answered Nov 5 '12 at 21:19









              JatinJatin

              1,17221123




              1,17221123

























                  11














                  This one works for me



                  CREATE Function [dbo].[RemoveNumericCharacters](@Temp VarChar(1000))
                  Returns VarChar(1000)
                  AS
                  Begin

                  Declare @NumRange as varchar(50) = '%[0-9]%'
                  While PatIndex(@NumRange, @Temp) > 0
                  Set @Temp = Stuff(@Temp, PatIndex(@NumRange, @Temp), 1, '')

                  Return @Temp
                  End


                  and you can use it like so



                  SELECT dbo.[RemoveNumericCharacters](Name) FROM TARGET_TABLE





                  share|improve this answer




























                    11














                    This one works for me



                    CREATE Function [dbo].[RemoveNumericCharacters](@Temp VarChar(1000))
                    Returns VarChar(1000)
                    AS
                    Begin

                    Declare @NumRange as varchar(50) = '%[0-9]%'
                    While PatIndex(@NumRange, @Temp) > 0
                    Set @Temp = Stuff(@Temp, PatIndex(@NumRange, @Temp), 1, '')

                    Return @Temp
                    End


                    and you can use it like so



                    SELECT dbo.[RemoveNumericCharacters](Name) FROM TARGET_TABLE





                    share|improve this answer


























                      11












                      11








                      11







                      This one works for me



                      CREATE Function [dbo].[RemoveNumericCharacters](@Temp VarChar(1000))
                      Returns VarChar(1000)
                      AS
                      Begin

                      Declare @NumRange as varchar(50) = '%[0-9]%'
                      While PatIndex(@NumRange, @Temp) > 0
                      Set @Temp = Stuff(@Temp, PatIndex(@NumRange, @Temp), 1, '')

                      Return @Temp
                      End


                      and you can use it like so



                      SELECT dbo.[RemoveNumericCharacters](Name) FROM TARGET_TABLE





                      share|improve this answer













                      This one works for me



                      CREATE Function [dbo].[RemoveNumericCharacters](@Temp VarChar(1000))
                      Returns VarChar(1000)
                      AS
                      Begin

                      Declare @NumRange as varchar(50) = '%[0-9]%'
                      While PatIndex(@NumRange, @Temp) > 0
                      Set @Temp = Stuff(@Temp, PatIndex(@NumRange, @Temp), 1, '')

                      Return @Temp
                      End


                      and you can use it like so



                      SELECT dbo.[RemoveNumericCharacters](Name) FROM TARGET_TABLE






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Feb 25 '13 at 16:40









                      Ben AndersonBen Anderson

                      4,47333137




                      4,47333137























                          4














                          Try below for your query. where val is your string or column name.



                          CASE WHEN PATINDEX('%[a-z]%', REVERSE(val)) > 1
                          THEN LEFT(val, LEN(val) - PATINDEX('%[a-z]%', REVERSE(val)) + 1)
                          ELSE '' END





                          share|improve this answer


























                          • I've replaced ELSE '' END with ELSE val END so it also returns when no numbers are in it.

                            – KoalaBear
                            Dec 10 '18 at 6:56
















                          4














                          Try below for your query. where val is your string or column name.



                          CASE WHEN PATINDEX('%[a-z]%', REVERSE(val)) > 1
                          THEN LEFT(val, LEN(val) - PATINDEX('%[a-z]%', REVERSE(val)) + 1)
                          ELSE '' END





                          share|improve this answer


























                          • I've replaced ELSE '' END with ELSE val END so it also returns when no numbers are in it.

                            – KoalaBear
                            Dec 10 '18 at 6:56














                          4












                          4








                          4







                          Try below for your query. where val is your string or column name.



                          CASE WHEN PATINDEX('%[a-z]%', REVERSE(val)) > 1
                          THEN LEFT(val, LEN(val) - PATINDEX('%[a-z]%', REVERSE(val)) + 1)
                          ELSE '' END





                          share|improve this answer















                          Try below for your query. where val is your string or column name.



                          CASE WHEN PATINDEX('%[a-z]%', REVERSE(val)) > 1
                          THEN LEFT(val, LEN(val) - PATINDEX('%[a-z]%', REVERSE(val)) + 1)
                          ELSE '' END






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jan 6 '18 at 8:06

























                          answered Jan 2 '18 at 10:02









                          SutirthSutirth

                          785814




                          785814













                          • I've replaced ELSE '' END with ELSE val END so it also returns when no numbers are in it.

                            – KoalaBear
                            Dec 10 '18 at 6:56



















                          • I've replaced ELSE '' END with ELSE val END so it also returns when no numbers are in it.

                            – KoalaBear
                            Dec 10 '18 at 6:56

















                          I've replaced ELSE '' END with ELSE val END so it also returns when no numbers are in it.

                          – KoalaBear
                          Dec 10 '18 at 6:56





                          I've replaced ELSE '' END with ELSE val END so it also returns when no numbers are in it.

                          – KoalaBear
                          Dec 10 '18 at 6:56











                          2














                          One more approach using Recursive CTE..



                          declare @string varchar(100)
                          set @string ='te165st1230004616161616'

                          ;With cte
                          as
                          (
                          select @string as string,0 as n
                          union all
                          select cast(replace(string,n,'') as varchar(100)),n+1
                          from cte
                          where n<9
                          )
                          select top 1 string from cte
                          order by n desc


                          **Output:**
                          test





                          share|improve this answer




























                            2














                            One more approach using Recursive CTE..



                            declare @string varchar(100)
                            set @string ='te165st1230004616161616'

                            ;With cte
                            as
                            (
                            select @string as string,0 as n
                            union all
                            select cast(replace(string,n,'') as varchar(100)),n+1
                            from cte
                            where n<9
                            )
                            select top 1 string from cte
                            order by n desc


                            **Output:**
                            test





                            share|improve this answer


























                              2












                              2








                              2







                              One more approach using Recursive CTE..



                              declare @string varchar(100)
                              set @string ='te165st1230004616161616'

                              ;With cte
                              as
                              (
                              select @string as string,0 as n
                              union all
                              select cast(replace(string,n,'') as varchar(100)),n+1
                              from cte
                              where n<9
                              )
                              select top 1 string from cte
                              order by n desc


                              **Output:**
                              test





                              share|improve this answer













                              One more approach using Recursive CTE..



                              declare @string varchar(100)
                              set @string ='te165st1230004616161616'

                              ;With cte
                              as
                              (
                              select @string as string,0 as n
                              union all
                              select cast(replace(string,n,'') as varchar(100)),n+1
                              from cte
                              where n<9
                              )
                              select top 1 string from cte
                              order by n desc


                              **Output:**
                              test






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Aug 17 '16 at 13:20









                              TheGameiswarTheGameiswar

                              21.4k53063




                              21.4k53063























                                  0














                                  Quoting part of @Jatin answer with some modifications,



                                  use this in your where statement:



                                      SELECT * FROM .... etc.
                                  Where
                                  REPLACE
                                  (REPLACE
                                  (REPLACE
                                  (REPLACE
                                  (REPLACE
                                  (REPLACE
                                  (REPLACE
                                  (REPLACE
                                  (REPLACE
                                  (REPLACE (Name, '0', ''),
                                  '1', ''),
                                  '2', ''),
                                  '3', ''),
                                  '4', ''),
                                  '5', ''),
                                  '6', ''),
                                  '7', ''),
                                  '8', ''),
                                  '9', '') = P_SEARCH_KEY





                                  share|improve this answer




























                                    0














                                    Quoting part of @Jatin answer with some modifications,



                                    use this in your where statement:



                                        SELECT * FROM .... etc.
                                    Where
                                    REPLACE
                                    (REPLACE
                                    (REPLACE
                                    (REPLACE
                                    (REPLACE
                                    (REPLACE
                                    (REPLACE
                                    (REPLACE
                                    (REPLACE
                                    (REPLACE (Name, '0', ''),
                                    '1', ''),
                                    '2', ''),
                                    '3', ''),
                                    '4', ''),
                                    '5', ''),
                                    '6', ''),
                                    '7', ''),
                                    '8', ''),
                                    '9', '') = P_SEARCH_KEY





                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      Quoting part of @Jatin answer with some modifications,



                                      use this in your where statement:



                                          SELECT * FROM .... etc.
                                      Where
                                      REPLACE
                                      (REPLACE
                                      (REPLACE
                                      (REPLACE
                                      (REPLACE
                                      (REPLACE
                                      (REPLACE
                                      (REPLACE
                                      (REPLACE
                                      (REPLACE (Name, '0', ''),
                                      '1', ''),
                                      '2', ''),
                                      '3', ''),
                                      '4', ''),
                                      '5', ''),
                                      '6', ''),
                                      '7', ''),
                                      '8', ''),
                                      '9', '') = P_SEARCH_KEY





                                      share|improve this answer













                                      Quoting part of @Jatin answer with some modifications,



                                      use this in your where statement:



                                          SELECT * FROM .... etc.
                                      Where
                                      REPLACE
                                      (REPLACE
                                      (REPLACE
                                      (REPLACE
                                      (REPLACE
                                      (REPLACE
                                      (REPLACE
                                      (REPLACE
                                      (REPLACE
                                      (REPLACE (Name, '0', ''),
                                      '1', ''),
                                      '2', ''),
                                      '3', ''),
                                      '4', ''),
                                      '5', ''),
                                      '6', ''),
                                      '7', ''),
                                      '8', ''),
                                      '9', '') = P_SEARCH_KEY






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Aug 20 '14 at 8:28









                                      simsimsimsim

                                      1,47521540




                                      1,47521540























                                          -1














                                          Not tested, but you can do something like this:



                                          Create Function dbo.AlphasOnly(@s as varchar(max)) Returns varchar(max) As
                                          Begin
                                          Declare @Pos int = 1
                                          Declare @Ret varchar(max) = null
                                          If @s Is Not Null
                                          Begin
                                          Set @Ret = ''
                                          While @Pos <= Len(@s)
                                          Begin
                                          If SubString(@s, @Pos, 1) Like '[A-Za-z]'
                                          Begin
                                          Set @Ret = @Ret + SubString(@s, @Pos, 1)
                                          End
                                          Set @Pos = @Pos + 1
                                          End
                                          End
                                          Return @Ret
                                          End


                                          The key is to use this as a computed column and index it. It doesn't really matter how fast you make this function if the database has to execute it against every row in your large table every time you run the query.






                                          share|improve this answer
























                                          • very slow solution, fast is required

                                            – CR41G14
                                            Nov 5 '12 at 21:36











                                          • If you insist on doing Select name from test where dbo.AlphasOnly(name) = 'JoeBloggs' then this will be slow even if the function takes 0 time. You still pay the cost of reading all the rows off the disk (assuming when you say large table you mean your database doesn't fit in memory). To make it fast you need to find a way of indexing it, which is why I suggest a computed column.

                                            – Laurence
                                            Nov 5 '12 at 22:45
















                                          -1














                                          Not tested, but you can do something like this:



                                          Create Function dbo.AlphasOnly(@s as varchar(max)) Returns varchar(max) As
                                          Begin
                                          Declare @Pos int = 1
                                          Declare @Ret varchar(max) = null
                                          If @s Is Not Null
                                          Begin
                                          Set @Ret = ''
                                          While @Pos <= Len(@s)
                                          Begin
                                          If SubString(@s, @Pos, 1) Like '[A-Za-z]'
                                          Begin
                                          Set @Ret = @Ret + SubString(@s, @Pos, 1)
                                          End
                                          Set @Pos = @Pos + 1
                                          End
                                          End
                                          Return @Ret
                                          End


                                          The key is to use this as a computed column and index it. It doesn't really matter how fast you make this function if the database has to execute it against every row in your large table every time you run the query.






                                          share|improve this answer
























                                          • very slow solution, fast is required

                                            – CR41G14
                                            Nov 5 '12 at 21:36











                                          • If you insist on doing Select name from test where dbo.AlphasOnly(name) = 'JoeBloggs' then this will be slow even if the function takes 0 time. You still pay the cost of reading all the rows off the disk (assuming when you say large table you mean your database doesn't fit in memory). To make it fast you need to find a way of indexing it, which is why I suggest a computed column.

                                            – Laurence
                                            Nov 5 '12 at 22:45














                                          -1












                                          -1








                                          -1







                                          Not tested, but you can do something like this:



                                          Create Function dbo.AlphasOnly(@s as varchar(max)) Returns varchar(max) As
                                          Begin
                                          Declare @Pos int = 1
                                          Declare @Ret varchar(max) = null
                                          If @s Is Not Null
                                          Begin
                                          Set @Ret = ''
                                          While @Pos <= Len(@s)
                                          Begin
                                          If SubString(@s, @Pos, 1) Like '[A-Za-z]'
                                          Begin
                                          Set @Ret = @Ret + SubString(@s, @Pos, 1)
                                          End
                                          Set @Pos = @Pos + 1
                                          End
                                          End
                                          Return @Ret
                                          End


                                          The key is to use this as a computed column and index it. It doesn't really matter how fast you make this function if the database has to execute it against every row in your large table every time you run the query.






                                          share|improve this answer













                                          Not tested, but you can do something like this:



                                          Create Function dbo.AlphasOnly(@s as varchar(max)) Returns varchar(max) As
                                          Begin
                                          Declare @Pos int = 1
                                          Declare @Ret varchar(max) = null
                                          If @s Is Not Null
                                          Begin
                                          Set @Ret = ''
                                          While @Pos <= Len(@s)
                                          Begin
                                          If SubString(@s, @Pos, 1) Like '[A-Za-z]'
                                          Begin
                                          Set @Ret = @Ret + SubString(@s, @Pos, 1)
                                          End
                                          Set @Pos = @Pos + 1
                                          End
                                          End
                                          Return @Ret
                                          End


                                          The key is to use this as a computed column and index it. It doesn't really matter how fast you make this function if the database has to execute it against every row in your large table every time you run the query.







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Nov 5 '12 at 21:24









                                          LaurenceLaurence

                                          10.2k11931




                                          10.2k11931













                                          • very slow solution, fast is required

                                            – CR41G14
                                            Nov 5 '12 at 21:36











                                          • If you insist on doing Select name from test where dbo.AlphasOnly(name) = 'JoeBloggs' then this will be slow even if the function takes 0 time. You still pay the cost of reading all the rows off the disk (assuming when you say large table you mean your database doesn't fit in memory). To make it fast you need to find a way of indexing it, which is why I suggest a computed column.

                                            – Laurence
                                            Nov 5 '12 at 22:45



















                                          • very slow solution, fast is required

                                            – CR41G14
                                            Nov 5 '12 at 21:36











                                          • If you insist on doing Select name from test where dbo.AlphasOnly(name) = 'JoeBloggs' then this will be slow even if the function takes 0 time. You still pay the cost of reading all the rows off the disk (assuming when you say large table you mean your database doesn't fit in memory). To make it fast you need to find a way of indexing it, which is why I suggest a computed column.

                                            – Laurence
                                            Nov 5 '12 at 22:45

















                                          very slow solution, fast is required

                                          – CR41G14
                                          Nov 5 '12 at 21:36





                                          very slow solution, fast is required

                                          – CR41G14
                                          Nov 5 '12 at 21:36













                                          If you insist on doing Select name from test where dbo.AlphasOnly(name) = 'JoeBloggs' then this will be slow even if the function takes 0 time. You still pay the cost of reading all the rows off the disk (assuming when you say large table you mean your database doesn't fit in memory). To make it fast you need to find a way of indexing it, which is why I suggest a computed column.

                                          – Laurence
                                          Nov 5 '12 at 22:45





                                          If you insist on doing Select name from test where dbo.AlphasOnly(name) = 'JoeBloggs' then this will be slow even if the function takes 0 time. You still pay the cost of reading all the rows off the disk (assuming when you say large table you mean your database doesn't fit in memory). To make it fast you need to find a way of indexing it, which is why I suggest a computed column.

                                          – Laurence
                                          Nov 5 '12 at 22:45


















                                          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.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13240298%2fremove-numbers-from-string-sql-server%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







                                          這個網誌中的熱門文章

                                          Xamarin.form Move up view when keyboard appear

                                          Post-Redirect-Get with Spring WebFlux and Thymeleaf

                                          Anylogic : not able to use stopDelay()