This topic has been archived. It cannot be replied.
-
工作学习 / IT技术讨论 / help: Sql 语句假设有一个SQL 语句是这样的:
SELECT CONVERT(varchar, job_id) + ' hello' AS jobid
FROM employee
其中job_id 是INT 数据类型,现在规定如果job_id 大于10 ,那么执行SQL 语句 后,在job_id 后跟上hello 的字符串,否则就直接把job_id 的值显示出来。比如 job_id =11 ,那么最后显示的结果是11hello,如果等于3,结果是3。
那么该如何写这条SQL 语句。
-bobo123(bobo);
2004-6-11
{338}
(#1761036@0)
-
union?
-goes(goes);
2004-6-11
(#1761042@0)
-
Use UNION.SELECT CONVERT(varchar, job_id) + ' hello' AS jobid
FROM employee
where job_id > 10
UNION
SELECT CONVERT(varchar, job_id) AS jobid
FROM employee
where job_id <= 10
-sailor(野苹果);
2004-6-11
{172}
(#1761045@0)
-
Try thisSelect
Case job_id
When >10 Then CONVERT(varchar, job_id) + 'hello'
Else CONVERT(varchar, job_id)
End As jobid
FROM employee
-charleslike(愚人);
2004-6-11
{152}
(#1761051@0)
-
这个执行的时候出现了语法错误
-bobo123(bobo);
2004-6-11
(#1761066@0)
-
家里没装SQL SERVER,凭记忆应该差不多了,以前用过很多类似的,你在改改试试。
-charleslike(愚人);
2004-6-11
(#1761077@0)
-
CONVERT(VARCHAR(10), job_id)
-charleslike(愚人);
2004-6-11
(#1761081@0)
-
use CASE statementSELECT CASE WHEN job_id > 10 THEN CONVERT(varchar, job_id) + ' hello' AS jobid else job_id AS jobid END
FROM employee
-schen(糊涂阿福);
2004-6-12
{120}
(#1761273@0)
-
try thisselect convert(varchar(10),job_id) + case when job_id>10 then 'hello' else '' end as job_id
from employee
-moodgood(ButterCup);
2004-6-12
{110}
(#1761634@0)