使用SQL Server Management Studio。想从列中拉出月份。尝试过以下方法:
ATTEMPT 1:
,Convert (CHAR, e.TERM_DATE, 110) AS [Due Date]
,PAT_CASE.OLD_CASE_ID AS [Case Number]
,CASE
WHEN e.TERM_DATE = 'MM-DD-YYYY'
THEN DateName(MONTH, GETDATE())
ELSE e.TERM_DATE
END AS [MONTH]
错误消息:
Msg 241,Level 16,State 1,Line 1
从字符串转换日期和/或时间时转换失败。
ATTEMPT 2:
,Convert (CHAR, e.TERM_DATE, 110) AS [Due Date]
,PAT_CASE.OLD_CASE_ID AS [Case Number]
,CASE
WHEN e.TERM_DATE = 'MM-DD-YYYY'
THEN DateName( month , DateAdd( month , @MonthNumber , -1 ))
ELSE e.TERM_DATE
END AS [MONTH]
错误消息:
Msg 137,Level 15,State 2,Line 6
必须声明标量变量“@MonthNumber” Msg 156,Level 15,State 1,Line 55
关键字“AND”附近的语法不正确。
尝试3:
,Convert (CHAR, e.TERM_DATE, 110) AS [Due Date]
,PAT_CASE.OLD_CASE_ID AS [Case Number]
,CASE
WHEN e.TERM_DATE = 'MM-DD-YYYY'
THEN SUBSTRING('JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ', (@intMonth * 4) - 3, 3)
ELSE e.TERM_DATE
END AS [MONTH]
错误消息:
Msg 137,Level 15,State 2,Line 6
必须声明标量变量“@intMonth” Msg 156,Level 15,State 1,Line 55
关键字“AND”附近的语法不正确。
答案 0 :(得分:0)
您的CAST
以及第一个示例中的WHEN e.TERM_DATE = 'MM-DD-YYYY'
个错误意味着e.TERM_DATE
是DATETIME
所以:
SELECT DATENAME(MONTH, e.TERM_DATE)