SQL Server截取字符串和处理中文技巧
一 环境介绍
SQL Server PRINT @@VERSION
MicrosoftSQLServer2012-11.0.2100.60(X64)
Feb10201219:39:15
Copyright(c)MicrosoftCorporation
EnterpriseEdition: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-471G
Processor: Intel(R) Core(TM) i5-3230M CPU @ 2.60GHz (4 CPUs), ~2.6GHz
Memory: 4096MB RAM
二 实现功能
从一大堆有包括中文字符和编号的字符串中过滤出编号。
三 实现模拟
首先,我们准备測试数据,注意。这里的数据所有都是模拟数据,无实际含义。语句例如以下:
CREATE TABLE #temp
(
name VARCHAR(80)
); INSERT INTO #temp
VALUES ('五道口店3059'); INSERT INTO #temp
VALUES ('五羊邨店3060'); INSERT INTO #temp
VALUES ('杨家屯店3061'); INSERT INTO #temp
VALUES ('十里堤店3062'); INSERT INTO #temp
VALUES ('中关村店3063'); INSERT INTO #temp
VALUES ('丽秀店3064'); INSERT INTO #temp
VALUES ('石门店3065'); INSERT INTO #temp
VALUES ('黄村店3066'); INSERT INTO #temp
VALUES ('东圃店3067'); INSERT INTO #temp
VALUES ('天河店3068'); INSERT INTO #temp
VALUES ('人民路广场3069'); INSERT INTO #temp
VALUES ('社区中心3070'); INSERT INTO #temp
VALUES ('珠海市3071'); INSERT INTO #temp
VALUES ('丽都3072'); INSERT INTO #temp
VALUES ('晓月3073'); INSERT INTO #temp
VALUES ('旧区3074'); INSERT INTO #temp
VALUES ('新城3075'); INSERT INTO #temp
VALUES ('水井沟3076');
然后。我们观察数据,发现这些数据都有规律。编号是数字,占4个字符。
数字前面包括店、场、心、市、都、月、区、城、沟共9个字符。
我们试着採用SQL Server内置的函数Substring、Charindex、Rtrim、Ltrim过滤掉出现次数最多(店)的字符串。
语句例如以下:
SELECT Rtrim(Ltrim(Substring(name, Charindex('店', name) + 1, Len(name)))) AS name
INTO #t1
FROM #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 #t1 3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
人民路广场3069
社区中心3070
珠海市3071
丽都3072
晓月3073
旧区3074
新城3075
水井沟3076
接着我们依次处理包括场、心、市、都、月、区、城、沟的字符串。语句和处理结果例如以下:
SELECT *
FROM #t1
WHERE name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN 人民路广场3069
社区中心3070
珠海市3071
丽都3072
晓月3073
旧区3074
新城3075
水井沟3076 SELECT Rtrim(Ltrim(Substring(name, Charindex('场', name) + 1, Len(name)))) AS name
INTO #t2
FROM #t1 SELECT *
FROM #t2
WHERE name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN 社区中心3070
珠海市3071
丽都3072
晓月3073
旧区3074
新城3075
水井沟3076 SELECT Rtrim(Ltrim(Substring(name, Charindex('心', name) + 1, Len(name)))) AS name
INTO #t3
FROM #t2 SELECT *
FROM #t3
WHERE name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN 珠海市3071
丽都3072
晓月3073
旧区3074
新城3075
水井沟3076 SELECT Rtrim(Ltrim(Substring(name, Charindex('市', name) + 1, Len(name)))) AS name
INTO #t4
FROM #t3 SELECT *
FROM #t4
WHERE name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN 丽都3072
晓月3073
旧区3074
新城3075
水井沟3076 SELECT Rtrim(Ltrim(Substring(name, Charindex('都', name) + 1, Len(name)))) AS name
INTO #t5
FROM #t4 SELECT *
FROM #t5
WHERE name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN 晓月3073
旧区3074
新城3075
水井沟3076 SELECT Rtrim(Ltrim(Substring(name, Charindex('月', name) + 1, Len(name)))) AS name
INTO #t6
FROM #t5 SELECT *
FROM #t6
WHERE name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN 旧区3074
新城3075
水井沟3076 SELECT Rtrim(Ltrim(Substring(name, Charindex('区', name) + 1, Len(name)))) AS name
INTO #t7
FROM #t6 SELECT *
FROM #t7
WHERE name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN 新城3075
水井沟3076 SELECT Rtrim(Ltrim(Substring(name, Charindex('城', name) + 1, Len(name)))) AS name
INTO #t8
FROM #t7 SELECT *
FROM #t8
WHERE name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN 水井沟3076 SELECT Rtrim(Ltrim(Substring(name, Charindex('沟', name) + 1, Len(name)))) AS name
INTO #t9
FROM #t8 SELECT *
FROM #t9
WHERE name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN --无记录
这是终于的处理结果,过滤出编号后,我就能够利用这些编号和数据库表进行关联,获得想要的数据。
SELECT *
INTO #result
FROM #t9 SELECT *
FROM #result name
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076 SELECT s.xxx,
s.xxx
FROM xx s
JOIN #result r
ON s.xxx = r.name
WHERE s.xxx = 0;
四 总结
本文过滤编号实际上核心代码就两个。第一个是利用SQL Server的内置函数过滤出指定编号,语句例如以下:
SELECT Rtrim(Ltrim(Substring(name, Charindex('店', name) + 1, Len(name)))) AS name
INTO #t1
FROM #temp
第二个是推断是否包括中文。语句例如以下:
SELECT *
FROM #t1
WHERE name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN
在工作中,发现和总结这些小技巧会让你的工作事半功倍。
Good Luck!
SQL Server截取字符串和处理中文技巧的更多相关文章
- SQL Server截取字符串
--SQL Server截取字符串 , Len('hello@163.com')) ,charindex('.','hello@163.com'))
- SQL Server 从字符串中提取中文、英文、数字
--[提取中文] IF OBJECT_ID('dbo.fun_getCN') IS NOT NULL DROP FUNCTION dbo.fun_getCN GO create function db ...
- SQL Server截取字符串(经纬度)
DECLARE @var VARCHAR(50) SET @var ='116.404556|39.915156' 方式一: SELECT CASE WHEN ISNULL(@var,'') < ...
- SQL Server:字符串函数
以下所有例子均Studnet表为例: 1. len():计算字符串长度 len()用来计算字符串的长度,每个中文汉字或英文字母都为一个长度 select sname, len(sname) from ...
- SQL Server 分隔字符串函数实现
在SQL Server中有时候也会遇到字符串进行分隔的需求.平时工作中常常遇到这样的需求,例如:人员数据表和人员爱好数据表,一条人员记录可以多多人员爱好记录,而往往人员和人员爱好在界面展示层要一并提交 ...
- .NET SQL Server连接字符串句法
.NET SQL Server连接字符串句法 数据库的连接性已经发展成为应用程序开发的一个标准方面.数据库连接字符串现在已经成为每个项目的标准必备条件.我发现自己为了找到所需要的句法,经常要从另外一个 ...
- MS SQL Server 数据库连接字符串详解
MS SQL Server 数据库连接字符串详解 原地址:http://blog.csdn.net/jhhja/article/details/6096565 问题 : 超时时间已到.在从池中获取连接 ...
- [转] SQL SERVER拼接字符串(字符串中有变量)
本文转自:http://blog.csdn.net/sikaiyuan2008/article/details/7848926 SQL SERVER拼接字符串(字符串中有变量)对我来说是一个难点,总是 ...
- delphi连接sql server的字符串2011-10-11 16:07
delphi连接sql server的字符串2011-10-11 16:07 一.delphi连接sql server 放一个连接组件 ADOConnection, 其它组件TADODataSet,T ...
随机推荐
- linux shell: 搜索字符串,剔除包含特定字符的行
搜索代码中出现CONFIG_BOOTARGS的行,但又不希望有#define和#undef, 暂时不知带如何直接用一个语句做到,因此用了如下三个语句. find . -type f | xargs g ...
- javascript 检测密码强度
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- js中的一元运算符
一元运算符只有一个参数,即要操作的对象或值.它们是 ECMAScript 中最简单的运算符. delete delete 运算符删除对以前定义的对象属性或方法的引用.例如: var o = new O ...
- java.lang.ClassNotFoundException: javax.servlet.Filter
java.lang.ClassNotFoundException: javax.servlet.Filter:有两个原因:(1)在maven中的作用域,不能是provided,需要是compile就是 ...
- 用JLabel显示时间-- JAVA初学者遇到的一个困难
问题:用一个JLabe,显示秒数,每过一秒数字自动减少1 问题看似很简单,但对初学JAVA的我来说,还真费了一点劲. 首先是如何即时,可以采用线程的方法: try { Thread.sleep(100 ...
- CSS实现背景图尺寸不随浏览器大小而变化的两种方法
一些网站的首页背景图尺寸不随浏览器缩放而变化,本例使用CSS 实现背景图尺寸不随浏览器缩放而变化,方法一. 把图片作为background,方法二使用img标签.喜欢的朋友可以看看 一些网站的首页 ...
- Ajax学习(三)——XMLHttpRequest对象的五步使使用方法
Ajax的核心技术是XMLHttpRequest对象,它能够在不向server提交整个页面的情况下.实现局部更新网页.通过这个对象,Ajax能够像桌面应用程序那样仅仅与server进行数据层的 ...
- 二分法查找 --JS 实现
var indexOfSorted = function f(arr,n){ //assume : arr has been sorted var low = 0; var high = arr.le ...
- TCP/IP之坚持定时器、报活定时器
TCP中的四个定时器: 1.超时定时器(最复杂的一个) 2.坚持定时器 3.保活定时器 4.2MSL定时器 坚持定时器用于防止通告窗口为0以后c/s双方相互等待死锁的情况:而保活定时器则用于处理半开发 ...
- iOS开发关于AppStore程序的上传流程
主要内容: 1.创建唯一标示符App ID(前提是你的程序在真机上测试没有任何问题) 2.申请发布证书 3.申请发布描述文件 4.iTunes Connect创建App并填写信息 5.选择证书编译打包 ...