Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Using COALESCE() Function

Using COALESCE() Function

Scheduled Pinned Locked Moved Database
databasesql-serversysadminhelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DIPAK EMSYS
    wrote on last edited by
    #1

    Hi all, I am trying to write a procedure which takes 1 parameter as @StudentID and returns Student Information for the given StudentId Parameter form Student and StudentDetail table. Now i want that if parameter is not passed(i.e NULL) then procedure should return all students information,but if StudentId passed then for that student only... I am trying this by using COALESCE function in sql server 2005. Following is DDL for my procedure.

    CREATE PROCEDURE [dbo].[GetStudentById]
    @StudentId AS NUMERIC
    AS
    BEGIN
    SELECT
    S.StudentId,
    S.StudentFName,
    S.StudentMName,
    S.StudentLName,
    S.Address,
    SD.StudentClassId
    FROM
    [dbo].Student S
    JOIN StudentDetail SD ON S.StudentId = SD.StudentId
    WHERE
    S.StudentId = COALESCE(@StudentId,NULL)
    END

    But not getting desired result ..When i am passing NULL 0 rows are returned. please help me out... Thank & Regards Dipak Thesiya

    dipak

    A 1 Reply Last reply
    0
    • D DIPAK EMSYS

      Hi all, I am trying to write a procedure which takes 1 parameter as @StudentID and returns Student Information for the given StudentId Parameter form Student and StudentDetail table. Now i want that if parameter is not passed(i.e NULL) then procedure should return all students information,but if StudentId passed then for that student only... I am trying this by using COALESCE function in sql server 2005. Following is DDL for my procedure.

      CREATE PROCEDURE [dbo].[GetStudentById]
      @StudentId AS NUMERIC
      AS
      BEGIN
      SELECT
      S.StudentId,
      S.StudentFName,
      S.StudentMName,
      S.StudentLName,
      S.Address,
      SD.StudentClassId
      FROM
      [dbo].Student S
      JOIN StudentDetail SD ON S.StudentId = SD.StudentId
      WHERE
      S.StudentId = COALESCE(@StudentId,NULL)
      END

      But not getting desired result ..When i am passing NULL 0 rows are returned. please help me out... Thank & Regards Dipak Thesiya

      dipak

      A Offline
      A Offline
      Ashfield
      wrote on last edited by
      #2

      Your coalesce is wrong. Try this

      WHERE S.StudentId = COALESCE(@StudentId,S.StudentId)

      what you were doing is saying when (@StudentId use null, what you really wanted to say was when (@StudentId is null don't compare - which effectively means compare to the column value.

      Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

      D M 2 Replies Last reply
      0
      • A Ashfield

        Your coalesce is wrong. Try this

        WHERE S.StudentId = COALESCE(@StudentId,S.StudentId)

        what you were doing is saying when (@StudentId use null, what you really wanted to say was when (@StudentId is null don't compare - which effectively means compare to the column value.

        Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

        D Offline
        D Offline
        DIPAK EMSYS
        wrote on last edited by
        #3

        Thanks BOB....its working fine....and ur description is very clear..i mean short and sweet... thanks.... :)

        dipak

        A 1 Reply Last reply
        0
        • D DIPAK EMSYS

          Thanks BOB....its working fine....and ur description is very clear..i mean short and sweet... thanks.... :)

          dipak

          A Offline
          A Offline
          Ashfield
          wrote on last edited by
          #4

          No problem, pleased to help

          Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

          1 Reply Last reply
          0
          • A Ashfield

            Your coalesce is wrong. Try this

            WHERE S.StudentId = COALESCE(@StudentId,S.StudentId)

            what you were doing is saying when (@StudentId use null, what you really wanted to say was when (@StudentId is null don't compare - which effectively means compare to the column value.

            Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            I use the following

            where (IsNull(@StudentID,0) = 0 OR StudentID = @StudentID)

            and have done for years, I wonder if there is a benifit to using COALESCE. It is certainly more elegant than using the OR method.

            Never underestimate the power of human stupidity RAH

            A 1 Reply Last reply
            0
            • M Mycroft Holmes

              I use the following

              where (IsNull(@StudentID,0) = 0 OR StudentID = @StudentID)

              and have done for years, I wonder if there is a benifit to using COALESCE. It is certainly more elegant than using the OR method.

              Never underestimate the power of human stupidity RAH

              A Offline
              A Offline
              Ashfield
              wrote on last edited by
              #6

              For years I used the same sort of thing, then I went to

              where IsNull(@StudentID,StudentID ) = StudentID

              but apparently COALESCE is preferable to isnull - not sure why in this instance. Its certainly good where you have several possible values, such as

              select coalesce(val1,val2,val3) as mainvalue

              Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups