开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 13015|回复: 0

SQL Server关于master..spt_values的应用

[复制链接]

0

精华

654

贡献

433

赞扬

赞助者组

Rank: 14Rank: 14Rank: 14Rank: 14

帖子
207
软币
3085
在线时间
440 小时
注册时间
2013-7-4
发表于 2013-12-18 19:04:38 | 显示全部楼层 |阅读模式
SQL Server关于master..spt_values的应用(转自CSDN)
  1. select number from master..spt_values with(nolock) where type='P'
  2. /**解释:master..spt_values表的字段值为P的对应number字段值是从0-2047*/

  3. -----------
  4. --1.将字符串转换为列显示
  5. if object_id('tb') is not null drop table tb
  6. go
  7. create table tb([编号] varchar(3),[产品] varchar(2),[数量] int,[单价] int,[金额] int,[序列号] varchar(8))
  8. insert into tb([编号],[产品],[数量],[单价],[金额],[序列号])
  9. select '001','AA',3,5,15,'12,13,14' union all
  10. select '002','BB',8,9,13,'22,23,24'
  11. go
  12. select [编号],[产品],[数量],[单价],[金额]
  13. ,substring([序列号],b.number,charindex(',',[序列号]+',',b.number)-b.number) as [序列号]
  14. from tb a with(nolock),master..spt_values b with(nolock)
  15. where b.number>=1 and b.number<len(a.[序列号]) and b.type='P'
  16. and substring(','+[序列号],number,1)=','
  17. go
  18. drop table tb
  19. go
  20. /**
  21. 编号   产品   数量          单价          金额          序列号
  22. ---- ---- ----------- ----------- ----------- --------
  23. 001  AA   3           5           15          12
  24. 001  AA   3           5           15          13
  25. 001  AA   3           5           15          14
  26. 002  BB   8           9           13          22
  27. 002  BB   8           9           13          23
  28. 002  BB   8           9           13          24
  29. */

  30. ----------
  31. --2.第四个逗号之前的字符串
  32. declare @str varchar(100)
  33. set @str='10,102,10254,103265,541,2154,41,156'
  34. ;with cte as(
  35. select left(@str,number-1) as ss,row_number()over(order by getdate()) as xh
  36. from master..spt_values with(nolock)
  37. where number>=1 and number<=len(@str+',') and type='P'
  38. and substring(@str+',',number,1)=','
  39. )select ss from cte where xh=4
  40. /**
  41. ss
  42. -------------------
  43. 10,102,10254,103265
  44. */

  45. ----------
  46. --3.找出两句话中相同的汉字
  47. declare @Lctext1 varchar(100)
  48. declare @Lctext2 varchar(100)
  49. set @Lctext1='我们都是来自五湖四海的朋友'
  50. set @Lctext2='朋友多了路真的好走吗'
  51. select substring(@Lctext2,number,1) as value
  52. from master..spt_values with(nolock)
  53. where type='P' and number>=1 and number<=len(@Lctext2)
  54. and charindex(substring(@Lctext2,number,1),@Lctext1,number)>1
  55. /**
  56. value
  57. -----



  58. */

  59. ---------
  60. --4.提取两个日期之间的所有月份
  61. if object_id('tb') is not null drop table tb
  62. go
  63. create table tb(id int identity(1,1),startDate varchar(10),endDate varchar(10))
  64. insert into tb(startDate,endDate) select '2013-01-01','2013-09-25'
  65. go
  66. declare @startDate varchar(10)
  67. declare @endDate varchar(10)
  68. select @startDate=startDate,@endDate=endDate from tb with(nolock)
  69. select convert(varchar(7),dateadd(mm,number,@startDate),120) as [月份]
  70. from master..spt_values with(nolock)
  71. where type='P' and number>=0
  72. and dateadd(mm,number,@startDate)<=@endDate
  73. go
  74. drop table tb
  75. go
  76. /**
  77. 月份
  78. -------
  79. 2013-01
  80. 2013-02
  81. 2013-03
  82. 2013-04
  83. 2013-05
  84. 2013-06
  85. 2013-07
  86. 2013-08
  87. 2013-09
  88. */

  89. ---------
  90. --5.求一个日期所在月份的所有日期
  91. declare @date datetime
  92. set @date='2013-08-31'
  93. select convert(char(7),@date,120)+'-'+right('0'+convert(varchar(2),number),2) as [日期格式1]
  94. ,ltrim(year(@date))+right(100+month(@date),2)+right('0'+ltrim(number),2) as [日期格式2]
  95. from master..spt_values with(nolock)
  96. where type='P' and number>=1
  97. --and number<=datediff(dd,@date,dateadd(mm,1,@date)) --对于mssql而言该语句不试用于2013-08-31的情况,这时由于9月没有31号,固计算出来的天数是30天
  98. and number<=datediff(dd,convert(char(7),@date,120)+'-01',convert(char(7),dateadd(mm,1,@date),120)+'-01')--转换为1号来计算天数
  99. /**
  100. 日期格式1       日期格式2
  101. ----------- --------------------
  102. 2013-08-01  20130801
  103. 2013-08-02  20130802
  104. 2013-08-03  20130803
  105. 2013-08-04  20130804
  106. 2013-08-05  20130805
  107. 2013-08-06  20130806
  108. 2013-08-07  20130807
  109. 2013-08-08  20130808
  110. 2013-08-09  20130809
  111. 2013-08-10  20130810
  112. 2013-08-11  20130811
  113. 2013-08-12  20130812
  114. 2013-08-13  20130813
  115. 2013-08-14  20130814
  116. 2013-08-15  20130815
  117. 2013-08-16  20130816
  118. 2013-08-17  20130817
  119. 2013-08-18  20130818
  120. 2013-08-19  20130819
  121. 2013-08-20  20130820
  122. 2013-08-21  20130821
  123. 2013-08-22  20130822
  124. 2013-08-23  20130823
  125. 2013-08-24  20130824
  126. 2013-08-25  20130825
  127. 2013-08-26  20130826
  128. 2013-08-27  20130827
  129. 2013-08-28  20130828
  130. 2013-08-29  20130829
  131. 2013-08-30  20130830
  132. 2013-08-31  20130831
  133. */

  134. ---------
  135. --6.根据给定时间为基准以2小时为划分,得出一天划分出的时间段
  136. declare @time varchar(5)
  137. set @time='11:13'
  138. select ltrim(a.number)+right(@time,3)+'-'+ltrim(b.number)+right(@time,3) as [划分结果]
  139. from master..spt_values a with(nolock),master..spt_values b with(nolock)
  140. where a.type='P' and b.type='P'
  141. and a.number>=left(@time,2) and b.number<=24
  142. and a.number+2=b.number
  143. /**
  144. 划分结果
  145. -----------------------------------
  146. 11:13-13:13
  147. 12:13-14:13
  148. 13:13-15:13
  149. 14:13-16:13
  150. 15:13-17:13
  151. 16:13-18:13
  152. 17:13-19:13
  153. 18:13-20:13
  154. 19:13-21:13
  155. 20:13-22:13
  156. 21:13-23:13
  157. 22:13-24:13
  158. */

  159. ---------
  160. --7.将字符串显示为行列
  161. if object_id('tb') is not null drop table tb
  162. create table tb(id int identity(1,1),s nvarchar(100))
  163. insert into tb(s) select '车位地址1,车位状况1|车位地址2,车位状况2|车位地址n,车位状况n'
  164. ;with cte as(
  165. select substring(s,number,charindex('|',s+'|',number)-number) as ss
  166. from tb with(nolock),master..spt_values with(nolock)
  167. where type='P' and number>=1 and number<=len(s)
  168. and substring('|'+s,number,1)='|'
  169. )select left(ss,charindex(',',ss)-1)as s1,substring(ss,charindex(',',ss)+1,len(ss))as s2 from cte
  170. drop table tb
  171. /**
  172. s1             s2
  173. ----------- ------------
  174. 车位地址1      车位状况1
  175. 车位地址2      车位状况2
  176. 车位地址n      车位状况n
  177. */
复制代码




回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-4-16 22:15

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表