NAME1 NAME2
张三 NULL
李四 NULL
NULL 王五
要求结果是
NAME3
张三
李四
王五
列为varchar类型
18 个解决方案
#1
select isnull(NAME1,NAME2) as NAME3 from tablename
#2
declare @table table (NAME1 varchar(4),NAME2 varchar(4))
insert into @table
select '张三',null union all
select '李四',null union all
select null,'王五'
select isnull(NAME1,NAME2) as NAME3 from @table
/*
NAME3
-----
张三
李四
王五
*/
#3
create table tb(name1 nvarchar(10),name2 nvarchar(10),name3 nvarchar(10))
insert into tb select '张三',NULL,NULL
insert into tb select '李四',NULL,NULL
insert into tb select NULL,'王五',NULL
go
update tb set name3=(case when name1 is null then name2 else name1 end)
select * from tb
go
drop table tb
/*
name1 name2 name3
---------- ---------- ----------
张三 NULL 张三
李四 NULL 李四
NULL 王五 王五
(3 行受影响)
*/
#4
select isnull(name1,name2) from tb
#5
select isnull(NAME1 ,NAME2) name from tb
select name1 name from tb where name1 is not null
union all
select name2 name from tb where name2 is not null
#6
select isnull(name1,name2) from tb
#7
select isnull(name1,name2) as name3 from tb
#8
[code=SQ]
use tempdb;
/*
create table test
(
name1 nvarchar(10),
name2 nvarchar(10)
);
insert into test(name1,name2)
values
('张三',NULL),('李四',NULL),(NULL,'王五');
*/
select ISNULL(name1,name2) as name3 from test;
[/code]
use tempdb;
/*
create table test
(
name1 nvarchar(10),
name2 nvarchar(10)
);
insert into test(name1,name2)
values
('张三',NULL),('李四',NULL),(NULL,'王五');
*/
select ISNULL(name1,name2) as name3 from test;
[/code]
#9
select isnull(name1,name2) from tb
#10
[code=SQ]UNION ALL
[/code]
[/code]
#11
[code=SQ]UNION ALL [/code]
#12
[code=SQ]UNION ALL [/code]
#13
上面的都很简单
我用另外一种方法处理一下
[code=SQ]declare @table table (NAME1 varchar(4),NAME2 varchar(4))
insert into @table
select '张三',null union all
select '李四',null union all
select null,'王五'
select case when name1 is null then name2 else name1 end name3 from @table[/code]
我用另外一种方法处理一下
[code=SQ]declare @table table (NAME1 varchar(4),NAME2 varchar(4))
insert into @table
select '张三',null union all
select '李四',null union all
select null,'王五'
select case when name1 is null then name2 else name1 end name3 from @table[/code]
#14
[code=SQ]
SELECT NAME1+NAME2 AS 'NAME3' FROM TB1
[/code]
SELECT NAME1+NAME2 AS 'NAME3' FROM TB1
[/code]
#15
declare @table table (NAME1 varchar(4),NAME2 varchar(4))
insert into @table
select '张三',null union all
select '李四',null union all
select null,'王五'
SELECT NAME3 = ISNULL(NAME1, '') + ISNULL(NAME2, '') FROM @table
#16
假如两列都没有NULL的
#17
都为空的时候
你也可以把NAME1 和 NAME2 交换一下,决定首选那个数据
select isnull(isnull(NAME1,NAME2),'未命名') as NAME3 from tablename
你也可以把NAME1 和 NAME2 交换一下,决定首选那个数据
#18
还是union all保险
select name1 name from tbname where name1 is not null
union all
select name2 name from tbname where name2 is not null
#1
select isnull(NAME1,NAME2) as NAME3 from tablename
#2
declare @table table (NAME1 varchar(4),NAME2 varchar(4))
insert into @table
select '张三',null union all
select '李四',null union all
select null,'王五'
select isnull(NAME1,NAME2) as NAME3 from @table
/*
NAME3
-----
张三
李四
王五
*/
#3
create table tb(name1 nvarchar(10),name2 nvarchar(10),name3 nvarchar(10))
insert into tb select '张三',NULL,NULL
insert into tb select '李四',NULL,NULL
insert into tb select NULL,'王五',NULL
go
update tb set name3=(case when name1 is null then name2 else name1 end)
select * from tb
go
drop table tb
/*
name1 name2 name3
---------- ---------- ----------
张三 NULL 张三
李四 NULL 李四
NULL 王五 王五
(3 行受影响)
*/
#4
select isnull(name1,name2) from tb
#5
select isnull(NAME1 ,NAME2) name from tb
select name1 name from tb where name1 is not null
union all
select name2 name from tb where name2 is not null
#6
select isnull(name1,name2) from tb
#7
select isnull(name1,name2) as name3 from tb
#8
[code=SQ]
use tempdb;
/*
create table test
(
name1 nvarchar(10),
name2 nvarchar(10)
);
insert into test(name1,name2)
values
('张三',NULL),('李四',NULL),(NULL,'王五');
*/
select ISNULL(name1,name2) as name3 from test;
[/code]
use tempdb;
/*
create table test
(
name1 nvarchar(10),
name2 nvarchar(10)
);
insert into test(name1,name2)
values
('张三',NULL),('李四',NULL),(NULL,'王五');
*/
select ISNULL(name1,name2) as name3 from test;
[/code]
#9
select isnull(name1,name2) from tb
#10
[code=SQ]UNION ALL
[/code]
[/code]
#11
[code=SQ]UNION ALL [/code]
#12
[code=SQ]UNION ALL [/code]
#13
上面的都很简单
我用另外一种方法处理一下
[code=SQ]declare @table table (NAME1 varchar(4),NAME2 varchar(4))
insert into @table
select '张三',null union all
select '李四',null union all
select null,'王五'
select case when name1 is null then name2 else name1 end name3 from @table[/code]
我用另外一种方法处理一下
[code=SQ]declare @table table (NAME1 varchar(4),NAME2 varchar(4))
insert into @table
select '张三',null union all
select '李四',null union all
select null,'王五'
select case when name1 is null then name2 else name1 end name3 from @table[/code]
#14
[code=SQ]
SELECT NAME1+NAME2 AS 'NAME3' FROM TB1
[/code]
SELECT NAME1+NAME2 AS 'NAME3' FROM TB1
[/code]
#15
declare @table table (NAME1 varchar(4),NAME2 varchar(4))
insert into @table
select '张三',null union all
select '李四',null union all
select null,'王五'
SELECT NAME3 = ISNULL(NAME1, '') + ISNULL(NAME2, '') FROM @table
#16
假如两列都没有NULL的
#17
都为空的时候
你也可以把NAME1 和 NAME2 交换一下,决定首选那个数据
select isnull(isnull(NAME1,NAME2),'未命名') as NAME3 from tablename
你也可以把NAME1 和 NAME2 交换一下,决定首选那个数据
#18
还是union all保险
select name1 name from tbname where name1 is not null
union all
select name2 name from tbname where name2 is not null