Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning Month Name in SQL Server Query

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.

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 

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

Incorrect syntax near 'AS'

message. What is the proper syntax for my query?

like image 913
Casey DiBroaggio Avatar asked Apr 13 '11 14:04

Casey DiBroaggio


People also ask

How do I get the current month in SQL Query?

We can retrieve the current month value in SQL using the MONTH() and DATEPART() functions along with the GETDATE() function. To retrieve the name of the month functions in SQL such as DATENAME() and FORMAT() are used.


1 Answers

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) 
like image 200
Mikael Eriksson Avatar answered Sep 21 '22 17:09

Mikael Eriksson