从列中删除特殊字符和数字

时间:2017-08-25 12:39:02

标签: sql-server

如何删除microsoft sql server中特定列的空格以外的所有特殊字符和数字?

1 个答案:

答案 0 :(得分:0)

上面的链接都使用循环来解决这个问题。对于这种类型的东西,没有必要求助于循环。相反,我们可以使用计数表。我在这个系统中保留了一个计数表作为一个视图。

<?xml version="1.0" encoding="UTF-8"?>
  <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

   <build>
       <plugins>
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-compiler-plugin</artifactId>
               <version>3.1</version>
               <configuration>
                   <source>1.8</source>
                   <target>1.8</target>
               </configuration>
           </plugin>
       </plugins>
   </build>

   <modelVersion>4.0.0</modelVersion>

   <groupId>io.github.carldata</groupId>
   <artifactId>InsertDataTrigger</artifactId>
   <version>1.0</version>

   <dependencies>
       <!-- https://mvnrepository.com/artifact/org.apache.cassandra/cassandra-all -->
       <dependency>
           <groupId>org.apache.cassandra</groupId>
           <artifactId>cassandra-all</artifactId>
           <version>3.11.0</version>
       </dependency>

       <dependency>
           <groupId>org.apache.kafka</groupId>
           <artifactId>kafka-clients</artifactId>
           <version>0.11.0.0</version>
       </dependency>
   </dependencies>

</project>

现在我们可以将该计数用作一组。这比基于循环的解决方案快得多。

create View [dbo].[cteTally] as

WITH
    E1(N) AS (select 1 from (values (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))dt(n)),
    E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
    E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
    cteTally(N) AS 
    (
        SELECT  ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
    )
select N from cteTally
GO