SQL查询GETGATE()和DATEPART格式。

时间:2021-12-13 01:45:24

The individual who implemented a db, setup the date column format = 'MONTH, YEAR"

实现db的个人,设置日期列格式= '月,年"

Example 'December, 2017'

例子的2017年12月

In order to automate query, I need to pass date as a parameter to the where clause.

为了自动化查询,我需要将日期作为参数传递给where子句。

So, I started to work with the DATE function. Is there a way to convert DATEPART from integer to full month name?

我开始使用日期函数。是否有方法将DATEPART从整数转换为完整月份名称?

 DECLARE @dtDate DATE
 DECLARE @dtMonth varchar(50)
 DECLARE @dtYear varchar(50)

 SET @dtDate = GETDATE();
 SET @dtMonth = convert(varchar, DATEPART(mm, @dtDate))
 SET @dtYear = convert(varchar, DATEPART(yyyy, @dtDate))

1 个解决方案

#1


3  

Just use datename

只使用datename

declare @dtDate datetime = '2017-01-01'
declare @dtMonth nvarchar(10)
SET @dtMonth = datename(month, @dtDate)
print @dtMonth

Output:January

#1


3  

Just use datename

只使用datename

declare @dtDate datetime = '2017-01-01'
declare @dtMonth nvarchar(10)
SET @dtMonth = datename(month, @dtDate)
print @dtMonth

Output:January