Coalesce function in sql server Part 16 — Transcript
Full transcript
- 0:00hello welcome to pre Technologies I'm
- 0:02wut this is part 16 of SQL server in
- 0:05this session we'll understand how to use
- 0:08coess function with an example now
- 0:11according to the msdn books online Coes
- 0:13function Returns the first nonnull value
- 0:17let's understand what we mean by this
- 0:19now if you look at this employee table
- 0:20here we've got ID first name middle name
- 0:23and last name columns and if you look at
- 0:25these employees some of them have just
- 0:28their first name some of them have their
- 0:30middle and last name and some of the
- 0:32employees have just their last name now
- 0:34we want to write a query which pulls out
- 0:38the ID and the name of the person as you
- 0:41can see on the slide now when you pull
- 0:43out an when you pull out the name okay
- 0:47if the employee has got the first name
- 0:49you pull that out okay but whereas if an
- 0:53employee does not have a first name and
- 0:55if it has middle and last name then we
- 0:58want to pull their middle name okay and
- 1:01if an employee doesn't have first name
- 1:03and middle name and if he has only the
- 1:06last name then we want to pull their
- 1:08last name okay but whereas if an
- 1:10employee has both first and middle name
- 1:14then we just want their first
- 1:16name similarly if he has all of their
- 1:19names you know first middle and last
- 1:21names then still we we just want their
- 1:23first name so the priority should first
- 1:25go to their first name if that's present
- 1:28pull it out if not then and middle name
- 1:31if first and middle name both of them
- 1:33are not available only then we pull the
- 1:35last name okay so that's how our output
- 1:38has to be all right so in order to do
- 1:41that we we can use the coess function
- 1:43now if you look at the query here to the
- 1:46coess function you're passing in look at
- 1:49this in the output we want the ID and
- 1:50the name of the person so obviously in
- 1:52the select list we'll have ID and then
- 1:54to pull out the name we are using the
- 1:56coas function we are not saying first
- 1:58name middle name last name dire we are
- 2:00passing those column names into this
- 2:02coas function look at the definition of
- 2:04the coas function it Returns the first
- 2:07non-null value so what's going to happen
- 2:09when you execute this query the coess
- 2:12function will check okay in this row is
- 2:15the first name available yes the first
- 2:17name available so first name is not null
- 2:21so it will immediately return Sam okay
- 2:24but whereas when it comes to the second
- 2:25row it checks okay is the first name
- 2:28available no it's null so what happens
- 2:31it checks the middle name okay is middle
- 2:33name present yes then what is that you
- 2:36know it's not null it's present so it's
- 2:38going to return that middle name which
- 2:40is Todd similarly it comes to the third
- 2:43row it checks okay is first name null
- 2:45yes it is null so it goes to the middle
- 2:47name okay is middle name null yes it is
- 2:49still null so it goes to last name okay
- 2:53and then return Sara so we get that but
- 2:55come here to the fourth row when it
- 2:57comes here the first name itself is
- 2:59present it doesn't even bother to check
- 3:01the middle name okay because Coles
- 3:03function Returns the first non-null
- 3:06value okay so col is first name middle
- 3:09name last name and we are giving it an
- 3:11alias name from TBL employee okay let's
- 3:14quickly write this query and see the
- 3:16result in
- 3:17action so if you look at this we exactly
- 3:20have the same table here and we want to
- 3:22select their ID so select ID and what
- 3:26else we want we want the name of the
- 3:28employee
- 3:31okay but we want to use the coas
- 3:34function so coas what do we want we want
- 3:37to pass into this function first
- 3:42name the middle name and last
- 3:46name so when we pass these three columns
- 3:49whichever column Returns the first
- 3:51nonnull value that value will be used in
- 3:54the output and we want to give this
- 3:56column an alas let's call it name and
- 3:59from which table are we trying to
- 4:01retrieve this from the employee table so
- 4:05TBL employee so let's execute
- 4:09that okay so if you look at this we get
- 4:12the output that we
- 4:18expected on this slide you can find
- 4:20resources for asp.net and cop interview
- 4:23questions that's it for today thank you
- 4:25for listening have a great day
About this transcript
This page contains the full transcript of Coalesce function in sql server Part 16 by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 779 words across 103 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.