SQL小技巧

转自:http://blog.csdn.net/justdb/article/details/24516997

一 环境介绍

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


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

[sql]  view plain  copy
  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 plain  copy
  1. SELECT * FROM #t1  
  2.   
  3.   
  4. 3059  
  5. 3060  
  6. 3061  
  7. 3062  
  8. 3063  
  9. 3064  
  10. 3065  
  11. 3066  
  12. 3067  
  13. 3068  
  14. 人民路广场3069  
  15. 社区中心3070  
  16. 珠海市3071  
  17. 丽都3072  
  18. 晓月3073  
  19. 旧区3074  
  20. 新城3075  
  21. 水井沟3076  


接着我们依次处理包含场、心、市、都、月、区、城、沟的字符串,语句和处理结果如下:
[sql]  view plain  copy
  1. SELECT *  
  2. FROM   #t1  
  3. WHERE  name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN   
  4.   
  5.   
  6. 人民路广场3069  
  7. 社区中心3070  
  8. 珠海市3071  
  9. 丽都3072  
  10. 晓月3073  
  11. 旧区3074  
  12. 新城3075  
  13. 水井沟3076  
  14.   
  15.   
  16. SELECT Rtrim(Ltrim(Substring(name, Charindex('场'name) + 1, Len(name)))) AS name  
  17. INTO   #t2  
  18. FROM   #t1  
  19.   
  20.   
  21. SELECT *  
  22. FROM   #t2  
  23. WHERE  name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN   
  24.   
  25.   
  26. 社区中心3070  
  27. 珠海市3071  
  28. 丽都3072  
  29. 晓月3073  
  30. 旧区3074  
  31. 新城3075  
  32. 水井沟3076  
  33.   
  34.   
  35. SELECT Rtrim(Ltrim(Substring(name, Charindex('心'name) + 1, Len(name)))) AS name  
  36. INTO   #t3  
  37. FROM   #t2  
  38.   
  39.   
  40. SELECT *  
  41. FROM   #t3  
  42. WHERE  name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN   
  43.   
  44.   
  45. 珠海市3071  
  46. 丽都3072  
  47. 晓月3073  
  48. 旧区3074  
  49. 新城3075  
  50. 水井沟3076  
  51.   
  52.   
  53. SELECT Rtrim(Ltrim(Substring(name, Charindex('市'name) + 1, Len(name)))) AS name  
  54. INTO   #t4  
  55. FROM   #t3  
  56.   
  57.   
  58. SELECT *  
  59. FROM   #t4  
  60. WHERE  name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN   
  61.   
  62.   
  63. 丽都3072  
  64. 晓月3073  
  65. 旧区3074  
  66. 新城3075  
  67. 水井沟3076  
  68.   
  69.   
  70. SELECT Rtrim(Ltrim(Substring(name, Charindex('都'name) + 1, Len(name)))) AS name  
  71. INTO   #t5  
  72. FROM   #t4  
  73.   
  74.   
  75. SELECT *  
  76. FROM   #t5  
  77. WHERE  name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN   
  78.   
  79.   
  80. 晓月3073  
  81. 旧区3074  
  82. 新城3075  
  83. 水井沟3076  
  84.   
  85.   
  86. SELECT Rtrim(Ltrim(Substring(name, Charindex('月'name) + 1, Len(name)))) AS name  
  87. INTO   #t6  
  88. FROM   #t5  
  89.   
  90.   
  91. SELECT *  
  92. FROM   #t6  
  93. WHERE  name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN   
  94.   
  95.   
  96. 旧区3074  
  97. 新城3075  
  98. 水井沟3076  
  99.   
  100.   
  101. SELECT Rtrim(Ltrim(Substring(name, Charindex('区'name) + 1, Len(name)))) AS name  
  102. INTO   #t7  
  103. FROM   #t6  
  104.   
  105.   
  106. SELECT *  
  107. FROM   #t7  
  108. WHERE  name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN   
  109.   
  110.   
  111. 新城3075  
  112. 水井沟3076  
  113.   
  114.   
  115. SELECT Rtrim(Ltrim(Substring(name, Charindex('城'name) + 1, Len(name)))) AS name  
  116. INTO   #t8  
  117. FROM   #t7  
  118.   
  119.   
  120. SELECT *  
  121. FROM   #t8  
  122. WHERE  name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN   
  123.   
  124.   
  125. 水井沟3076  
  126.   
  127.   
  128. SELECT Rtrim(Ltrim(Substring(name, Charindex('沟'name) + 1, Len(name)))) AS name  
  129. INTO   #t9  
  130. FROM   #t8  
  131.   
  132.   
  133. SELECT *  
  134. FROM   #t9  
  135. WHERE  name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN   
  136.   
  137.   
  138. --无记录  


这是最终的处理结果,过滤出编号后,我就可以利用这些编号和数据库表进行关联,获得想要的数据。
[sql]  view plain  copy
  1. SELECT *  
  2. INTO   #result  
  3. FROM   #t9  
  4.   
  5.   
  6. SELECT *  
  7. FROM   #result   
  8.   
  9.   
  10. name  
  11. 3059  
  12. 3060  
  13. 3061  
  14. 3062  
  15. 3063  
  16. 3064  
  17. 3065  
  18. 3066  
  19. 3067  
  20. 3068  
  21. 3069  
  22. 3070  
  23. 3071  
  24. 3072  
  25. 3073  
  26. 3074  
  27. 3075  
  28. 3076  
  29.   
  30.   
  31. SELECT s.xxx,  
  32.        s.xxx  
  33. FROM   xx s  
  34.        JOIN #result r  
  35.          ON s.xxx = r.name  
  36. WHERE  s.xxx = 0;   


四 总结

本文过滤编号实际上核心代码就两个,第一个是利用SQL Server的内置函数过滤出指定编号,语句如下:
[sql]  view plain  copy
  1. SELECT Rtrim(Ltrim(Substring(name, Charindex('店'name) + 1, Len(name)))) AS name  
  2. INTO   #t1  
  3. FROM   #temp   


第二个是判断是否包含中文,语句如下:
[sql]  view plain  copy
  1. SELECT *  
  2. FROM   #t1  
  3. WHERE  name LIKE N'%[一-龥]%' COLLATE Chinese_PRC_BIN   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值