Your cart is currently empty!
Author: alien
-
Khóa học miễn phí SQL – Null Functions nhận dự án làm có lương
SQL – Null Functions
Table of content
SQL NULL functions are used to perform operations on NULL values that are stored in the database tables.
A NULL value serves as a placeholder in the database when data is absent or the required information is unavailable. It is a flexible value not associated to any specific data type and can be used in columns of various data types, including string, int, varchar, and more.
Following are the various features of a NULL value −
-
The NULL value is different from a zero value or a field containing a space. A record with a NULL value is one that has been left empty or unspecified during record creation.
-
The NULL value assists us in removing ambiguity from data. Thus, maintaining the uniform datatype across the column.
SQL NULL Functions
To handle these NULL values in a database table, SQL provides various NULL functions. They are listed as follows −
- ISNULL()
- COALESCE()
- NULLIF()
- IFNULL()
The ISNULL() Function
The SQL ISNULL() function returns 0 and 1 depending on whether the expression is null or not. If the expression is null, then this function returns 1; otherwise, it returns 0.
Syntax
Following is the syntax for the ISNULL() function −
ISNULL(column_name)
Example
First of all let us create a table named CUSTOMERS, containing the personal details of customers including their name, age, address and salary etc., using the following query −
CREATE TABLE CUSTOMERS ( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
Now, insert records into this table using the INSERT INTO statement as follows −
INSERT INTO CUSTOMERS VALUES (1, ''Ramesh'', 32, ''Ahmedabad'', 2000.00 ), (2, ''Khilan'', 25, ''Delhi'', 1500.00 ), (3, ''Kaushik'', 23, ''Kota'', NULL ), (4, ''Chaitali'', 25, ''Mumbai'', 6500.00 ), (5, ''Hardik'', 27, ''Bhopal'', 8500.00 ), (6, ''Komal'', 22, ''Hyderabad'', NULL ), (7, ''Indore'', 24, ''Indore'', 10000.00 );
The table will be created as −
ID NAME AGE ADDRESS SALARY 1 Ramesh 32 Ahmedabad 2000.00 2 Khilan 25 Delhi 1500.00 3 Kaushik 23 Kota NULL 4 Chaitali 25 Mumbai 6500.00 5 Hardik 27 Bhopal 8500.00 6 Komal 22 Hyderabad NULL 7 Indore 24 Indore 10000.00 Following is the query to check whether SALARY is NULL or not −
SELECT SALARY, ISNULL(SALARY) AS Null_value FROM CUSTOMERS;
Output
On execution of the above query, we get the column “SALARY” and Null_value. If the SALARY is NULL, then their null value is 1; otherwise, it is 0. −
SALARY Null_value 2000.00 0 1500.00 0 NULL 1 6500.00 0 8500.00 0 NULL 1 10000.00 0 The COALESCE() Function
The SQL COALESCE() function returns the first occurred NON-NULL expression among its arguments. If all the expressions are NULL, then the COALESCE() function will return NULL.
An integer is evaluated first in the COALESCE() function, and an integer followed by a character expression always produces an integer as the output.
Syntax
Following is the syntax for the COALESCE() function −
COALESCE(expression_1, expression_2, expression_n);
Example
In the following query, we are returning the first occurred NON-NULL value −
SELECT COALESCE (NULL, ''welcome'', ''tutorialspoint'') AS Result;
Output
On executing the above query, we get “welcome” as a result, because it is the first NON-NULL value −
Result welcome Example
In the following query, we are using the COALESCE() function on the SALARY and AGE columns of CUSTOMERS table. The first NON-NULL values evaluated from these two columns are displayed in another column named “Result”.
SELECT NAME, SALARY, AGE, COALESCE(SALARY, AGE) AS Result FROM CUSTOMERS;
Output
When you execute the above query, we get the following table as a result −
NAME SALARY AGE Result Ramesh 2000.00 32 2000.00 Khilan 1500.00 25 1500.00 Kaushik NULL 23 23.00 Chaitali 6500.00 25 6500.00 Hardik 8500.00 27 8500.00 Komal NULL 22 22.00 Indore 10000.00 24 10000.00 The NULLIF() Function
The SQL NULLIF() function compares two expressions. If both expressions are the same, it returns NULL. Otherwise, it returns the first expression. This function can be used directly with clauses like SELECT, WHERE, and GROUP BY.
Syntax
Following is the syntax of NULLIF() function −
NULLIF(expression_1, expression_2);
Example
The following SQL query uses NULLIF() function to compare values in NAME and ADDRESS columns of the CUSTOMERS table. If the NAME value matches the ADDRESS value, the result is NULL; otherwise, it returns the NAME value. The result values are stored in another column called “Result”.
SELECT NAME, ADDRESS, NULLIF(NAME, ADDRESS) AS Result FROM CUSTOMERS;
Output
When you execute the above query, we get the following table as a result −
NAME ADDRESS Result Ramesh Ahmedabad Ramesh Khilan Delhi Khilan Kaushik Kota Kaushik Chaitali Mumbai Chaitali Hardik Bhopal Hardik Komal Hyderabad Komal Indore Indore NULL The IFNULL() Function
The IFNULL() function replaces the NULL values in a database table with a specific value. This function accepts two arguments. If the first argument is a NULL value, it is replaced with the second argument. Otherwise, the first argument is returned as it is.
This function does not work in the SQL Server database.
If both the arguments are NULL, the result of this function is also NULL.
Syntax
Following is the syntax for IFNULL() function −
IFNULL(column_name, value_to_replace);
Example
The following query evaluates the values in SALARY column of the CUSTOMERS table. Using the IFNULL() function, we are replacing the NULL values in this column (if any) with the value 5500 −
SELECT NAME, SALARY, IFNULL(SALARY, 5500) AS Result FROM CUSTOMERS;
Output
Following is the output of the above query −
NAME SALARY Result Ramesh 2000.00 2000.00 Khilan 1500.00 1500.00 Kaushik NULL 5500.00 Chaitali 6500.00 6500.00 Hardik 8500.00 8500.00 Komal NULL 5500.00 Indore 10000.00 10000.00
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc
-
Khóa học miễn phí SQL – Hosting nhận dự án làm có lương
SQL – Hosting
SQL Hosting
SQL Hosting is nothing but a means to manage any RDBMS linked to your website using SQL. If the website has an access to a RDBMS, any data from the website you created will be stored and retrieved from this database.
There are various SQL hosting plans available if your web server is hosted by an Internet Service Provider (ISP).
Following are the most common SQL hosting databases −
- MS SQL Server
- Oracle
- MySQL
- MS Access
MS SQL Server
MS SQL Server is created and developed by Microsoft. It is compatible with both virtual and cloud servers. It is very efficient to use with database-driven websites having high traffic.
MS SQL Server”s features include −
- Maximum scalability and security
- Integrated reporting capabilities
- Easy to use
- Powerful
- Robust
- Offers more diverse features while hosting
Oracle
Oracle is a popular database which is suitable to use with high-traffic websites. This database offers various features like,
- Cost-effective product
- High-performance
- Converged, multi-model database management system
- In-memory MySQL databases
Oracle is also a very powerful, robust and full featured SQL database system.
MySQL
MySQL is one of the most popular RDBMS in the world used to store and manage data. It is compatible with any type of server, say cloud, virtual or dedicated. Features of MySQL are as follows −
- Easy to use
- High performance
- Excellent security
- Improved speed
- Cost effective
MS Access
Microsoft Access is a very simple database which can be used for simple websites. MS Access is neither as powerful as MySQL, MS SQL Server or Oracle. Thus, it is not effective for websites with higher traffic.
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc