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就是你的表名