如何从SQL中的日期字段中提取月份 [英] How to extract month on from the date field in SQL

查看:2486
本文介绍了如何从SQL中的日期字段中提取月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表格中有一个日期字段名称为InvoiceDate,其日期信息如此20140231,我想从该日期字段中获取月份,以便我可以按季度对记录进行分组,请参阅附件我试过的查询,目前日期字段是硬编码的,如您在查询中看到的那样。

请帮助我如何获得此记录中的月份

I have a date field name as "InvoiceDate" in my table that have a date information like this "20140231", I want to get the month out of that date field so that I can group my record per quarter see the attached query that I tried, currently the date field is hard coded as you see on the query.
Can you please help how can I get the month on this record

"20140231", 





我的尝试:



这是我试过的东西



What I have tried:

This what I tried

SELECT TOP 1000 [Advise no]
      ,[Amount]
      ,[Date]
      ,[Bas Fund Description]
	  ,[Invoice Date]
         (MONTH, DATEDIFF(MONTH, 0, [Invoice Date]), 0) AS months
	   , CASE WHEN [Invoice Date] BETWEEN 20170401 AND 20170631
             THEN 'Q1' 
           WHEN [Invoice Date] BETWEEN 20170701 AND 20170931
             THEN 'Q2' 
           WHEN [Invoice Date] BETWEEN 20171001 AND 20171231
             THEN 'Q3'
           WHEN [Invoice Date] BETWEEN 20180101 AND 20180331 
              THEN 'Q4' 
           WHEN [Invoice Date] Is Null 
              THEN 'Null' 

        END AS QuarterPeriod 
      ,[Valuation Date]
      ,[qgs_geometry]
      ,[qgs_fid]
      ,[ProvinceName]
  FROM [DOD].[dbo].[View_DPW_PACE]

推荐答案

SELECT MONTH('2014-02-31')

请参阅: MONTH(Transact-SQL)| Microsoft Docs [ ^ ]


这篇关于如何从SQL中的日期字段中提取月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