在 SQL Server 查询中返回月份名称 [英] Returning Month Name in SQL Server Query

查看:60
本文介绍了在 SQL Server 查询中返回月份名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用SQL Server 2008,我有一个用于创建视图的查询,我试图显示月份名称而不是整数.

Using SQL Server 2008, I have a query that is used to create a view and I'm trying to display a month's name instead of an integer.

在我的数据库中,datetime 位于名为 OrderDateTime 的列中.查询中返回日期的行是:

In my database, the datetime is in a column called OrderDateTime. The lines in the query that return the date is:

DATENAME(yyyy, S0.OrderDateTime) AS OrderYear,
DATEPART(MONTH, S0.OrderDateTime) AS OrderMonth

这会以整数形式返回一列年份和一列月份.我想返回月份名称(Jan、Feb 等).我试过了:

This returns a column of years and a column of months as integers. I want to return the month names (Jan, Feb, etc). I've tried:

CONVERT(varchar(3), DATEPART(MONTH, S0.OrderDateTime) AS OrderMonth

这显然是不正确的,因为我明白了

This is obviously is incorrect, as I get

'AS' 附近的语法不正确

Incorrect syntax near 'AS'

留言.我的查询的正确语法是什么?

message. What is the proper syntax for my query?

推荐答案

这将为您提供月份的全名.

This will give you the full name of the month.

select datename(month, S0.OrderDateTime)

如果你只想要前三个字母,你可以使用这个

If you only want the first three letters you can use this

select convert(char(3), S0.OrderDateTime, 0)

这篇关于在 SQL Server 查询中返回月份名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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