Different ways to replace NULL in sql server - Part 15 — Transcript
Full transcript
- 0:00hello welcome to preim Technologies I am
- 0:02wenet this is part 15 of SQL server in
- 0:06this session we'll learn about the
- 0:07different ways that are available to
- 0:09replace null values in SQL Server
- 0:12specifically we'll be looking at ISL
- 0:14function case statement and coas
- 0:18function please watch part 14 of this
- 0:20video series before continuing with this
- 0:23session now I have an employee table
- 0:26which has got three columns within it
- 0:28employee ID name and manager ID now if I
- 0:33ask you who is the manager of Rob you
- 0:35look at manager ID column okay manager
- 0:38ID for Rob is one and you look up one
- 0:42within the employee ID column okay one
- 0:44is Mike so Rob's manager is Mike so
- 0:47along the same lines if you look Todd
- 0:49Todd manager ID value is null which
- 0:51means Todd doesn't have a manager and if
- 0:54you remember in part 14 of this video
- 0:56series we have written a left outer self
- 1:00join to retrieve employee names along
- 1:02with their manager names okay and this
- 1:05is the output that we have since Todd
- 1:08doesn't have a manager ID his manager
- 1:10name is null which makes sense but then
- 1:13in the output I don't want nulls to be
- 1:16displayed instead of nulls you know if
- 1:19an employee doesn't have a manager
- 1:21instead of displaying null value I want
- 1:23to replace that with no manager okay so
- 1:27instead of an output like this I want an
- 1:30output like this any employee if he
- 1:32doesn't have a manager don't display
- 1:34nulls replace those null values with
- 1:37this no manager word is that possible
- 1:41absolutely we can we we actually have
- 1:44different ways to do that we can make
- 1:45use of e now case statement and coess
- 1:48functions let us look at practical
- 1:50examples of using each one of
- 1:54them all right so since we are we are
- 1:58using this manager idid column and then
- 2:00looking up in the employee ID column
- 2:03we're kind of doing a self jooin here
- 2:05and if you're not sure about what a self
- 2:06jooin is please watch part 14 of this
- 2:09video series okay so let's write the
- 2:12left outer self jooin query so select
- 2:15okay we'll come to the columns list in a
- 2:17bit first let's specify the from Clause
- 2:21from let's say TBL employee table we'll
- 2:25call this as e and I want to do a left
- 2:28join
- 2:31and join with the same
- 2:33table TBL employe but I'm going to Alas
- 2:37that with letter M for manager so this
- 2:40is employee table this is the manager
- 2:43table so from the employee table what do
- 2:46we want if you look at the output I want
- 2:47the employee name so in the employ from
- 2:50the employee table I want the employee
- 2:52name and let's give it a meaningful name
- 2:55so
- 2:57employee and from the manager table I
- 3:00want again the name but this is going to
- 3:03be
- 3:05manager okay so and we have to specify
- 3:09so we specified the column list we
- 3:11specified the employee table we
- 3:13specified the manager table we have to
- 3:15specifi the join condition so on E do in
- 3:20the employee table take the manager ID
- 3:23and look up in the manager table the
- 3:26employee ID column and we have seen I
- 3:29mean we have discussed about this query
- 3:31in a great detail in part 14 of this
- 3:33video series so now when you execute
- 3:35this query we get the output you know
- 3:38the employee names with the manager
- 3:40names but then as we you
- 3:42know like how we have it here Todd
- 3:45doesn't have a manager so he has null
- 3:48there so similarly in the output here
- 3:50null is displayed for Todd but what's
- 3:53our you know ultimate aim our ultimate
- 3:55aim is basically to replace that null
- 3:57value with no manager word so how do we
- 4:00do that the first and simp I mean the
- 4:02easiest way to do it is basically to use
- 4:05eal function okay let's see how to use e
- 4:08Nal function now eull is such a simple
- 4:10function now as the name itself suggest
- 4:13is null if it is null okay is
- 4:18null so if you look at this function you
- 4:20know the intelligence of SQL Server 2008
- 4:23it shows up the first parameter it's an
- 4:26expression that you pass in here if the
- 4:28expression that you are passing in here
- 4:30if it returns null then there is a
- 4:33second parameter called replacement
- 4:35value that parameter will be used so for
- 4:38example let's pass a null here for the
- 4:40expression and replacement value I'm
- 4:43saying let's say no
- 4:48manager okay so when I execute this
- 4:51what's going to happen look at this
- 4:54since this function is we are passing in
- 4:56null as the first parameter since this
- 4:58is null is making use of the replacement
- 5:01value and look at this the column
- 5:03doesn't have a name so let's give it a
- 5:04meaningful name let's call this manager
- 5:07for example so when I execute that look
- 5:09at that you know since we are passing
- 5:11null to e null function it is making use
- 5:14of the replacement value on the other
- 5:16hand let's say instead of saying null
- 5:18I'm saying praim here okay so I'm not
- 5:21passing null now so when you're not
- 5:23passing null and if it is not null then
- 5:26that value will be used so now instead
- 5:28of saying no man
- 5:30it will use that word praim so whatever
- 5:33expression that you pass in here if it
- 5:35returns null then this replacement value
- 5:38will be used if it doesn't return null
- 5:41then that value itself will be used so
- 5:44what we have to do is we have to use
- 5:45this e null function and pass in this m.
- 5:49name into this e null function when this
- 5:53expression returns a manager name it
- 5:55will be used but on the other hand for
- 5:57Todd it returns null so when it returns
- 6:00null it uses this no
- 6:02manager and you know we will have no
- 6:05manager displayed so let's see how to do
- 6:07that you can simply copy the
- 6:12implementation and paste it here okay
- 6:15now we don't want this hardcoded value
- 6:18instead we want to pass M do name so if
- 6:22this name is null then no man use no
- 6:24manager word otherwise use the value
- 6:27returned by m. name so when we execute
- 6:32this okay so Todd has no manager pretty
- 6:35simple very easy to understand along the
- 6:38same lines you can actually use coess
- 6:41function as well so another way to
- 6:42replace null value is to use coess
- 6:45function and if you look up the
- 6:47definition of coess function in msdn
- 6:50books online it says it is used to
- 6:52replace I mean it it Returns the first
- 6:56non-null value what does what do we mean
- 6:59by that don't worry about that for the
- 7:00time being we're going to talk about Co
- 7:02is function in a very very great detail
- 7:04in the next session but for now
- 7:06understand that you know the usage of
- 7:08coess function is pretty much similar to
- 7:11eal function but there is much more to
- 7:13it okay it is basically used to return
- 7:17the first non-null value very powerful
- 7:20function we'll be talking about that
- 7:22piece in the next session okay but what
- 7:24you can do is you know coess function
- 7:27implementation I mean using coess
- 7:29function is is a little similar to e
- 7:32now so Co
- 7:36a and the way I follow to remember the
- 7:40you know the spelling of this function
- 7:41that's a little tricky cool a uh you
- 7:44know it's not easy to remember the
- 7:46spelling but the trick I follow to
- 7:48remember that is uh I spell it like
- 7:51coal Escape e okay that's how I remember
- 7:55that just in case if you want to use
- 7:57that trick to remember the of the
- 7:59function Co C Escape e okay all right Co
- 8:04is and look at this when I execute this
- 8:07I get now
- 8:09praim okay it's exactly similar to the
- 8:11way we use eal function now on the other
- 8:14hand if I pass null for
- 8:19that and I press F5 look at that I get
- 8:22no manager but then colis is much more
- 8:24powerful than is n you can pass in
- 8:27multiple Expressions to that and it
- 8:29Returns the first non-null expression
- 8:32value which we'll be talking about in
- 8:34the next session in a great detail but
- 8:36for now just understand cois usage is
- 8:38much similar to eal function okay so if
- 8:42you want to use colas all you have to do
- 8:45is instead of eal just specify col a CO
- 8:48Escape e and when I execute this
- 8:52obviously Tod instead of displaying null
- 8:55we have no manager and the final way to
- 8:58do it is is to use case statement okay
- 9:01now you have this case statement is also
- 9:04very simple uh basically case I mean the
- 9:09way you use it is case you specify case
- 9:12keyword and then you
- 9:14specify an expression here if that
- 9:17expression case when you specify your
- 9:21expression if that expression returns
- 9:25true then what value you want else
- 9:31what value you
- 9:33want and then you end the case statement
- 9:36so this is the Syntax for the case
- 9:38statement case when you specify the
- 9:41expression this expression returns a
- 9:43true or a false okay if that expression
- 9:47returns true then use the value here on
- 9:51the other hand if it returns false then
- 9:53use the value in the else part and this
- 9:55is the end for case statement so within
- 9:58case you can have as many Expressions as
- 10:01you want okay but here we don't we we
- 10:04only have one expression okay so instead
- 10:08of using Coles here what you basically
- 10:11can do is you can also use the case
- 10:13statement so
- 10:16case
- 10:19when m. name okay so when m. name is
- 10:25null if that is
- 10:27null then I want to use no
- 10:33manager
- 10:36else I want to use m. name itself
- 10:40whatever m. name if it is not null then
- 10:43I want to use the name that is returned
- 10:45by m. name and then you can end the case
- 10:48expression that's
- 10:51it and then you're saying whatever value
- 10:54this case statement returns you know
- 10:56that should be the value for the manager
- 10:59column so this is the alas for that
- 11:01column so case when m. name is null then
- 11:05no manager else you know m. name
- 11:12and all right let's execute this and see
- 11:15the
- 11:17output okay look at that the output is
- 11:20exactly similar so you can either use
- 11:22case statement is null or coess function
- 11:24we'll be talking about coess function in
- 11:26a great detail in the next session and
- 11:29you can see the implementation right
- 11:31here for three of
- 11:33them on this slide you can find
- 11:35resources for asp.net and cop interview
- 11:37questions that's it for today thank you
- 11:39for listening have a great day
About this transcript
This page contains the full transcript of Different ways to replace NULL in sql server - Part 15 by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 1,825 words across 253 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.