<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      【轉】SQL Server 中master..spt_values的應用

      今天在做數據分析報表的時候遇到一個這樣的問題。

      表結構如下。
      部門編碼、部門名稱、部門人員ID(中間用逗號分割)

      我想通過和人員表鏈接,查詢出一個新的數據集,查詢出的結果集格式如下:
      人員信息(ID或者姓名)、部門編碼、部門名稱

       

      以前都是通過程序遍歷拆分表字段組成新的集合字段,然后在結合SQL語句查詢出結果集,但是這個報表要求只能通過SQL語句實現,以前記得可以通過寫字段分割函數再結合游標實現。然而今天在網上無意間找到一個新的方法。用“master..spt_values”來實現,具體實現方法見下面實例1感覺這個東西太好用了。把網上的實例都整理了一下,希望各路大神批評指教,也希望大家繼續把這方面的應用貼上.

       

      復制代碼
      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 
      2013-08-03  20130803 
      2013-08-04  20130804 
      2013-08-05  20130805 
      2013-08-06  20130806 
      2013-08-07  20130807 
      2013-08-08  20130808 
      2013-08-09  20130809 
      2013-08-10  20130810 
      2013-08-11  20130811 
      2013-08-12  20130812 
      2013-08-13  20130813 
      2013-08-14  20130814 
      2013-08-15  20130815 
      2013-08-16  20130816 
      2013-08-17  20130817 
      2013-08-18  20130818 
      2013-08-19  20130819 
      2013-08-20  20130820 
      2013-08-21  20130821 
      2013-08-22  20130822 
      2013-08-23  20130823 
      2013-08-24  20130824 
      2013-08-25  20130825 
      2013-08-26  20130826 
      2013-08-27  20130827 
      2013-08-28  20130828 
      2013-08-29  20130829 
      2013-08-30  20130830 
      2013-08-31  20130831 
      */ 
      --------- 
        
        
      --6.根據給定時間為基準以2小時為劃分,得出一天劃分出的時間段 
      declare @time varchar(5) 
      set @time='11:13'
      select ltrim(a.number)+right(@time,3)+'-'+ltrim(b.number)+right(@time,3) as [劃分結果] 
      from master..spt_values a with(nolock),master..spt_values b with(nolock) 
      where a.type='P' and b.type='P'
      and a.number>=left(@time,2) and b.number<=24 
      and a.number+2=b.number 
      /** 
      劃分結果 
      ----------------------------------- 
      11:13-13:13 
      12:13-14:13 
      13:13-15:13 
      14:13-16:13 
      15:13-17:13 
      16:13-18:13 
      17:13-19:13 
      18:13-20:13 
      19:13-21:13 
      20:13-22:13 
      21:13-23:13 
      22:13-24:13 
      */ 
      --------- 
        
        
      --7.將字符串顯示為行列 
      if object_id('tb') is not null drop table tb 
      create table tb(id int identity(1,1),s nvarchar(100)) 
      insert into tb(s) select '車位地址1,車位狀況1|車位地址2,車位狀況2|車位地址n,車位狀況n'
      ;with cte as( 
      select substring(s,number,charindex('|',s+'|',number)-number) as ss 
      from tb with(nolock),master..spt_values with(nolock) 
      where type='P' and number>=1 and number<=len(s) 
      and substring('|'+s,number,1)='|'
      )select left(ss,charindex(',',ss)-1)as s1,substring(ss,charindex(',',ss)+1,len(ss))as s2 from cte 
      drop table tb 
      /** 
      s1             s2 
      ----------- ------------ 
      車位地址1      車位狀況1 
      車位地址2      車位狀況2 
      車位地址n      車位狀況n 
      */
      復制代碼

       

      轉自作者:靈雨飄零

      出處:http://www.rzrgm.cn/kingboy2008/p/3978007.html

      posted @ 2022-07-04 14:36  竹樓風雨聲  閱讀(224)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 久久久亚洲欧洲日产国码αv| 河东区| 免费午夜无码片在线观看影院| 国产精品青草久久久久福利99| 久久99精品中文字幕在| 中文字幕有码日韩精品| 国产精品呻吟一区二区三区| 俺也来俺也去俺也射| 农民人伦一区二区三区| 国产日产欧产精品精品| 中文字幕精品人妻丝袜| 国产亚洲一区二区三区四区| 久久发布国产伦子伦精品| 人妻系列中文字幕精品| 亚洲精品一区二区妖精| 一区二区三区人妻无码| 风流老熟女一区二区三区| 国产AV无码专区亚洲AV潘金链| 林甸县| 国产视频最新| 蜜桃av一区二区高潮久久精品| 久爱无码精品免费视频在线观看| 九九热在线免费精品视频| 女同另类激情在线三区| 国产综合亚洲区在线观看| 亚洲国产精品久久久天堂麻豆宅男 | 精品无码黑人又粗又大又长| 起碰免费公开97在线视频| 宝贝腿开大点我添添公口述视频| 延庆县| 国产精品亚洲综合一区二区| 成人做爰69片免费看网站野花| 亚洲 欧美 唯美 国产 伦 综合| 性视频一区| 日韩国产精品无码一区二区三区| 潮喷失禁大喷水无码| 岛国岛国免费v片在线观看| 视频一区二区三区四区不卡| 丰满少妇人妻久久久久久| 亚洲精品专区永久免费区| 18禁超污无遮挡无码网址|