仅将日期时间转换为月-年 - T-SQL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23490031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting datetime to month-year only - T-SQL
提问by StackTrace
I want to convert a dateTime value to Month-Year (note the (-)between them).
我想将 dateTime 值转换为 Month-Year (注意它们之间的(-))。
i want them like Jan-2014, Feb-2014, Mar-2014, Apr-2014.
我希望它们像 2014 年 1 月、2014 年 2 月、2014 年 3 月、2014 年 4 月一样。
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)
但是第一行给了我 2014-05(我需要它像 2014 年 5 月一样),而第二行给了 2014 年 5 月(它缺少 -,如 2014 年 5 月)
回答by dean
You were almost there:
你快到了:
SELECT replace(right(convert(varchar, getdate(), 106), 8), ' ', '-')
回答by Deepshikha
Write as:
写成:
SELECT REPLACE(RIGHT(CONVERT(VARCHAR(11),
GETDATE(), 106), 8),
' ', '-') AS [Mon-YYYY]
回答by Brett Schneider
i think convert my not be the correct approach. try this:
我认为转换我不是正确的方法。尝试这个:
select substring(DATENAME(M,GETDATE()),1,3)+'-'+DATENAME(YYYY,GETDATE())
回答by mohan111
SELECT
CONVERT(CHAR(4), getdate(), 100) +'-'+ CONVERT(CHAR(4), getdate(), 120)
回答by huMpty duMpty
SELECT CONVERT(CHAR(4), GETDATE(), 100) +'-'+ CONVERT(CHAR(4), GETDATE(), 120)