
{"id":12155,"date":"2022-03-29T11:34:07","date_gmt":"2022-03-29T09:34:07","guid":{"rendered":"https:\/\/careerfoundry.inbearbeitung.de\/en\/?p=12155"},"modified":"2022-09-29T15:20:09","modified_gmt":"2022-09-29T13:20:09","slug":"isnull-sql","status":"publish","type":"post","link":"https:\/\/careerfoundry.inbearbeitung.de\/en\/blog\/data-analytics\/isnull-sql\/","title":{"rendered":"Exploring the ISNULL() SQL Function"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">I\u2019ve been working with data professionally for almost a decade, and the first thing I tell all newcomers is to start mastering relational databases and Structured Query Language (SQL). Understanding how to query and manipulate data in databases is an important skill because every company has data and most want to understand it. Learning the basics of SQL is easy, and the skill can land a high-paying job if you\u2019re good at it.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Whether you\u2019re learning SQL to become a database administrator, data analyst or data scientist, it is important to understand how to handle NULL values. In this tutorial we\u2019re going to cover how to use the ISNULL() function in <\/span><a href=\"https:\/\/www.microsoft.com\/en-us\/sql-server\/sql-server-downloads\" rel=\"noopener\"><span style=\"font-weight: 400;\">SQL Server<\/span><\/a><span style=\"font-weight: 400;\">, as well as some tips and tricks to make sure you\u2019re applying ISNULL() correctly.<\/span><\/p>\n<p>You can use this clickable menu to skip ahead to any section you&#8217;re interested in:<\/p>\n<ol>\n<li><a href=\"#getting-started\">Before we get started: creating test data<\/a><\/li>\n<li><a href=\"#what-is-ISNULL\">What is the ISNULL function?<\/a><\/li>\n<li><a href=\"#using-ISNULL\">Using the ISNULL function<\/a><\/li>\n<li><a href=\"#not-using-ISNULL\">When not to use the ISNULL function<\/a><\/li>\n<li><a href=\"#data-type-conversion\">Data type conversion with ISNULL<\/a><\/li>\n<li><a href=\"#wrapping-up\">Wrapping up<\/a><\/li>\n<\/ol>\n<h2 id=\"getting-started\"><span style=\"font-weight: 400;\">1. Before we get started: creating test data<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Before we explore the ISNULL() function, we need some data to play with. I spun up a little database named <\/span><b>Test <\/b><span style=\"font-weight: 400;\">and created a table named <\/span><i><span style=\"font-weight: 400;\">people <\/span><\/i><span style=\"font-weight: 400;\">containing six columns. Then I inserted three records into the table. Use this script to replicate the people table and example records:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">use Test<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CREATE TABLE people<\/span><\/p>\n<p><span style=\"font-weight: 400;\">(personID\u00a0 INT IDENTITY(1, 1) NOT NULL,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">firstName VARCHAR(50) NOT NULL,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">homePhone VARCHAR(15) NULL,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">workPhone VARCHAR(15) NULL,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">cellPhone VARCHAR(15) NULL,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">age INT NULL<\/span><\/p>\n<p><span style=\"font-weight: 400;\">);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">insert into people(firstname, homephone, workphone, cellPhone, age)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">values(&#8216;Eric&#8217;, &#8216;555-555-6655&#8217;, &#8216;555-555-1234&#8217;, null, 34);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">insert into people(firstname, homephone, workphone, cellPhone, age)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">values(&#8216;Tom&#8217;, null, &#8216;555-555-4457&#8217;, null, 43);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">insert into people(firstname, homephone, workphone, cellPhone, age)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">values(&#8216;Lucy&#8217;, &#8216;555-555-7978&#8217;, null, &#8216;555-555-1212&#8217;, null);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">As we can see, the table contains six fields, four of which can contain null values:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">homePhone<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">workPhone<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">cellPhone<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">age<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">We can review the inserted records using a SELECT statement:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">SELECT * FROM people<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12158\" src=\"http:\/\/careerfoundry.inbearbeitung.de\/en\/wp-content\/uploads\/2022\/02\/image1-1.png\" alt=\"ISNULL SQL tutorial\" width=\"453\" height=\"112\" title=\"\" srcset=\"https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-content\/uploads\/2022\/02\/image1-1.png 453w, https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-content\/uploads\/2022\/02\/image1-1-300x74.png 300w\" sizes=\"auto, (max-width: 453px) 100vw, 453px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Notice the three records display NULL for the phone numbers and age that do not exist. Although it says NULL in the results, it is important to understand data is absent from that field. Null is the absence of data, meaning the NULL values are completely empty.\u00a0<\/span><\/p>\n<h2 id=\"what-is-ISNULL\"><span style=\"font-weight: 400;\">2. What is the ISNULL() function?<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Although many consider ISNULL() to be an advanced SQL function, it is very easy to understand and use. Before getting into examples, let&#8217;s take a look at the syntax for the ISNULL() function:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">ISNULL(expression, value)<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The <\/span><b>Expression <\/b><span style=\"font-weight: 400;\">corresponds to the field that will be<\/span> <span style=\"font-weight: 400;\">checked by the function to see if it\u2019s NULL or not. The <\/span><b>Value <\/b><span style=\"font-weight: 400;\">corresponds to the specified value that will be returned if the <\/span><i><span style=\"font-weight: 400;\">expression<\/span><\/i><span style=\"font-weight: 400;\"> is NULL.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">When querying data and using ISNULL(), the function returns a specified <\/span><i><span style=\"font-weight: 400;\">value <\/span><\/i><span style=\"font-weight: 400;\">if the <\/span><i><span style=\"font-weight: 400;\">expression <\/span><\/i><span style=\"font-weight: 400;\">is found to be NULL. If the expression is not NULL, then the expression value is returned. For example, if we look at the cellPhone column in the example data, we see two of the three records show NULL values. If cellPhone is passed into ISNULL() as the <\/span><b>expression<\/b> <span style=\"font-weight: 400;\">and the word \u201cN\/A\u201d is set as the <\/span><b>value<\/b><span style=\"font-weight: 400;\">, the function will return the specified value, \u201cN\/A\u201d for two of the three records.<\/span><\/p>\n<h2 id=\"using-ISNULL\"><span style=\"font-weight: 400;\">3. Using the ISNULL() function<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">The ISNULL() function can be used in multiple situations. The most straightforward way is to use it whenever you want to output a value if a NULL is found. Using the example data, let\u2019s write a SELECT statement that will return <\/span><b>firstName <\/b><span style=\"font-weight: 400;\">and <\/span><b>homePhone <\/b><span style=\"font-weight: 400;\">fields. Using ISNULL(), we can make the query return the text, \u201cNo Home Phone\u201d if the homePhone field is NULL:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">select firstName,<\/span><\/p>\n<p><span style=\"font-weight: 400;\">ISNULL(homePhone, &#8216;No Home Phone&#8217;) as HomePhoneNumber<\/span><\/p>\n<p><span style=\"font-weight: 400;\">from people<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12159\" src=\"http:\/\/careerfoundry.inbearbeitung.de\/en\/wp-content\/uploads\/2022\/02\/image2-1.png\" alt=\"ISNULL SQL tutorial \" width=\"280\" height=\"112\" title=\"\"><\/p>\n<p><span style=\"font-weight: 400;\">We can see in the results, Tom returns the text<\/span><b> No Home Phone<\/b><span style=\"font-weight: 400;\"> since his homePhone value is NULL in the table.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Another way to use the ISNULL() function is to have it return a different column in the data, like a different phone number, instead of the \u201cNo Home Phone\u201d<\/span> <span style=\"font-weight: 400;\">text. For example, this expression returns the workPhone value if the homePhone is NULL:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">select firstName, ISNULL(homePhone, workPhone) as HomeOrWork<\/span><\/p>\n<p><span style=\"font-weight: 400;\">from people<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12160\" src=\"http:\/\/careerfoundry.inbearbeitung.de\/en\/wp-content\/uploads\/2022\/02\/image3-1.png\" alt=\"ISNULL SQL tutorial\" width=\"287\" height=\"115\" title=\"\"><\/p>\n<p><span style=\"font-weight: 400;\">Notice Tom\u2019s workPhone value now outputs because it was specified in the function and the ISNULL <\/span><b>expression<\/b> <span style=\"font-weight: 400;\">is finding a NULL value for his homePhone.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The ISNULL() function can also be used in dynamic SQL or when using declared variables. For example, declared variables can be passed into the function just like we did with column names in the previous example\u2019s SELECT statement.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">DECLARE @expression VARCHAR(50);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">DECLARE @value VARCHAR(50);<\/span><\/p>\n<p><span style=\"font-weight: 400;\">SET @expression = null;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">SET @value = &#8216;This tutorial is great!&#8217;;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">SELECT ISNULL(@expression, @value) AS example;<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12161\" src=\"http:\/\/careerfoundry.inbearbeitung.de\/en\/wp-content\/uploads\/2022\/02\/image4-1.png\" alt=\"ISNULL SQL tutorial\" width=\"200\" height=\"80\" title=\"\"><\/p>\n<p><span style=\"font-weight: 400;\">Notice the declared <\/span><b>value<\/b><span style=\"font-weight: 400;\"> returned since the declared <\/span><i><span style=\"font-weight: 400;\">expression <\/span><\/i><span style=\"font-weight: 400;\">was set to NULL.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">It is also possible to use ISNULL() within aggregate functions like AVG() and SUM(). Let\u2019s say we want the average age of the people in the dataset. We can use ISNULL() to supply a value to the nulls as a means for estimating the average age:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">select avg(ISNULL(age, 30)) as averageAge from people<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12162\" src=\"http:\/\/careerfoundry.inbearbeitung.de\/en\/wp-content\/uploads\/2022\/02\/image5-1.png\" alt=\"ISNULL SQL tutorial\" width=\"168\" height=\"73\" title=\"\"><\/p>\n<h2 id=\"not-using-ISNULL\"><span style=\"font-weight: 400;\">4. When not to use the ISNULL() function<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">While ISNULL() is a powerful function, there are some situations where it is not the best function for the job. Consider we have three phone number fields in the data. If we want to look at all three fields and return the first non-null value, the COALESCE() function should be used instead of the ISNULL() function.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">COALESCE versus ISNULL<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">The COALESCE() function is used to return the first non-null value in a given list. For example, let\u2019s say we want to query all three phone number fields, but only want one value returned. We decide we want to first check the cellPhone field. If the cellPhone value doesn\u2019t exist, we want the homePhone value. If homePhone doesn\u2019t exist, we want workPhone. Using COALESCE(), we can pass that list of phone number columns into the query and get a single non-null phone number back.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Here is an example of using COALESCE():<\/span><\/p>\n<p><span style=\"font-weight: 400;\">select firstName, COALESCE(cellPhone, homePhone, workPhone) as phoneNumber<\/span><\/p>\n<p><span style=\"font-weight: 400;\">from people<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12163\" src=\"http:\/\/careerfoundry.inbearbeitung.de\/en\/wp-content\/uploads\/2022\/02\/image6-1.png\" alt=\"ISNULL SQL tutorial\" width=\"309\" height=\"119\" title=\"\" srcset=\"https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-content\/uploads\/2022\/02\/image6-1.png 309w, https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-content\/uploads\/2022\/02\/image6-1-300x116.png 300w\" sizes=\"auto, (max-width: 309px) 100vw, 309px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">The ISNULL() function is limited to the two parameters, but the COALESCE() function can take a list of many parameters. The COALESCE() functionality can be replicated using the ISNULL() function, but we need to nest ISNULL() within ISNULL(), making the code harder to understand versus using COALESCE(). Nesting the ISNULL function inside itself to get the same results as COALESCE() would look like this:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">select firstname, ISNULL(cellphone, ISNULL(homePhone, workPhone)) from people<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12164\" src=\"http:\/\/careerfoundry.inbearbeitung.de\/en\/wp-content\/uploads\/2022\/02\/image7-1.png\" alt=\"ISNULL SQL tutorial\" width=\"263\" height=\"111\" title=\"\"><\/p>\n<p><span style=\"font-weight: 400;\">Notice the output is the same whether we use COALESCE() or nested ISNULL() functions. As you can imagine, if you have many items in your list, nesting ISNULL() would become very cumbersome. While it works, coalescing the data is better because it makes the code easier to read.\u00a0<\/span><\/p>\n<h2 id=\"data-type-conversion\"><span style=\"font-weight: 400;\">5. Data type conversion with ISNULL<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Another time ISNULL() will not work is if the data types are not compatible. In SQL Server, the ISNULL() function tries to convert the lower precedence data types into a higher precedence data types. If ISNULL() is not able to convert the data type, it returns an error message.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For example, if we use ISNULL() on personID an try to return the current datetime on a null value, an error will display:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">select ISNULL(personID, getdate()) from people<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-12165\" src=\"http:\/\/careerfoundry.inbearbeitung.de\/en\/wp-content\/uploads\/2022\/02\/image8-1.png\" alt=\"ISNULL SQL tutorial\" width=\"800\" height=\"57\" title=\"\" srcset=\"https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-content\/uploads\/2022\/02\/image8-1.png 800w, https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-content\/uploads\/2022\/02\/image8-1-300x21.png 300w, https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-content\/uploads\/2022\/02\/image8-1-768x55.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">As the error message explains, converting the datetime to an integer is not possible.<\/span><a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/t-sql\/data-types\/data-type-conversion-database-engine?view=sql-server-2017\" rel=\"noopener\"><span style=\"font-weight: 400;\"> Refer to the official documentation for more information on data types and conversions.<\/span><\/a><\/p>\n<h2 id=\"wrapping-up\"><span style=\"font-weight: 400;\">6. Wrapping up<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Dealing with NULL values in SQL Server can be tricky, especially when preparing data for things like machine learning. Using built-in SQL Server functions like ISNULL() and COALESCE() give you options to work around NULL values in the data. In this article we not only explored the situations in which ISNULL() can be used to replace NULL values with a specified value, but we also covered a few situations in which it is not the correct function for the job.<\/span><\/p>\n<p>Want to get a more in-depth look at SQL through a formal course? We&#8217;ve rounded up a list of the <a href=\"https:\/\/careerfoundry.inbearbeitung.de\/en\/blog\/data-analytics\/sql-certifications\/\">best SQL certifications<\/a> in this post.<\/p>\n<p><span style=\"font-weight: 400;\">To learn more about data analytics, try out this <\/span><a href=\"https:\/\/careerfoundry.inbearbeitung.de\/en\/short-courses\/become-a-data-analyst\/?popup-tracking=WYSDN-short-course-DAT\"><span style=\"font-weight: 400;\">free 5-day short course<\/span><\/a><span style=\"font-weight: 400;\">, or check out some of our other data analytics articles:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><a href=\"https:\/\/careerfoundry.inbearbeitung.de\/en\/blog\/data-analytics\/sql-cheat-sheet\/\"><span style=\"font-weight: 400;\">SQL Cheatsheet: Learn Your First 8 Commands<\/span><\/a><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><a href=\"https:\/\/careerfoundry.inbearbeitung.de\/en\/blog\/data-analytics\/data-analytics-projects\/\"><span style=\"font-weight: 400;\">5 of the Best Data Analytics Projects for Beginners<\/span><\/a><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><a href=\"https:\/\/careerfoundry.inbearbeitung.de\/en\/blog\/data-analytics\/data-analysis-in-excel\/\"><span style=\"font-weight: 400;\">10 Essential Excel Features For Data Analysts (and How to Use Them)<\/span><\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Whether you\u2019re learning SQL to become a database administrator, data analyst or data scientist, it is important to understand how to handle NULL values. In this tutorial we\u2019re going to cover how to use the ISNULL() SQL function.<\/p>\n","protected":false},"author":133,"featured_media":12157,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_lmt_disableupdate":"no","_lmt_disable":"","footnotes":""},"categories":[3],"tags":[],"class_list":["post-12155","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-analytics"],"acf":{"homepage_category_featured":false},"modified_by":"Kirstie Sequitin","_links":{"self":[{"href":"https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-json\/wp\/v2\/posts\/12155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-json\/wp\/v2\/users\/133"}],"replies":[{"embeddable":true,"href":"https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-json\/wp\/v2\/comments?post=12155"}],"version-history":[{"count":0,"href":"https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-json\/wp\/v2\/posts\/12155\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-json\/wp\/v2\/media\/12157"}],"wp:attachment":[{"href":"https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-json\/wp\/v2\/media?parent=12155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-json\/wp\/v2\/categories?post=12155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerfoundry.inbearbeitung.de\/en\/wp-json\/wp\/v2\/tags?post=12155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}