在sql查询的select case中从int值获取文本

时间:2014-09-25 10:35:43

标签: sql sql-server-2008

我需要在select for string值中为人类阅读转换int值。

我尝试两种形式,但任何作品

select id, case Preparacion  when 1 then 'Yes' when 0 then 'No' else preparacion end as   Preparación
from pinchos 
inner join Laboratorios on Laboratorios.CodKais = Pinchos.codKais 
where entradaproduccion = '20140905'

将Preparacion转换为varchar()

select id, case convert(varchar(1),Preparacion)  when '1' then 'Yes' when '0' then 'No' else preparacion end as Preparacion
from pinchos 
inner join Laboratorios on Laboratorios.CodKais = Pinchos.codKais 
where entradaproduccion = '20140905'

我如何显示字符串值?

我只读过数据库中的permisions而没有Preparación表

2 个答案:

答案 0 :(得分:0)

在需要时比较为INT和施法。

试试这个: (未经测试的代码)

select  id, 
        case Preparacion 
            when 1 then 'Yes' 
            when 0 then 'No' 
            else CAST(preparacion as VARCHAR(255)) 
        end as Preparacion
from pinchos 
inner join Laboratorios on Laboratorios.CodKais = Pinchos.codKais 
where entradaproduccion = '20140905'

答案 1 :(得分:-2)

你有其他人在那里

选择id,case Preparacion
当1然后'是' 的其他 当0然后'否'否则预备结束 结束为Preparación 来自pinchos Laboratorios.CodKais = Pinchos.codKais上的内部连接Laboratorios 其中entradaproduccion ='20140905'