有一个关于sql的问题需要解决 表结构和数据如下: ID Name1 Name2 1 N1 NULL 2 N2 NULL 3 NULL N3 4 N4 NULL 我想最终显示的时候仍然是 1 N1 2 N2 3 N3 4 N4 请问这样的SQL语句该如何写? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 select id,isnull(name1,name2) from tb SELECT ID, ISNULL(Name1,Name2) AS NameFROM tb --不是问过?现在问题是?if object_id('tb')is not null drop table tbgocreate table tb(ID int, Name1 varchar(10), Name2 varchar(10)) insert tb select 1, 'N1', NULL insert tb select 2, 'N2' , NULL insert tb select 3, NULL , 'N3' insert tb select 4, 'N4' , NULL select id,isnull(name1,name2)[name] from tb/*id name----------- ----------1 N12 N23 N34 N4*/ SQL codeif object_id('tempdb..#')is not null drop table #gocreate table #(ID int, Name1 varchar(10), Name2 varchar(10)) insert # select 1 ,'N1' ,NULL insert # select 2 , 'N2' , NULL insert # select 3 , NULL , 'N3' insert # select 4, 'N4', NULL select id,[name] from (select id,name1 [Name] from # union all select id,name2 from #)Twhere [name]is not null/*id name----------- ----------1 N12 N24 N43 N3*/ select id,isnull(name1,name2)[name] from tb SELECT ID,CASE WHEN NAME1 IS NULL THEN NAME2 ELSE NAME1 END AS [NAME] FROM TB select id,isnull(name1,name2) from tb 请教个sql server备份的问题 应收应付问题,怎么把一列数字分为两列显示 ~~~!!!比较难的SQL!!!~~~ SQL语句,取数据库中前五条数据,并且把5条数据转成行的形式显示出来 _bak 是数据库备份文件吗(在一个源码文件里,它是这样标注的) 远程登陆sql server 2005,出现问题! 对数据类型而言运算符无效。运算符为 add,类型为 uniqueidentifier。 请问这个sql 语句怎么写? 表A中有1000条纪录,表B中有30条纪录,要求把B和A结合在一起,特简单的问题! 复制数据时如何去掉控制字符? 一个简单的删除语句 请教关于MS-SQL讲课建议
ID,
ISNULL(Name1,Name2) AS Name
FROM tb
if object_id('tb')is not null drop table tb
go
create table tb(ID int, Name1 varchar(10), Name2 varchar(10))
insert tb select 1, 'N1', NULL
insert tb select 2, 'N2' , NULL
insert tb select 3, NULL , 'N3'
insert tb select 4, 'N4' , NULL
select id,isnull(name1,name2)[name] from tb
/*id name
----------- ----------
1 N1
2 N2
3 N3
4 N4*/
SQL codeif object_id('tempdb..#')is not null drop table #
go
create table #(ID int, Name1 varchar(10), Name2 varchar(10))
insert # select 1 ,'N1' ,NULL
insert # select 2 , 'N2' , NULL
insert # select 3 , NULL , 'N3'
insert # select 4, 'N4', NULL
select id,[name] from (
select id,name1 [Name] from #
union all
select id,name2 from #)T
where [name]is not null
/*id name
----------- ----------
1 N1
2 N2
4 N4
3 N3*/