如何将整数与字符串连接起来

时间:2015-09-23 09:27:08

标签: sql sql-server

我正在尝试这个

CAST(DATEDIFF(month,[patient_date_birth],getdate()) as varchar(10))+ 'month'

但没有工作!! 任何帮助,请

这是我的选择查询

SELECT study_patient_name+' '+study_patient_prenom as Patient,
    CASE 
        WHEN DATEDIFF(month,patient_date_birth,getdate()) > 12 THEN DATEDIFF(year,patient_date_birth,getdate()) 
        ELSE CAST(DATEDIFF(month,patient_date_birth,getdate()) as varchar )+ ' month' 
    END as Age 
from patient

3 个答案:

答案 0 :(得分:1)

这应该有效

CAST(DATEDIFF(month,CAST([patient_date_birth] AS DATE),getdate()) as varchar(10))+ ' month'

答案 1 :(得分:0)

我犯的错误是我没有投出案例CAST(DATEDIFF(year,patient_date_birth,getdate()) as varchar)

的第一部分

答案 2 :(得分:-1)

您需要将CASE语句的两个元素都转换为varchar,因此无论是否' month',结果始终为varchar。出现在结果中:

SELECT
'dave'+' '+'rave' as Patient,
'ENT' as Service,
CASE 
    WHEN DATEDIFF(month, '2015-02-25', getdate()) > 12 
    THEN CAST(DATEDIFF(year, '2015-02-25', getdate()) as varchar(10))
    ELSE CAST(DATEDIFF(month, '2015-02-25', getdate()) as varchar(10)) + ' month' 
END as Age