如何在MSSQL查询中将单个数字更改为字符串

时间:2016-01-04 20:03:20

标签: sql-server date

SELECT 
a.AccountNumber, a.FirstName, a.LastName, a.Address, a.City, a.State, a.Zip, a.EmailAddress, 
a.PhoneNumber, a.LastUpdated, a.LastVisit, a.TotalSales, a.AccountOpened, a.CustomText4 as StoreCode, 
    CASE
        WHEN (a.CustomText1 = 'JAN') THEN '01' +'/'+a.CustomText2+'/'+a.CustomText3
        WHEN (a.CustomText1 = 'FEB') THEN '02' +'/'+a.CustomText2+'/'+a.CustomText3
        WHEN (a.CustomText1 = 'MAR') THEN '03' +'/'+a.CustomText2+'/'+a.CustomText3
        WHEN (a.CustomText1 = 'APR') THEN '04' +'/'+a.CustomText2+'/'+a.CustomText3
        WHEN (a.CustomText1 = 'MAY') THEN '05' +'/'+a.CustomText2+'/'+a.CustomText3
        WHEN (a.CustomText1 = 'JUN') THEN '06' +'/'+a.CustomText2+'/'+a.CustomText3
        WHEN (a.CustomText1 = 'JUL') THEN '07' +'/'+a.CustomText2+'/'+a.CustomText3
        WHEN (a.CustomText1 = 'AUG') THEN '08' +'/'+a.CustomText2+'/'+a.CustomText3
        WHEN (a.CustomText1 = 'SEP') THEN '09' +'/'+a.CustomText2+'/'+a.CustomText3
        WHEN (a.CustomText1 = 'OCT') THEN '10' +'/'+a.CustomText2+'/'+a.CustomText3
        WHEN (a.CustomText1 = 'NOV') THEN '11' +'/'+a.CustomText2+'/'+a.CustomText3
        WHEN (a.CustomText1 = 'DEC') THEN '12' +'/'+(select CONVERT(decimal(10,2),a.CustomText2))+'/'+a.CustomText3
    END AS DOB, GETDATE() as Extract_date

CustomText1 = JAN~DEC字母列,所以转换为数字。
CustomText2 =是一个带数字格式的日子。

我想将其转换为两位十进制数。任何人都可以帮我写sql查询吗?

1 个答案:

答案 0 :(得分:0)

小心你如何说出你的问题。你不希望decimal,你想要一个字符串,与你的其他字符串片段连接。

这就是你想要的:

...RIGHT('0'+a.CustomText2, 2)...

如果您实际将其转换为小数,则在尝试将其与'/'连接时会出现错误,等等。

如果您需要最终结果为date类型,则将整个CASE语句CAST或CONVERT为该数据类型。