Вопрос

I want to convert a dateTime value to Month-Year (note the (-) between them).

i want them like Jan-2014, Feb-2014, Mar-2014, Apr-2014.

i have tried

SELECT convert(varchar(7), getdate(), 126) 
SELECT right(convert(varchar, getdate(), 106), 8) 

But the first line gives me 2014-05 (i need it like May-2014) while the second line gives May 2014 (its missing the -, as in May-2014)

Это было полезно?

Решение

You were almost there:

SELECT replace(right(convert(varchar, getdate(), 106), 8), ' ', '-')

Другие советы

Write as:

SELECT REPLACE(RIGHT(CONVERT(VARCHAR(11),
                     GETDATE(), 106), 8),
                     ' ', '-') AS [Mon-YYYY]

i think convert my not be the correct approach. try this:

select substring(DATENAME(M,GETDATE()),1,3)+'-'+DATENAME(YYYY,GETDATE())
SELECT 
  CONVERT(CHAR(4), getdate(), 100) +'-'+ CONVERT(CHAR(4),  getdate(), 120) 
SELECT  CONVERT(CHAR(4), GETDATE(), 100) +'-'+ CONVERT(CHAR(4), GETDATE(), 120) 
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top