1.空值與null的區(qū)別
????????null沒(méi)有被分配任何值或?qū)ο?,表示這個(gè)字段沒(méi)有被賦值或者值是未知的,占空間,不會(huì)被count()函數(shù)統(tǒng)計(jì);
????????空值表示這個(gè)字段被賦了一個(gè)空的值,不占空間,會(huì)被count()函數(shù)統(tǒng)計(jì)。
2.空值與null的判斷
????????null和''(空值)在SQL中的篩選過(guò)濾條件是不一樣的,is null 識(shí)別不了 '',同樣地,'' 也識(shí)別不了 null 。
2.1 null判斷
--篩選null
select * from table where a is null
--篩選非null
select * from table where a is not null
2.2 空值判斷
--篩選空值
select * from table where a = ''
--篩選非空值
select * from table where a <> ''
--或者
--篩選空值
select * from table where length(a) = 0
--篩選非空值
select * from table where length(a) <> 0
2.3 null與空值判斷
--篩選null與空值
select * from table where a is null and a = ''
--篩選非null與非空值
select * from table where a is not null and a <> ''
--或者
--篩選null與空值
select * from table where nvl(a,'') = ''
--篩選非null與非空值
select * from table where nvl(a,'') <> ''
3.空值與null的處理
3.1 null處理
nvl函數(shù)
nvl(expr1,expr2)
例如:nvl(a,0)
如果a字段值為null,那么null轉(zhuǎn)化為0這個(gè)值,如果a字段值不為null,則顯示a本來(lái)的值。
拓展--nvl2(expr1,expr2,expr3)
例如:nvl2(a,0,1)
如果a字段值為null,那么null轉(zhuǎn)化為0這個(gè)值,如果a字段值不為null,則轉(zhuǎn)化為1這個(gè)值。
3.2 空值處理
????????hive本身沒(méi)有replace函數(shù),可以用translate和regexp_replace函數(shù)代替。
translate(expr1,expr2,expr3)
例如:translate(a,'',0)
將a字段的空值替換為0
regexp_replace(expr1,正則表達(dá)式,expr2)
例如:regexp_replace(a,"[\\s]+|[\u3000]+",0)
將a字段的空值替換為1([\\s]+|[\u3000]+是正則)
????????對(duì)于null的處理其實(shí)也可以使用translate和regexp_replace函數(shù)。
translate(a,null,0)
regexp_replace(nvl(a,""),"[\\s]+|[\u3000]+",0)文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-755611.html
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-755611.html
到了這里,關(guān)于【SQL相關(guān)】Hive中空值與Null的判斷及處理的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!