0

I have a report which returns number of doors sold in a month from 2 locations. The month is displayed as a value 1-12.... I would like to return 1-Jan, 2-Feb etc.

table

EDIT

I am using: datepart(MONTH, DeFactoUser.F_ST_Transaction.STTR_DATE) to get the month number, I have tried: datepart(MONTH, DeFactoUser.F_ST_Transaction.STTR_DATE) + '-' + datename(MONTH, DeFactoUser.F_ST_Transaction.STTR_DATE) to get the name appended to the number. I get an error Conversion failed when converting the nvarchar value 'September' to data type int.

Obviously doing something silly.... any clues would be good.

Bugs
  • 4,491
  • 9
  • 32
  • 41
R_Avery_17
  • 305
  • 4
  • 19

3 Answers3

2

If your month column is a date you can use the DATENAME function.

If it is just a number you can still use DATENAME but simulate the date with DATEFROMPARTS e.g.

SELECT datename(MONTH,datefromparts(2017,1,1))

then just concatenate your number and the name with

concat(cast(yournum AS varchar(2)),'-',datename(MONTH,datefromparts(2017,1,1)))
SQLBadPanda
  • 625
  • 5
  • 7
1

try this:

SELECT CAST(MONTH(GETDATE()) AS nvarchar(2))  +'-'+ CONVERT(varchar(3), GETDATE(), 100)
Marian Nasry
  • 821
  • 9
  • 22
1

You can use a case expression:

SELECT CASE WHEN [Month] = 1 THEN '1-Jan'
WHEN [Month] = 2 THEN '2-Feb'
WHEN [Month] = 3 THEN '3-Mar'
...
END AS [MonthName]

Alternative is using DATENAME:

SELECT CAST(MONTH(1) AS NVARCHAR(2))+'-'+LEFT(UPPER(DATENAME(MONTH,MONTH(1))),3)

This has been asked before: Convert Month Number to Month Name Function in SQL

If you do not want a case expression.

Keith
  • 1,008
  • 10
  • 19