ISNULL -toiminto SQL Server - SQL Server -oppaat (2024)

Tässä SQL Server -opetusohjelmassa opitISNULL -toiminto SQL -palvelimessa, jolloin voimme käsitellä taulukomme nolla -arvoja.

Koska NULL -arvojen käsittely on erittäin tärkeä osa tietojen analysointia ja eheyttä, ensin sinut esitellään"Mikä on NYNULL () -toiminto SQL -palvelimessa?"syntaksinsa kanssa.

Esimerkin avulla näet kuinka se käsittelee nolla -arvoja tai lausekkeita, jotka sisältävät nolla -arvoja.

Lisäksi ymmärrät, kuinka muuttuja siirretäänIsNull () -toimintoNULL -arvojen käsitteleminen.Lopuksi käytätIsNull () -toimintopöydällä.

Sisällysluettelo

ISNULL -toiminto SQL -palvelimessa

SQL -palvelimen ISNULL -toiminto korvaa taulukon sarakkeen nolla -arvon määritellyllä arvolla.Tietokantataulukko sisältää joitain puuttuvia tai tuntemattomia arvoja, joita NULL -arvot edustavat.

Kun tuotteita tuotetaan päätöksen tekemistä varten, jos taulukosi sisältää nolla -arvoja, se voi tuottaa väärän tuloksen, mikä voi johtaa huonoon päätökseen.Joten ennen operaation tekemistä sinun on varmistettava, että tämä ei -toivottu arvo ei ole pöydässäsi.

Näiden nolla -arvojen käsittelemiseksi ISNULL () -toiminnolla, joka korvaa uuden arvon taulukon tai lausekkeen nolla -arvojen sijasta.

Syntaksi on annettu alla.

IsNull (check_expression, korvaava_value)

Missä,

  • IsNull ():Tämä täydellinen toiminto korvaa nolla -arvon uudella määritetyllä arvolla.
  • check_expression:Tämä on lauseke, jonka siirrät funktiolle tarkistaaksesi, onko annettu lausekkeen arvo nolla.Se voi olla sarake- tai NUL -arvot.
  • korvaava_arvo:Se on arvo, joka korvataan nolla -arvon paikassa.Jos annettu lausekkeen arvo on nolla -arvo, korvaava arvo korvaa kyseisen nolla -arvon.Korvaavan arvon tietotyypin tulisi vastata Check_Expression -arvon tietoja.

Aloitetaan esimerkillä: Oletetaan, että sinun on korvattavaTYHJÄarvo kokonaislukuarvolla25;Käytä sitä alla olevaa kyselyä.

Valitse NULLESTILACESSION ISNULL (NULL, '25 ');
ISNULL -toiminto SQL Server - SQL Server -oppaat (1)

Yllä olevan kyselyn ulostulosta THän nolla -arvo korvataan kokonaislukuarvolla 25, kuten voit nähdä tulosta.

Tässä,check_expressionon nolla -arvo, ensimmäinen arvo IsNull () -funktiossa ja toinen arvo onkorvaava_arvo, mikä on tässä tapauksessa 25.Joten isNull () löysi ensimmäisencheck_expressionNULL -arvona, nolla -arvo korvaa 25.

Muuttujan siirtäminen NYNULL -toimintoon SQL -palvelimessa

Voit siirtää muuttujan check_expression -toimintoon ().

Ilmoita esimerkiksi muuttuja@arvotyyppinenVarchar (30)ja määritä aTYHJÄarvo sille, kuten alla on esitetty.

DECLARE @Value varchar (30) = NULL;

Siirrä nyt muuttuja@arvosiihenIsNull ()toimia jkn kanssakorvaava_arvoyhtä kuin'NULL -arvojen korvaaminen'kuten alla.

Valitse ISNULL (@arvo, 'NULL -arvojen korvaaminen') nollaplacement;
ISNULL -toiminto SQL Server - SQL Server -oppaat (2)

Yllä olevan kyselyn ulostulosta muuttujan nolla -arvo@arvokorvataan uudella arvolla'NULL -arvojen korvaaminen'että näet tulossa.

