<bdo id='8URDM'></bdo><ul id='8URDM'></ul>
    1. <i id='8URDM'><tr id='8URDM'><dt id='8URDM'><q id='8URDM'><span id='8URDM'><b id='8URDM'><form id='8URDM'><ins id='8URDM'></ins><ul id='8URDM'></ul><sub id='8URDM'></sub></form><legend id='8URDM'></legend><bdo id='8URDM'><pre id='8URDM'><center id='8URDM'></center></pre></bdo></b><th id='8URDM'></th></span></q></dt></tr></i><div id='8URDM'><tfoot id='8URDM'></tfoot><dl id='8URDM'><fieldset id='8URDM'></fieldset></dl></div>
    2. <small id='8URDM'></small><noframes id='8URDM'>

    3. <legend id='8URDM'><style id='8URDM'><dir id='8URDM'><q id='8URDM'></q></dir></style></legend>

      <tfoot id='8URDM'></tfoot>

        从 SQL Server 中的 VARCHAR 中删除非数字字符的最快方法

        时间:2023-07-17

            <small id='IZsC2'></small><noframes id='IZsC2'>

              <tbody id='IZsC2'></tbody>
            <tfoot id='IZsC2'></tfoot>

              • <i id='IZsC2'><tr id='IZsC2'><dt id='IZsC2'><q id='IZsC2'><span id='IZsC2'><b id='IZsC2'><form id='IZsC2'><ins id='IZsC2'></ins><ul id='IZsC2'></ul><sub id='IZsC2'></sub></form><legend id='IZsC2'></legend><bdo id='IZsC2'><pre id='IZsC2'><center id='IZsC2'></center></pre></bdo></b><th id='IZsC2'></th></span></q></dt></tr></i><div id='IZsC2'><tfoot id='IZsC2'></tfoot><dl id='IZsC2'><fieldset id='IZsC2'></fieldset></dl></div>
                  <bdo id='IZsC2'></bdo><ul id='IZsC2'></ul>
                  <legend id='IZsC2'><style id='IZsC2'><dir id='IZsC2'><q id='IZsC2'></q></dir></style></legend>
                  本文介绍了从 SQL Server 中的 VARCHAR 中删除非数字字符的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在编写一个导入实用程序,它使用电话号码作为导入中的唯一键.

                  I'm writing an import utility that is using phone numbers as a unique key within the import.

                  我需要检查我的数据库中是否不存在电话号码.问题是数据库中的电话号码可能有破折号和括号之类的东西,也可能有其他东西.我写了一个函数来删除这些东西,问题是它并且在我的数据库中有数千条记录并且一次导入数千条记录,这个过程可能会慢得令人无法接受.我已经将电话号码列为索引.

                  I need to check that the phone number does not already exist in my DB. The problem is that phone numbers in the DB could have things like dashes and parenthesis and possibly other things. I wrote a function to remove these things, the problem is that it is slow and with thousands of records in my DB and thousands of records to import at once, this process can be unacceptably slow. I've already made the phone number column an index.

                  我尝试使用这篇文章中的脚本:
                  T-SQL 修剪 &nbsp(和其他非字母数字字符)

                  I tried using the script from this post:
                  T-SQL trim &nbsp (and other non-alphanumeric characters)

                  但这并没有加快速度.

                  有没有更快的方法来删除非数字字符?在必须比较 10,000 到 100,000 条记录时表现良好的东西.

                  Is there a faster way to remove non-numeric characters? Something that can perform well when 10,000 to 100,000 records have to be compared.

                  无论做什么都需要快速.

                  更新
                  鉴于人们的反应,我想我必须在运行导入实用程序之前清理字段.

                  Update
                  Given what people responded with, I think I'm going to have to clean the fields before I run the import utility.

                  为了回答我在其中编写导入实用程序的问题,它是一个 C# 应用程序.我现在正在将 BIGINT 与 BIGINT 进行比较,无需更改数据库数据,而且我仍然使用非常小的数据集(大约 2000 条记录)对性能造成影响.

                  To answer the question of what I'm writing the import utility in, it is a C# app. I'm comparing BIGINT to BIGINT now, with no need to alter DB data and I'm still taking a performance hit with a very small set of data (about 2000 records).

                  将 BIGINT 与 BIGINT 进行比较会减慢速度吗?

                  Could comparing BIGINT to BIGINT be slowing things down?

                  我已经尽可能地优化了我的应用程序的代码端(删除了正则表达式,删除了不必要的数据库调用).虽然我不能再孤立 SQL 作为问题的根源,但我仍然觉得它是.

                  I've optimized the code side of my app as much as I can (removed regexes, removed unneccessary DB calls). Although I can't isolate SQL as the source of the problem anymore, I still feel like it is.

                  推荐答案

                  我可能会误解,但是您有两组数据可以从一组数据中删除数据库中当前数据的字符串,然后在每次导入时删除一组新数据.

                  I may misunderstand, but you've got two sets of data to remove the strings from one for current data in the database and then a new set whenever you import.

                  为了更新现有的记录,我只会使用 SQL,这只需要发生一次.

                  For updating the existing records, I would just use SQL, that only has to happen once.

                  但是,SQL 并未针对此类操作进行优化,因为您说您正在编写导入实用程序,所以我会在导入实用程序本身的上下文中进行这些更新,而不是在 SQL 中.这将是更好的性能明智.你在用什么编写实用程序?

                  However, SQL isn't optimized for this sort of operation, since you said you are writing an import utility, I would do those updates in the context of the import utility itself, not in SQL. This would be much better performance wise. What are you writing the utility in?

                  另外,我可能完全误解了这个过程,所以如果不在基地,我很抱歉.

                  Also, I may be completely misunderstanding the process, so I apologize if off-base.


                  对于初始更新,如果您使用的是 SQL Server 2005,您可以尝试使用 CLR 函数.这是使用正则表达式的快速方法.不知道性能会如何比较,我自己从来没有使用过这个,除了现在的快速测试.


                  For the initial update, if you are using SQL Server 2005, you could try a CLR function. Here's a quick one using regex. Not sure how the performance would compare, I've never used this myself except for a quick test right now.

                  using System;  
                  using System.Data;  
                  using System.Text.RegularExpressions;  
                  using System.Data.SqlClient;  
                  using System.Data.SqlTypes;  
                  using Microsoft.SqlServer.Server;  
                  
                  public partial class UserDefinedFunctions  
                  {  
                      [Microsoft.SqlServer.Server.SqlFunction]  
                      public static SqlString StripNonNumeric(SqlString input)  
                      {  
                          Regex regEx = new Regex(@"D");  
                          return regEx.Replace(input.Value, "");  
                      }  
                  };  
                  

                  部署后,要更新,您可以使用:

                  After this is deployed, to update you could just use:

                  UPDATE table SET phoneNumber = dbo.StripNonNumeric(phoneNumber)
                  

                  这篇关于从 SQL Server 中的 VARCHAR 中删除非数字字符的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 SQL Server 中创建物化视图? 下一篇:SQL Server 检查区分大小写?

                  相关文章

                  <i id='3JXcM'><tr id='3JXcM'><dt id='3JXcM'><q id='3JXcM'><span id='3JXcM'><b id='3JXcM'><form id='3JXcM'><ins id='3JXcM'></ins><ul id='3JXcM'></ul><sub id='3JXcM'></sub></form><legend id='3JXcM'></legend><bdo id='3JXcM'><pre id='3JXcM'><center id='3JXcM'></center></pre></bdo></b><th id='3JXcM'></th></span></q></dt></tr></i><div id='3JXcM'><tfoot id='3JXcM'></tfoot><dl id='3JXcM'><fieldset id='3JXcM'></fieldset></dl></div>
                    1. <small id='3JXcM'></small><noframes id='3JXcM'>

                      <legend id='3JXcM'><style id='3JXcM'><dir id='3JXcM'><q id='3JXcM'></q></dir></style></legend><tfoot id='3JXcM'></tfoot>
                        <bdo id='3JXcM'></bdo><ul id='3JXcM'></ul>