[原创]SQL Server截取字符串和处理中文技巧_MySQL, Oracle及数据库讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  MySQL, Oracle及数据库讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2836 | 回复: 0   主题: [原创]SQL Server截取字符串和处理中文技巧        下一篇 
    本主题由 hui.chen 于 2014-11-5 16:20:14 移动
Robin
注册用户
等级:少校
经验:856
发帖:63
精华:4
注册:2014-3-22
状态:离线
发送短消息息给Robin 加好友    发送短消息息给Robin 发消息
发表于: IP:您无权察看 2014-4-26 21:32:38 | [全部帖] [楼主帖] 楼主   主页

一 环境介绍


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
;


二 实现功能

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

三 实现模拟


首先,我们准备测试数据,注意,这里的数据全部都是模拟数据,无实际含义。语句如下:

[sql]
view plaincopy北京联动北方科技有限公司北京联动北方科技有限公司

  1. CREATE TABLE #temp 
  2.  name VARCHAR(80) 
  3. ); 
  4. INSERT INTO #temp 
  5. VALUES ('五道口店3059'); 
  6. INSERT INTO #temp 
  7. VALUES ('五羊邨店3060'); 
  8. INSERT INTO #temp 
  9. VALUES ('杨家屯店3061'); 
  10. INSERT INTO #temp 
  11. VALUES ('十里堤店3062'); 
  12. INSERT INTO #temp 
  13. VALUES ('中关村店3063'); 
  14. INSERT INTO #temp 
  15. VALUES ('丽秀店3064'); 
  16. INSERT INTO #temp 
  17. VALUES ('石门店3065'); 
  18. INSERT INTO #temp 
  19. VALUES ('黄村店3066'); 
  20. INSERT INTO #temp 
  21. VALUES ('东圃店3067'); 
  22. INSERT INTO #temp 
  23. VALUES ('天河店3068'); 
  24. INSERT INTO #temp 
  25. VALUES ('人民路广场3069'); 
  26. INSERT INTO #temp 
  27. VALUES ('社区中心3070'); 
  28. INSERT INTO #temp 
  29. VALUES ('珠海市3071'); 
  30. INSERT INTO #temp 
  31. VALUES ('丽都3072'); 
  32. INSERT INTO #temp 
  33. VALUES ('晓月3073'); 
  34. INSERT INTO #temp 
  35. VALUES ('旧区3074'); 
  36. INSERT INTO #temp 
  37. VALUES ('新城3075'); 
  38. INSERT INTO #temp 
  39. VALUES ('水井沟3076'); 

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

[sql]
view plaincopy北京联动北方科技有限公司北京联动北方科技有限公司

  1. SELECT Rtrim(Ltrim(Substring(name, Charindex('店', name) + 1, Len(name)))) AS name 
  2. INTO #t1 
  3. 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


好了,我们查看处理完后的结果,可以看到包含店的字符串已经全部过滤出编号。

这是最终的处理结果,过滤出编号后,我就可以利用这些编号和数据库表进行关联,获得想要的数据。

[sql]
view plaincopy北京联动北方科技有限公司北京联动北方科技有限公司

  1. SELECT * 
  2. INTO #result 
  3. FROM #t9 
  4. SELECT * 
  5. FROM #result 
  6. name 
  7. 3059 
  8. 3060 
  9. 3061 
  10. 3062 
  11. 3063 
  12. 3064 
  13. 3065 
  14. 3066 
  15. 3067 
  16. 3068 
  17. 3069 
  18. 3070 
  19. 3071 
  20. 3072 
  21. 3073 
  22. 3074 
  23. 3075 
  24. 3076 
  25. SELECT s.xxx, 
  26.  s.xxx 
  27. FROM xx s 
  28.  JOIN #result r 
  29.  ON s.xxx = r.name 
  30. WHERE s.xxx = 0; 



四 总结

本文过滤编号实际上核心代码就两个,第一个是利用SQL Server的内置函数过滤出指定编号,语句如下:

[sql]
view plaincopy北京联动北方科技有限公司北京联动北方科技有限公司

  1. SELECT Rtrim(Ltrim(Substring(name, Charindex('店', name) + 1, Len(name)))) AS name 
  2. INTO #t1 
  3. FROM #temp 

第二个是判断是否包含中文,语句如下:

[sql]
view plaincopy北京联动北方科技有限公司北京联动北方科技有限公司

  1. SELECT * 
  2. FROM #t1 
  3. WHERE name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN 

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

Good Luck!


更快速的方法:SELECT RIGHT(Name,4) FROM #temp

该贴由hui.chen转至本版2014-11-5 16:20:14
该贴由hui.chen转至本版2014-11-11 15:08:57



赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论