Tässä nolla -arvo on@Varitable;Sitten tämä muuttuja siirretäänIsNull ()varmistaa, sisältääkö se nolla -arvon.Seurauksena on, että ISNULL () -toiminto löytää nolla -arvon korvaamalla sen arvolla 'NULL -arvojen korvaaminen'.

Näin siirretään muuttuja SQL -palvelimen ISNULL -toimintoon.

ISNULL -toiminnon käyttäminen taulukon SQL -palvelimessa

Katsotaanpa, kuinka SQL -palvelimen SQL -palvelimen ISNULL -toimintoa käytetään taulukon sarakkeissa, kun olet usein vuorovaikutuksessa taulukoiden kanssa.

Katso esimerkiksi alla olevaa työntekijöiden taulukkoa.

ISNULL -toiminto SQL Server - SQL Server -oppaat (3)

Yllä olevan taulukon avulla sinun on laskettava työntekijän taulukon keskimääräinen palkka.Silti, kuten näette, palkkapylväs sisältää nolliarvoja, mikä johtaa liian alhaiseen keskimääräiseen palkkaan, joten tämän päivän () toiminnon ratkaisemiseksi nolla -arvo korvaa 0.

Käytä sitä alla olevaa kyselyä.

SELECTDEPLOTIDID, AVG (ISNULL (palkka, 0)) keskiarvoina DepartleID: n työntekijöiden ryhmä;
ISNULL -toiminto SQL Server - SQL Server -oppaat (4)

Yllä olevan kyselyn suorittamisen jälkeen tulosjoukko sisältää työntekijöiden keskimääräisen palkan.Esimerkiksi,Laitoksen tunnuksen keskimääräinen palkka on 21000.

Oletetaan, että lasket työntekijän keskimääräisen palkan käyttämättä ISNULL () -toimintoa.Tällöin saatat löytää erilaisia ​​keskimääräisiä palkkoja työntekijöille, jotka voivat olla väärin nolla -arvojen takia.

Täällä, kyselyosassaAvg(ISNULL (palkka, 0)), ISNULL () -toiminto korvaa kaikki palkkapylvään nollasarjat 0: lla.

Tämä on vain yksi esimerkki SQL -palvelimen ISNULL -toiminnon käytöstä, jota voidaan käsitellä taulukon tietojen yhdistämisessä.

Johtopäätös

Tässä SQL -palvelimen opetusohjelmassa käytitISNULL -toiminto SQL -palvelimessaNULL -arvon korvaaminen määritetyllä arvolla ja muuttujan nolla -arvolla esimerkin avulla.

Loppujen lopuksi olet soveltanut taulukon sarakkeen Inull () -toimintoa korvataksesi nolla -arvon 0: lla, jossa olet laskenut työntekijöiden keskimääräisen palkan.Null -arvojen käsitteleminen on erittäin välttämätöntä.Muuten se voi johtaa vääriin tuloksiin tai tietoihin.

Haluat ehkä lukea:

  • ISDATE -toiminto SQL -palvelimessa
  • Korvaa toiminto SQL -palvelimessa
  • Leikkaustoiminto SQL -palvelimessa

ISNULL -toiminto SQL Server - SQL Server -oppaat (5)

Vijay Kumar Sahu

Olen työskennellyt yli 15 vuotta ohjelmistokentällä, etenkin Microsoft Technologiesissa, olen päättänyt jakaa asiantuntijatietoni SQL -palvelimesta.Katso kaikki SQL -palvelin ja siihen liittyvät tietokanta -opetusohjelmat, jotka olen jakanut täällä.Suurin osa lukijoista on kotoisin maista, kuten Yhdysvallat, Yhdistynyt kuningaskunta, Uusi -Seelanti, Australia, Kanada jne. Olen myösMicrosoft MVP.Katso lisäätässä.

ISNULL -toiminto SQL Server - SQL Server -oppaat (2024)

FAQs

How to check isnull in SQL? ›

SQL Server ISNULL() Function

The ISNULL() function returns a specified value if the expression is NULL. If the expression is NOT NULL, this function returns the expression.

WHERE isnull in SQL Server? ›

WHERE IS NULL tests if a column has a NULL value. NULL is a special value that signifies unknown or no value. Testing for NULL with the = operator is not possible.

What is the difference between Isnull and Isnull in SQL Server? ›

