有字段
name1 varchar,
name2 varchar,
name3 varchar
现在我新建一个视图,
想将name1与name2的内容合到一起作为新视图的一个字段(如:name4)与name3 一起输出,请教sql语句如何写?
6 个解决方案
#1
select name1+name2 as name4,name3 from tablename
#2
create view viewname
as
select name1+name2 as name4,name3 from tablename
go
as
select name1+name2 as name4,name3 from tablename
go
#3
create view viewname
as
select name3,name4=name1+name2 from tablename
go
as
select name3,name4=name1+name2 from tablename
go
#4
--如果要考虑全面一点,最好用:
create view viewname
as
select name3,name4=isnull(name1,'')+isnull(name2,'') from tablename
go
create view viewname
as
select name3,name4=isnull(name1,'')+isnull(name2,'') from tablename
go
#5
因为NULL值与任何值的计算结果均为null,所以,如果用第一种办法.
当name1,name2任意一个为NULL时,就得不到正确的结果.
第二种方法就可以避免这个问题.
当name1,name2任意一个为NULL时,就得不到正确的结果.
第二种方法就可以避免这个问题.
#6
ok,给分
#1
select name1+name2 as name4,name3 from tablename
#2
create view viewname
as
select name1+name2 as name4,name3 from tablename
go
as
select name1+name2 as name4,name3 from tablename
go
#3
create view viewname
as
select name3,name4=name1+name2 from tablename
go
as
select name3,name4=name1+name2 from tablename
go
#4
--如果要考虑全面一点,最好用:
create view viewname
as
select name3,name4=isnull(name1,'')+isnull(name2,'') from tablename
go
create view viewname
as
select name3,name4=isnull(name1,'')+isnull(name2,'') from tablename
go
#5
因为NULL值与任何值的计算结果均为null,所以,如果用第一种办法.
当name1,name2任意一个为NULL时,就得不到正确的结果.
第二种方法就可以避免这个问题.
当name1,name2任意一个为NULL时,就得不到正确的结果.
第二种方法就可以避免这个问题.
#6
ok,给分