假定有一个表
表名:table1
有如下数据列
id col1 col2 col3 col4
1 a aa aa
2 b d aa aa
3 c aa aa
我想查询出如下效果,应该怎样写 id col1 col2 value1 value2
1 yes no a
2 yes yes b d
3 yes no c 请各位帮帮忙,谢谢!
表名:table1
有如下数据列
id col1 col2 col3 col4
1 a aa aa
2 b d aa aa
3 c aa aa
我想查询出如下效果,应该怎样写 id col1 col2 value1 value2
1 yes no a
2 yes yes b d
3 yes no c 请各位帮帮忙,谢谢!
表名:table1
有如下数据列
id col1 col2 col3 col4
1 a aa aa
2 b d aa aa
3 c aa aa
我想查询出如下效果,应该怎样写 id col1 col2 value1 value2
1 yes no a
2 yes yes b d
3 yes no c 请各位帮帮忙,谢谢!
表名:table1
有如下数据列
id col1 col2 col3 col4
1 a aa aa
2 b d aa aa
3 c aa aa
我想查询出如下效果,应该怎样写 id col1 col2 value1 value2
1 yes no a
2 yes yes b d
3 yes no c 请各位帮帮忙,谢谢!
这样排行不行,看的明白吗?
---------------------------------------------------------------
-- DESIGNER :happycell188(喜喜)
-- QQ :584738179
-- Development Tool :Microsoft Visual C++ 6.0 C Language
-- FUNCTION :CONVERT DATA TO T-SQL
---------------------------------------------------------------
-- Microsoft SQL Server 2005
-- Developer Edition on Microsoft Windows XP [版本 5.1.2600]
---------------------------------------------------------------
---------------------------------------------------------------use test
go
if object_id('test.dbo.tb') is not null drop table tb
-- 创建数据表
create table tb
(
id int,
col1 char(2),
col2 char(2),
col3 char(3),
col4 char(3)
)
go
--插入测试数据
insert into tb select 1,'a',null,'aa','aa'
union all select 2,'b','d','aa','aa'
union all select 3,'c',null,'aa','aa'
go
--代码实现select id,col1=case when col1 is null then 'no' else 'yes' end,
col2=case when col2 is null then 'no' else 'yes' end,
value1=isnull(col1,''),
value2=isnull(col2,'')
from tb/*测试结果id col1 col2 value1 value2
-----------------------------------
1 yes no a
2 yes yes b d
3 yes no c (3 行受影响)
*/