The ISNULL and IS NULL functions are both different in SQL Server. We use the ISNULL function when we have a need to replace the NULL values with a specified value. On the other hand, we use the IS NULL function when we want to identify NULL values in a table.

How to check NULL record in SQL Server? ›

To determine whether an expression is NULL, use IS NULL or IS NOT NULL instead of comparison operators (such as = or !=). Comparison operators return UNKNOWN when either or both arguments are NULL.

How do I check if a SQL Server string is NULL or empty? ›

In standard SQL, you can use the IS NULL predicate to check if a value is NULL . To check for empty strings, you can use the IS NULL predicate along with the LENGTH function (or its equivalent for your database system) to check if the length of the string is zero.

How to use ifnull in SQL Server? ›

The SQL IFNULL() function takes two arguments. The first one is the expression, which checks if the value of the expression is NULL. If so, the function returns the second argument i.e. the alternate_value. On the other hand, if the value of the expression is NOT NULL, then the same value is returned.

What can I use instead of isnull in SQL? ›

NVL2. NVL2 works similar to ISNULL() but instead of checking and replacing one pair, NVL2 can check and replace two pairs of such cases in a single statement. Oracle NVL2 function is replaced by Case Statement in SQL Server. The NVL2 function evaluates the first expression (expr1, expr2, expr3).

Is NULL not working in SQL Server? ›

NULL can be assigned, but using ' = NULL ', ' <> NULL ', or any other comparison operator, in an expression with NULL as a value, is illegal in SQL and should trigger an error. It can never be correct. The expression will always return NULL .

How to check if any column has a null value in SQL? ›

3. ISNULL() Function. The function ISNULL() returns all records with 'NULL' values and records having Empty value (”) or Spaces (' ') in the specified column.

What is the use of Isnull operator in SQL? ›

The IS NULL operator is used to test for empty values (NULL values).

Is NULL NULL true in SQL Server? ›

In SQL server if you have nullParam=NULL in a where clause, it always evaluates to false.

How to display NULL values in SQL Server? ›

NULL can be inserted in any column by using the assignment operator "=". Also, to test whether a NULL is present or not we have to use certain operators, for example, IS NULL and IS NOT NULL. NULL value in SQL signifies that the column's corresponding value is either undefined or unknown.

How to handle NULL in SQL Server? ›

In SQL Server, ISNULL() function is used to replace NULL values. FROM table_name; Example: Consider the following Employee table, Find the sum of the salary of all Employees, if the Salary of any employee is not available (or NULL value), use salary as 10000.

How to check null value in SQL if condition? ›

Description. The IS NULL condition is used in SQL to test for a NULL value. It returns TRUE if a NULL value is found, otherwise it returns FALSE. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.

How to check NULL condition in SQL SELECT query? ›

How to Test for NULL Values? It is not possible to test for NULL values with comparison operators, such as =, <, or <>. We will have to use the IS NULL and IS NOT NULL operators instead.

How to check NULL in MySQL SQL? ›

To determine if a column is empty or null in MySQL, we utilize the IS NULL and IS NOT NULL conditions in a SELECT statement. To ascertain if a column is empty or null in SQL, use COALESCE(column, ”) or column IS NULL OR column = ”.

How do you check is not null or empty in SQL? ›

In addition to the IS NULL operator, SQL also provides the IS NOT NULL operator, which is used to test for non-NULL values in a column. SELECT * FROM table WHERE column1 IS NOT NULL; As a result, you would see the rows that are not empty or do not have NULL values.

How to check if date is NULL in SQL query? ›

SQL Server ISDATE() Function

The ISDATE() function checks an expression and returns 1 if it is a valid date, otherwise 0.

Top Articles
Latest Posts
Article information

Author: Arielle Torp

Last Updated:

Views: 6302

Rating: 4 / 5 (41 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Arielle Torp

Birthday: 1997-09-20

Address: 87313 Erdman Vista, North Dustinborough, WA 37563

Phone: +97216742823598

Job: Central Technology Officer

Hobby: Taekwondo, Macrame, Foreign language learning, Kite flying, Cooking, Skiing, Computer programming

Introduction: My name is Arielle Torp, I am a comfortable, kind, zealous, lovely, jolly, colorful, adventurous person who loves writing and wants to share my knowledge and understanding with you.