如果查询的结果某个字段是Null,用默认的值代替。
1、)Sql server中:
select case when 字段名1 is null
then 替代值
else 字段名1
end +
case when 字段名2 is null
then 替代值
else 字段名2
end as 显示字段名
from 表名
注:字段名是表中的列。
经常如果某个字段是“”时(空格),也可以用某个字段代替,例如customer表:
执行下列语句:
select custid,shipline1,shipline2, name ,
case when Status =' ' then '0' else Status end as Status from customer
得到(如果status为’ ’,则让它视图显示“0”):
执行进一步更复杂的要求,如果name为’my’显示‘a’,为’yo’显示‘b’,其他显示’c’,sql语句如下:
select
custid,shipline1,shipline2,
case when name='my' then 'a'
when name='yo' then 'b'
else 'c' end as name ,
case when Status ='' then '0' else Status end
as Status from customer order by custid asc
注:(附建表语句)
create table AppDta.dbo.customer(
custid int Not Null check(custid>0),
name char(30) Not Null check(name<>''),
shipline1 varchar(100) Not Null Default '',
shipline2 varchar(100) Not Null Default '',
Status char(1) Not Null Default '',
CreditLimit Money Not Null check((CreditLimit Is NUll) or (CreditLimit>=0)));
2、)oracle中:
下面用一个常见的数据显示来说明decode函数的用法。就是成绩单的显示。我想做开发的人员都遇到过这个,而且在大学期间也是常常接触成绩单,显示的是:姓名、语文、数学等。 实现脚本如下(cjd.sql):
--建表
create table stud
(
sid varchar2(10),
kcbm varchar2(10),
cj int
);
--插入测试数据
insert into stud values(''''1'''',''''语文'''',80);
insert into stud values(''''2'''',''''数学'''',90);
insert into stud values(''''3'''',''''英语'''',100);
commit;
--创建视图,decode用法
create or replace view cjd as
select sid,
decode(kcbm,''''语文'''',cj,0) 语文,
decode(kcbm,''''数学'''',cj,0) 数学,
decode(kcbm,''''英语'''',cj,0) 英语
from stud
order by sid;
--显示数据
select * from cjd;
执行过程如下:
SQL> create table stud(sid varchar2(10),
2 kcbm varchar2(10),
3 cj int);
表已创建。
WS$R=业@网VgoX育Yb网IlU
SQL> insert into stud values(''''1'''',''''语文'''',80);
已创建 1 行。
SQL> insert into stud values(''''2'''',''''数学'''',90);
已创建 1 行。
SQL> insert into stud values(''''3'''',''''英语'''',100);
已创建 1 行。
SQL> commit;
提交完成。
SQL> create or replace view cjd as
2 select sid,
3 decode(kcbm,''''语文'''',cj,0) 语文,
4 decode(kcbm,''''数学'''',cj,0) 数学,
5 decode(kcbm,''''英语'''',cj,0) 英语
6 from stud
7 order by sid;
视图已建立。
SQL> select * from cjd;
SID 语文 数学 英语
---------- ---------- ---------- ----------
1 80 0 0
2 0 90 0
3 0 0 100
如果查询的结果某个字段是Null,用默认的值代替。1、)Sql server中:select case when 字段名1 is null then 替代值 else 字段名1 end + ...
2.1、NVL函数功能介绍
NVL 是Oracle的函数,功能是实现空
值
的转换,如果fieldName为
NULL
,则NVL函数返回replace_with的
值
,否则返回string1的
值
。
2.2、NVL函数格式
//fieldName表示
字段
名称,replace_with表示该
字段
内容为空
时
的替换
值
NVL(fieldName, replace_with)
引申一下,此NVL的作用与
SQL
...
SQL
的
null
查询
、is
null
函数、
case
when then问题和net页面设置问题
1.怎样在
一个
表中
查询
指定
字段
值
为
null
的记录?
最直接想法:
select
* from 表名 where
字段
=
null
测试结果:没有报错,但什么也没有显示出来。
正确答案:
select
* from 表名 where
字段
is
null
2.
SQL
的IS
NULL
问题:
使用IS
NULL
函数,可以使用指定的
值
替换
null
语法如下:IS
NULL
( check _ expression , replac
sql
中的if()和if
null
() 的用法和区别
my
sql
coalesce函数与 if
null
函数用法
1.f
null
(
字段
,
默认值
) :
字段
如果是
null
就返回
默认值
# f
null
(
null
, a),if
null
(a,b), if
null
里有两个数,如果第
一个
不是
null
,是a非
null
,就都等于a,
# 如果a=
Null
,就都为a。
SELECT
IF
NULL
(
NULL
,"11"); -> 11
SELECT
IF
NULL
("00
我们在
查询
My
Sql
数据库
时
,如果使用比较复杂的
查询
方式,
查询
结果有
时
候会为空(
NULL
)。
大多数
时
候,我们会希望他有
一个
默认值
。
对于这个
默认值
,一种办法是
查询
完成后用编程语言给他赋
默认值
。
然后My
sql
给我们提供了
一个
更简单的办法 :IF
NULL
函数
SELECT
IF
NULL
(a,0) FROM table WHERE 1;
这个
sql
语句 为
查询
结果a...
在oracle、ms
sql
、my
sql
中,判断是否为空,为空则替换为其他字符的函数不一样,这里分别列举一下
功能为:如果第
一个
value为
null
,则返回第二个value
// my
sql
select
if
null
(
null
,'my
sql
')
select
if
null
(c_name,'没有名字')
// ms
sql
select
is
null
(
null
,'ms
sql
')
select
is
null
(c_name,'没有名字')
// oracle
select
nvl(
null
,'oracle
另外if
null
(usenum,0)函数间有空格也会产生这个问题。
原文链接:https://blog.csdn.net/weixin_43591947/article/details/103287983
在编程过程中常会遇到将
数据库
中读取到的
null
值
替换为我们想要的
值
得情况,现将我们常见的
数据库
null
值
转换函数总结如下:
一、
SQL
Server
中
null
值
替换
方法:IS
NULL
()
语法:IS
NULL
( check_expression , replacement_value )
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.MoveTask
茶色...:
Linux 重启后重新Mount磁盘
hehui0921:
M个苹果放在N个盘子里,有多少种不同的放法
qq_28069031:
JPA QuerySyntaxException:XXX is not mapped
Rhine_danji_ys:
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.MoveTask
Randolph605: