SQL Server截取字符串和处理中文技巧

来源:互联网 发布:改短信软件 编辑:程序博客网 时间:2024/06/08 18:39

一 环境介绍

SQL  ServerPRINT @@VERSIONMicrosoftSQLServer2012-11.0.2100.60(X64)Feb10201219:39:15Copyright(c)MicrosoftCorporationEnterpriseEdition:Core-basedLicensing(64-bit)onWindowsNT6.1(Build7601:ServicePack1)操作系统------------------System Information------------------Operating System: Windows 7 Ultimate 64-bit (6.1, Build 7601) Service Pack 1 (7601.win7sp1_gdr.130828-1532)System Model: Aspire E1-471GProcessor: Intel(R) Core(TM) i5-3230M CPU @ 2.60GHz (4 CPUs), ~2.6GHzMemory: 4096MB RAM


二 实现功能

从一大堆有包含中文字符和编号的字符串中过滤出编号。


三 实现模拟


首先,我们准备测试数据,注意,这里的数据全部都是模拟数据,无实际含义。语句如下:
CREATE TABLE #temp(   name VARCHAR(80));INSERT INTO #tempVALUES     ('五道口店3059');INSERT INTO #tempVALUES     ('五羊邨店3060');INSERT INTO #tempVALUES     ('杨家屯店3061');INSERT INTO #tempVALUES     ('十里堤店3062');INSERT INTO #tempVALUES     ('中关村店3063');INSERT INTO #tempVALUES     ('丽秀店3064');INSERT INTO #tempVALUES     ('石门店3065');INSERT INTO #tempVALUES     ('黄村店3066');INSERT INTO #tempVALUES     ('东圃店3067');INSERT INTO #tempVALUES     ('天河店3068');INSERT INTO #tempVALUES     ('人民路广场3069');INSERT INTO #tempVALUES     ('社区中心3070');INSERT INTO #tempVALUES     ('珠海市3071');INSERT INTO #tempVALUES     ('丽都3072');INSERT INTO #tempVALUES     ('晓月3073');INSERT INTO #tempVALUES     ('旧区3074');INSERT INTO #tempVALUES     ('新城3075');INSERT INTO #tempVALUES     ('水井沟3076'); 


然后,我们观察数据,发现这些数据都有规律,编号是数字,占4个字符。数字前面包含店、场、心、市、都、月、区、城、沟共9个字符。
我们试着采用SQL Server内置的函数Substring、Charindex、Rtrim、Ltrim过滤掉出现次数最多(店)的字符串。
语句如下:

SELECT Rtrim(Ltrim(Substring(name, Charindex('店', name) + 1, Len(name)))) AS nameINTO   #t1FROM   #temp 

以下是这几个函数的使用说明:


Substring

Returns the part of a character expression that starts at the specified position and has the specified length. The position parameter and the length parameter must evaluate to integers.


Syntax

SUBSTRING(character_expression, position, length)


Arguments

character_expression

Is a character expression from which to extract characters.

position

Is an integer that specifies where the substring begins.

length

Is an integer that specifies the length of the substring as number of characters.


Result Types

DT_WSTR


Charindex
Searches an expression for another expression and returns its starting position if found.

Syntax

CHARINDEX ( expressionToFind ,expressionToSearch [ , start_location ] ) 


Arguments
expressionToFind
Is a character expression that contains the sequence to be found. expressionToFind is limited to 8000 characters.
expressionToSearch
Is a character expression to be searched.
start_location
Is an integer or bigint expression at which the search starts. If start_location is not specified, is a negative number, or is 0, the search starts at the beginning of expressionToSearch.

Return Types
bigint if expressionToSearch is of the varchar(max), nvarchar(max), or varbinary(max) data types; otherwise, int.

Rtrim
Returns a character expression after removing trailing spaces.

RTRIM does not remove white space characters such as the tab or line feed characters. Unicode provides code points for many different types of spaces, but this function recognizes only the Unicode code point 0x0020. When double-byte character set (DBCS) strings are converted to Unicode they may include space characters other than 0x0020 and the function cannot remove such spaces. To remove all kinds of spaces, you can use the Microsoft Visual Basic .NET RTrim method in a script run from the Script component.

