将月份名称转换为日期/月份编号(问题和答案的组合)

时间:2018-12-03 14:26:06

标签: sql-server tsql

我看了很多关于我的问题的答案,但没有一个人能真正回答我的问题。

我有数据:

cBatchDesc  idBatches
Journal Batch - March 2017  88
Journal Batch - April 2017  2
Journal Batch - May 2017    3
Journal Batch - June 2017   4
Journal Batch - July 2017   5
Journal Batch - August 2017 6
Journal Batch - September 2017  7
Journal Batch - October 2017    8
Journal Batch - November 2017   9
Journal Batch - December 2017   10
Journal Batch - January 2018    11
Journal Batch - February 2018   12
Journal Batch - March 2017  89
Journal Batch - April 2017  13
Journal Batch - May 2017    14
Journal Batch - June 2017   15
Journal Batch - July 2017   16
Journal Batch - August 2017 17
Journal Batch - September 2017  18
Journal Batch - October 2017    19
Journal Batch - November 2017   20
Journal Batch - December 2017   21
Journal Batch - January 2018    22
Journal Batch - February 2018   23
Journal Batch - March 2017  90
Journal Batch - April 2017  27
Journal Batch - May 2017    28
Journal Batch - June 2017   29
Journal Batch - July 2017   30
Journal Batch - August 2017 31
Journal Batch - September 2017  32
Journal Batch - October 2017    33
Journal Batch - November 2017   34
Journal Batch - December 2017   35
Journal Batch - January 2018    36
Journal Batch - February 2018   37

我需要将cBatchDesc末尾的日期名称转换为实际日期。

2 个答案:

答案 0 :(得分:3)

在阅读了很多人的帖子后,我设法提出了以下代码:

select
    cBatchDesc
,   substring(cBatchDesc,16,100)    [SUBSTRING]
,   datepart(MM,substring(cBatchDesc,16,100))   [DATEPART]
,   cast(substring(cBatchDesc,16,100) as date)  [CAST]
,   eomonth(cast(substring(cBatchDesc,16,100) as date)) [EOMONTH]
,   idBatches
from    _btblJrBatches
order by cBatchNo

可以注意到,我不得不使用SUBSTRING将月份与说明的其余部分分开。

从那里,我使用DATEPART获得月份号。

使用CAST,我可以将月份名称转换为DATE

然后我需要该月底,您将看到我使用了EOMONTH

请参阅以下结果:

enter image description here

我希望这会有所帮助。

答案 1 :(得分:1)

首先,如果您发布的数据为“易消耗品”,人们将更愿意提供帮助,例如以下内容...

   DROP TABLE IF EXISTS #TestTable;
GO
 SELECT  v.cBatchDesc, v.idBatches
   INTO #TestTable
   FROM (VALUES  
         ('Journal Batch - March 2017'    ,88)
        ,('Journal Batch - April 2017'    ,2 )
        ,('Journal Batch - May 2017'      ,3 )
        ,('Journal Batch - June 2017'     ,4 )
        ,('Journal Batch - July 2017'     ,5 )
        ,('Journal Batch - August 2017'   ,6 )
        ,('Journal Batch - September 2017',7 )
        ,('Journal Batch - October 2017'  ,8 )
        ,('Journal Batch - November 2017' ,9 )
        ,('Journal Batch - December 2017' ,10)
        ,('Journal Batch - January 2018'  ,11)
        ,('Journal Batch - February 2018' ,12)
        ,('Journal Batch - March 2017'    ,89)
        ,('Journal Batch - April 2017'    ,13)
        ,('Journal Batch - May 2017'      ,14)
        ,('Journal Batch - June 2017'     ,15)
        ,('Journal Batch - July 2017'     ,16)
        ,('Journal Batch - August 2017'   ,17)
        ,('Journal Batch - September 2017',18)
        ,('Journal Batch - October 2017'  ,19)
        ,('Journal Batch - November 2017' ,20)
        ,('Journal Batch - December 2017' ,21)
        ,('Journal Batch - January 2018'  ,22)
        ,('Journal Batch - February 2018' ,23)
        ,('Journal Batch - March 2017'    ,90)
        ,('Journal Batch - April 2017'    ,27)
        ,('Journal Batch - May 2017'      ,28)
        ,('Journal Batch - June 2017'     ,29)
        ,('Journal Batch - July 2017'     ,30)
        ,('Journal Batch - August 2017'   ,31)
        ,('Journal Batch - September 2017',32)
        ,('Journal Batch - October 2017'  ,33)
        ,('Journal Batch - November 2017' ,34)
        ,('Journal Batch - December 2017' ,35)
        ,('Journal Batch - January 2018'  ,36)
        ,('Journal Batch - February 2018' ,37)
        )v(cBatchDesc,idBatches)
;

很高兴您自己解决了这个问题,但是可以通过计算“-”的位置使事情变得更加灵活,以便在出现其他描述时也可以进行处理。

此外,您还可以将代码“干燥”( D 而不是 R 自行复制 Y )(以使其更容易读取/更改),使用像这样的CROSS APPLY为每行只计算一次日期(请注意,您没有提供cBatchNo列,因此ORDER BY不同,因此我没有显示SUBSTRING)...

 SELECT  cBatchDesc
        ,MonthNumber = DATEPART(mm,ca.Date)
        ,MonthStart  = ca.Date
        ,MonthEnd    = EOMONTH(ca.Date)
        ,idBatches
   FROM #TestTable
  CROSS APPLY (SELECT CONVERT(DATE,SUBSTRING(cBatchDesc,CHARINDEX('-',cBatchDesc)+1,100)))ca(Date)
  ORDER BY ca.Date, idBatches
;

对于您提供的数据,可提供以下(截断的)结果...

enter image description here

相关问题