/**
最近在CSDN上见到很多关于master..spt_values的应用,感觉这个东西太好用了,
所以搜索了很多关于这方面的应用。
在此贴上自己的总结结果,希望各路大神批评指教,也希望大家继续把这方面的应用贴上。
*/
select number from master..spt_values with(nolock) where type='P'
/**解释:master..spt_values表的字段值为P的对应number字段值是从0-2047*/
-----------
--1.将字符串转换为列显示
if object_id('tb') is not null drop table tb
go
create table tb([编号] varchar(3),[产品] varchar(2),[数量] int,[单价] int,[金额] int,[序列号] varchar(8))
insert into tb([编号],[产品],[数量],[单价],[金额],[序列号])
select '001','AA',3,5,15,'12,13,14' union all
select '002','BB',8,9,13,'22,23,24'
go
select [编号],[产品],[数量],[单价],[金额]
,substring([序列号],b.number,charindex(',',[序列号]+',',b.number)-b.number) as [序列号]
from tb a with(nolock),master..spt_values b with(nolock)
where b.number>=1 and b.number<len(a.[序列号]) and b.type='P'
and substring(','+[序列号],number,1)=','
go
drop table tb
go
/**
编号 产品 数量 单价 金额 序列号
---- ---- ----------- ----------- ----------- --------
001 AA 3 5 15 12
001 AA 3 5 15 13
001 AA 3 5 15 14
002 BB 8 9 13 22
002 BB 8 9 13 23
002 BB 8 9 13 24
*/
----------
--2.第四个逗号之前的字符串
declare @str varchar(100)
set @str='10,102,10254,103265,541,2154,41,156'
;with cte as(
select left(@str,number-1) as ss,row_number()over(order by getdate()) as xh
from master..spt_values with(nolock)
where number>=1 and number<=len(@str+',') and type='P'
and substring(@str+',',number,1)=','
)select ss from cte where xh=4
/**
ss
-------------------
10,102,10254,103265
*/
----------
--3.找出两句话中相同的汉字
declare @Lctext1 varchar(100)
declare @Lctext2 varchar(100)
set @Lctext1='我们都是来自五湖四海的朋友'
set @Lctext2='朋友多了路真的好走吗'
select substring(@Lctext2,number,1) as value
from master..spt_values with(nolock)
where type='P' and number>=1 and number<=len(@Lctext2)
and charindex(substring(@Lctext2,number,1),@Lctext1,number)>1
/**
value
-----
朋
友
的
*/
---------
--4.提取两个日期之间的所有月份
if object_id('tb') is not null drop table tb
go
create table tb(id int identity(1,1),startDate varchar(10),endDate varchar(10))
insert into tb(startDate,endDate) select '2013-01-01','2013-09-25'
go
declare @startDate varchar(10)
declare @endDate varchar(10)
select @startDate=startDate,@endDate=endDate from tb with(nolock)
select convert(varchar(7),dateadd(mm,number,@startDate),120) as [月份]
from master..spt_values with(nolock)
where type='P' and number>=0
and dateadd(mm,number,@startDate)<=@endDate
go
drop table tb
go
/**
月份
-------
2013-01
2013-02
2013-03
2013-04
2013-05
2013-06
2013-07
2013-08
2013-09
*/
---------
--5.求一个日期所在月份的所有日期
declare @date datetime
set @date='2013-08-31'
select convert(char(7),@date,120)+'-'+right('0'+convert(varchar(2),number),2) as [日期格式1]
,ltrim(year(@date))+right(100+month(@date),2)+right('0'+ltrim(number),2) as [日期格式2]
from master..spt_values with(nolock)
where type='P' and number>=1
--and number<=datediff(dd,@date,dateadd(mm,1,@date)) --对于mssql而言该语句不试用于2013-08-31的情况,这时由于9月没有31号,固计算出来的天数是30天
and number<=datediff(dd,convert(char(7),@date,120)+'-01',convert(char(7),dateadd(mm,1,@date),120)+'-01')--转换为1号来计算天数
/**
日期格式1 日期格式2
----------- --------------------
2013-08-01 20130801
2013-08-02 20130802
详细解决方案
-1.关于master.spt_values的应用帖子-该如何处理
热度:25 发布时间:2016-04-24 10:00:45.0
相关解决方案
- master.FindControl的有关问题,初学者!
- Parser Error Message: The file '/MasterPage.master' does not exist.解决办法
- 母板页(master)怎么绑定皮肤
- (急)在数据库 'master' 中回绝了 CREATE DATABASE 权限。~~~~~~~~~~
- 在数据库 'master' 中拒绝了 CREATE DATABASE 权限。解决方案
- master page 下,無法觸發gridview GridView2_SelectedIndexChanged事件,為什麼?该怎么处理
- master page頁面怎么用js來做輸入驗證
- sharepoint 2010 页面增添logo banner方法 custom banner for sharepoint 2010 master page
- sharepoint 2010 页面增添footer方法 custom footer for sharepoint 2010 master page
- 执行"EXEC master.xp_cmdshell."后找不到相干文件,请大家帮忙解答
- mysql主主同步,show master status时,提示empty set.
- 问个基础有关问题:USE master
- 关于执行 EXEC master.xp_cmdshell @sql 的有关问题
- exec master.xp_cmdshell net use 有关问题,多谢!请进入:)
- 怎么修改SQL2008 系统库(master,tempdb等)的 排序规则名称
- master.spt_values表的作用是什么,几个字段的含义是什么,哪位高手能讲讲呢
- 2005 management studio 下面 找不到 master 数据库解决办法
- 邮件发送,exec master.dbo.xp_sendmail提示异常,求!请高手帮忙!多谢
- 在查询分析器中可以执行 EXEC master.dbo.xp_cmdshell 'd:\ab\p.exe beep' 为什么将它移到触发器中却不能执行?解决思路
- 启动sql服务管理器,想启动里面的数据库,结果启动命令执行后还是停止的状态,查看日志说FCB:Open failed: c:\Server\MSSQL\data\master.md,该怎么处理
- SQLSERVER打开出现找不到存储过程'master.dbo.xp_MSplatform'解决方法
- EXEC master.xp_cmdshell 导出Excel能不能设置Excel字体格式,该如何处理
- master.dbo.xp_cmdshell 被删除了 不允许用 怎么处理
- 出现ERROR: Can't get master address from ZooKeeper; znode data == null解决方法
- 故障案例:主从同步出错Fatal error: The slave I/O thread stops because master and slave have equal MySQL server
- 记处置线上记录垃圾日志 The view 'Error' or its master was not found
- git checkout 出错原因 (error: pathspec 'master' did not match any file(s) known to )
- An error occurred while performing `git pull` on repo `master
- sharepoint2013 master pages和日历有关问题
- "Error Rendering Control” in master page, % of availabe storage used 99.20%,该如何解决