sqlnull替换
A. Mysql中查询一个表,把结果中的NULL替换成0,请写出sql语句
1、MSSQL: ISNULL()
语法
ISNULL ( check_expression , replacement_value )
参数
check_expression
将被检查是否为 NULL的表达式。check_expression 可以是任何类型的。
replacement_value
在 check_expression 为 NULL时将返回的表达式。replacement_value 必须与 check_expresssion 具有相同的类型。
返回类型
返回与 check_expression 相同的类型。
注释
如果 check_expression 不为 NULL,那么返回该表达式的值;否则返回 replacement_value。
2、Oracle: NVL()
语法
NVL(eExpression1, eExpression2)
参数
eExpression1, eExpression2
如果 eExpression1 的计算结果为 null 值,则 NVL() 返回 eExpression2。如果 eExpression1 的计算结果不是 null 值,则返回 eExpression1。eExpression1 和 eExpression2 可以是任意一种数据类型。如果 eExpression1 与 eExpression2 的结果皆为 null 值,则 NVL( ) 返回 NULL
B. sql 查询时,把表中的null替换为“未知” 但下面不对啊,
晕,如果只是在查询结果中替换一下方便阅读,你把“=”换成 "IS" 看看,即 select name,(case when class is null then '未知' else class end) as class from student
如果是要替换数据库中的字段值,替换要用UPDATE 的哈,即update student set class='未知' where class is null就可以了的。。然后如果你要查看替换结果,可以再select name,class from student.
C. sql替换null值
这个一般无法做到,
比如,数字、日期类型的数据,也可以存成 null, 但是却无法存成 空字符串 ''
再比如Oracle,一般来说, 空字符串 '' 在数据库中 都存成 null
当然,你要是只改 字符 列,
可以,循环取出 所有表的所有列,然后 用动态sql执行:
update tab_a set col_x = '' where col_x is null;
D. mysql怎么把null替换成0
代码如下:
select ifnull(colname,0) from tablename;
可以用case when解决:
selectcasewhen字段isnullthen0else字段endfrom表名
E. sql怎样将null赋值为0
在sqlserver中可以有几种方法:
---方法1:使用isnull替换
select keyId,isnull(info,0) as info from test
---方法2:使用case when 替换
select keyId,case when info is null then 0 else info end as info from test
---方法3:使用coalesce替换相应的值
select keyId , coalesce(info,0) as info from test
F. sql如何把查询到的NULL替换成空值
1、这要看你如何保存你查询的结果。只能是你把你查询的结果保存为0,查询不会改变原本存在的值。表名test,字段a=.null.(int型),字段b=1,字段c=2 :select * from test into tabel test1
update set a=0 where a=.null。
2、用 IsNull(字段名, '') 可以将NULL的字段转换为空值,这在多个字段连接时很有用,因为NULL值+任何字段都是NULL。
3、将NULL替换为空create procere fill_null@tablename varchar(100) --表名asdeclare @colname varchar(100)declare col_cur cursor for select c.name from syscolumns c,sysobjects o where c.id=o.id and o.name=@tablename open col_curfetch next from col_cur into @colnamewhile @@fetch_status!=-1beginexec ('update '+@tablename+' set '+@colname+'='''' where '+@colname+' is null' )fetch next from col_cur into @colnam endclose col_curdeallocate col_cur
G. sql如何空值替换成null
各个数据库都有空值操作函数,例如Oracle的nvl,mysql的ifnull,sqlserver的isnull等
都可以把空值替换成另外一个内容,你这里只需要把空值替换“null字符”就可以了。
oracle:select nvl(字段,'NULL') from ****
mysql:select ifnull(字段,''NULL'') from ****
sqlserver,也类似,我就不写了
H. 怎样将sql数据库字段中的NULL都替换为空
大概想法是通过利用sys.columns这个系统表,然后组合语句之后执行。
declare @cmd varchar(MAX)declare @column varchar(MAX)declare @index intwhile 1 = 1 select top 1 @column = name, @index = column_id from sys.columns where column_id > @index and object_name(object_id) = 'Table_name'if @column is null breakselect @com = 'update Table_name set ' + @column + ' = '''' where ' + @column + ' is null'exec(@cmd)end
I. 这句sql里的null是什么意思
你好,向你讲解一下SQL中null空值:
在
SQL
语句中,
NULL
值与字符列中的空格,
数字中的零,
字符列中的
NULL
ASCII
字符都不相同。
在sql中null是一种数据类型,null不能与任何列或者变量使用"="或者"!="去比较,判断某列或者变量为null是只能用
is
(not)
null
去判断这样他的返回值才是true或者false。
一、关于null的运用
1、NULL值与索引
如果一个列中有NULL值,那么不可以在这个列上建唯一索引,可以建立非唯一索引;但是如果一个字段有很多行有NULL值,那么在这个字段上建索引效果不佳。所以建议不在在一个频繁出现NULL值的字段上建索引。
2、NULL与排序
NULL参与排序时总是作为最小值存在,即ORDER
BY
COL
ASC时COL为NULL的行在最前面,反之在最后面。
二、IsNull的用法:使用指定的替换值替换
NULL。
语法
ISNULL
(
check_expression
,
replacement_value
)
参数
check_expression
将被检查是否为
NULL的表达式。check_expression
可以是任何类型的。
replacement_value
在
check_expression
为
NULL时将返回的表达式。replacement_value
必须与
check_expresssion
具有相同的类型。
返回类型
返回与
check_expression
相同的类型。
如果文字
NULL
作为
check_expression
提供,则返回
replacement_value
的数据类型。
如果文字
NULL
作为
check_expression
提供并且未提供
replacement_value,则返回
int。
注释
如果
check_expression
不为
NULL,则返回它的值;否则,在将
replacement_value
隐式转换为
check_expression
的类型(如果这两个类型不同)后,则返回前者。
如果
replacement_value
比
check_expression
长,则可以截断
replacement_value。
注意:
请勿使用
ISNULL
查找
NULL
值。
而应使用
IS
NULL。
参考资料:
http://www.studyofnet.com/news/111.html
希望以上的回答对你有帮助!
J. SQL,怎样可以一次性将表中的所有NULL替换成空
大概想法是通过利用sys.columns这个系统表,然后组合语句之后执行。
declare@cmdvarchar(MAX)
declare@columnvarchar(MAX)
declare@indexint
while1=1
selecttop1@column=name,@index=column_idfromsys.columnswherecolumn_id>@indexandobject_name(object_id)='Table_name'
if@columnisnull
break
select@com='updateTable_nameset'+@column+'=''''where'+@column+'isnull'
exec(@cmd)
end
里面的Table_name就是你的表名