Syntax
RTRIM(character expression)
              
Arguments
character_expression
Is a character expression from which to remove spaces.

Result Types
DT_WSTR

Ltrim
Returns a character expression after removing leading spaces.

LTRIM does not remove white-space characters such as the tab or line feed characters. Unicode provides code points for many different types of spaces, but this function recognizes only the Unicode code point 0x0020. When double-byte character set (DBCS) strings are converted to Unicode they may include space characters other than 0x0020 and the function cannot remove such spaces. To remove all kinds of spaces, you can use the Microsoft Visual Basic .NET LTrim method in a script run from the Script component.

Syntax
LTRIM(character expression)
              
Arguments
character_expression
Is a character expression from which to remove spaces.

Result Types
DT_WSTR


好了,我们查看处理完后的结果,可以看到包含店的字符串已经全部过滤出编号。
SELECT * FROM #t13059306030613062306330643065306630673068人民路广场3069社区中心3070珠海市3071丽都3072晓月3073旧区3074新城3075水井沟3076


接着我们依次处理包含场、心、市、都、月、区、城、沟的字符串,语句和处理结果如下:
SELECT *FROM   #t1WHERE  name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN 人民路广场3069社区中心3070珠海市3071丽都3072晓月3073旧区3074新城3075水井沟3076SELECT Rtrim(Ltrim(Substring(name, Charindex('场', name) + 1, Len(name)))) AS nameINTO   #t2FROM   #t1SELECT *FROM   #t2WHERE  name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN 社区中心3070珠海市3071丽都3072晓月3073旧区3074新城3075水井沟3076SELECT Rtrim(Ltrim(Substring(name, Charindex('心', name) + 1, Len(name)))) AS nameINTO   #t3FROM   #t2SELECT *FROM   #t3WHERE  name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN 珠海市3071丽都3072晓月3073旧区3074新城3075水井沟3076SELECT Rtrim(Ltrim(Substring(name, Charindex('市', name) + 1, Len(name)))) AS nameINTO   #t4FROM   #t3SELECT *FROM   #t4WHERE  name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN 丽都3072晓月3073旧区3074新城3075水井沟3076SELECT Rtrim(Ltrim(Substring(name, Charindex('都', name) + 1, Len(name)))) AS nameINTO   #t5FROM   #t4SELECT *FROM   #t5WHERE  name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN 晓月3073旧区3074新城3075水井沟3076SELECT Rtrim(Ltrim(Substring(name, Charindex('月', name) + 1, Len(name)))) AS nameINTO   #t6FROM   #t5SELECT *FROM   #t6WHERE  name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN 旧区3074新城3075水井沟3076SELECT Rtrim(Ltrim(Substring(name, Charindex('区', name) + 1, Len(name)))) AS nameINTO   #t7FROM   #t6SELECT *FROM   #t7WHERE  name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN 新城3075水井沟3076SELECT Rtrim(Ltrim(Substring(name, Charindex('城', name) + 1, Len(name)))) AS nameINTO   #t8FROM   #t7SELECT *FROM   #t8WHERE  name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN 水井沟3076SELECT Rtrim(Ltrim(Substring(name, Charindex('沟', name) + 1, Len(name)))) AS nameINTO   #t9FROM   #t8SELECT *FROM   #t9WHERE  name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN --无记录


这是最终的处理结果,过滤出编号后,我就可以利用这些编号和数据库表进行关联,获得想要的数据。
SELECT *INTO   #resultFROM   #t9SELECT *FROM   #result name305930603061306230633064306530663067306830693070307130723073307430753076SELECT s.xxx,       s.xxxFROM   xx s       JOIN #result r         ON s.xxx = r.nameWHERE  s.xxx = 0; 


四 总结

本文过滤编号实际上核心代码就两个,第一个是利用SQL Server的内置函数过滤出指定编号,语句如下:
SELECT Rtrim(Ltrim(Substring(name, Charindex('店', name) + 1, Len(name)))) AS nameINTO   #t1FROM   #temp 


第二个是判断是否包含中文,语句如下:
SELECT *FROM   #t1WHERE  name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN 


在工作中,发现和总结这些小技巧会让你的工作事半功倍。

Good Luck!


1 0