空值NULL處理

2021-08-29 07:58:24 字數 1657 閱讀 6338

--1.

空值(null)處理 --

查詢籍貫為null同學 --

如果判斷乙個欄位的的值是不是null,需要使用is關鍵字, 不能使用=

select

*from

tbstudent

where

stuaddress

isnull --

查詢籍貫不是null的所有同學

select

*from

tbstudent

where

stuaddress

isnot

null --

注意:not like ,is not --

查詢所有同學資訊,如果籍貫是null,就用』未知『替代

--isnull

(原始資料,代替值)函式

select

stuname

,stunumber

,isnull

(sstuaddress,'

未知')

from

tbstudent --

查詢所有數學成績為null的學生

select

*from

tbstudent

where

stumath

isnull --

查詢所有學生的考試成績,如果為null,則用』缺考『代替

select

stuname

,stunumber

,isnull

(stumath,'

缺考')

from

tbstudent

--stumath

是int型別,』缺考『是varchar型別的,所以不允許填充到同一列上 --

資料表中任何一列,都只能儲存一種資料型別的數

select

stuname

,stunumber

,stumath

from

tbstudent

--sql

裡的null和c#裡的null是不一樣的

--c#

裡的null:表示乙個引用型別的變數,不指向堆上的任何物件

--sql

裡的null:unknown,不知道。sql裡的任意資料型別的字段,都可以賦值null

--sql

裡null與任何資料做運算,結果還是null --

查詢所有的學生的數學成績,並且給所有人的學習成績+10分

select

stuname

,stunumber

,stumath

+10 as

'數學'

from

tbstudent --

為啥不讓用 = null,只能用 is null --

查詢所有學生的考試成績,如果為null,則用』缺考『代替

select

stuname

,stunumber

,isnull

(cast

(stumath

asvarchar

(4)),

'缺考'

)from

tbstudent --

注意:這個isnull()函式,不會修改原表中的資料,它只是把查詢結果集中的 --

顯式方式變了變

處理Null(空)值

如果將null設定給物件的屬性,程式會報錯。例如 如果myblog.settitle null 程式會報錯。如果引數傳了乙個空值,那麼jdbc type對於所有的jdbc允許為空的列來說是必須指定的。解決方法 在引數中指定jdbctype屬性,這個屬性只在insert,update,delete的時...

hive 空值 null判斷

hive中空值判斷基本分兩種 1 null 與 n hive在底層資料中 如何儲存和標識null,是由 alter table name set serdeproperties serialization.null.format n 引數控制的 比如 1.設定 alter table name se...

hive 空值 NULL判斷和處理方式

首先需要明白乙個問題,空值和null值不是等價的。首先看乙個表結構的例子 由此可見,hive中空值判斷基本分兩種 hive在底層資料中如何儲存和標識null,是由alter table name set serdeproperties serialization.null.format n 引數控制...