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. put a condition in join query

put a condition in join query

Scheduled Pinned Locked Moved Database
sharepointdatabasewpfwcftutorial
7 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
    demoninside9
    wrote on last edited by
    #1

    hi all below is my Store Proc

    SELECT vb.Sno,
    vb.Vehicle_Number,
    vb.Vehicle_Booking_Date,
    vb.Time_From,
    vb.Time_To,
    vb.Vehicle_Used_By,
    vb.Cost_Code,
    vb.Budget_Line,
    lb.Vehicle_Number,
    lb.Vehicle_Booking_Date,
    lb.Time_From,
    lb.Time_To,
    lb.KM_Start,
    lb.KM_End,
    lb.Vehicle_Used_By,
    lb.Cost_Code,
    lb.Budget_Line
    FROM mtblVehicle_Booking vb
    LEFT JOIN mtblLog_Book lb
    ON vb.Sno = lb.Sno
    where vb.Vehicle_Number = @Vehicle_Number
    AND DATEPART(MONTH,vb.Vehicle_Booking_Date) = MONTH(getdate())

    it is working fine. But here I need to put a condition, it should give priority to mtblLog_Book means if mtblLog_Book having data then it should show from mtblLog_Book table otherwise it should shows data from mtblVehicle_Booking for a particular Sno.

    Actually firstly on the behalf of mtblVehicle_Booking I am binding a Gridview after updating the data it should show the data from mtblLog_Book.

    So let me know please how to put a condition in my sp.

    Thanks

    J 1 Reply Last reply
    0
    • D demoninside9

      hi all below is my Store Proc

      SELECT vb.Sno,
      vb.Vehicle_Number,
      vb.Vehicle_Booking_Date,
      vb.Time_From,
      vb.Time_To,
      vb.Vehicle_Used_By,
      vb.Cost_Code,
      vb.Budget_Line,
      lb.Vehicle_Number,
      lb.Vehicle_Booking_Date,
      lb.Time_From,
      lb.Time_To,
      lb.KM_Start,
      lb.KM_End,
      lb.Vehicle_Used_By,
      lb.Cost_Code,
      lb.Budget_Line
      FROM mtblVehicle_Booking vb
      LEFT JOIN mtblLog_Book lb
      ON vb.Sno = lb.Sno
      where vb.Vehicle_Number = @Vehicle_Number
      AND DATEPART(MONTH,vb.Vehicle_Booking_Date) = MONTH(getdate())

      it is working fine. But here I need to put a condition, it should give priority to mtblLog_Book means if mtblLog_Book having data then it should show from mtblLog_Book table otherwise it should shows data from mtblVehicle_Booking for a particular Sno.

      Actually firstly on the behalf of mtblVehicle_Booking I am binding a Gridview after updating the data it should show the data from mtblLog_Book.

      So let me know please how to put a condition in my sp.

      Thanks

      J Offline
      J Offline
      Jorgen Andersson
      wrote on last edited by
      #2

      Use a Case When or IsNull condition like this:

      SELECT isnull(lb.somefield,vb.anotherfield) as columnname
      FROM mtblVehicle_Booking vb
      LEFT JOIN mtblLog_Book lb
      ON vb.Sno = lb.Sno

      If lb.somefield is null it will be replaced by vb.anotherfield

      "The ones who care enough to do it right care too much to compromise." Matthew Faithfull

      D 2 Replies Last reply
      0
      • J Jorgen Andersson

        Use a Case When or IsNull condition like this:

        SELECT isnull(lb.somefield,vb.anotherfield) as columnname
        FROM mtblVehicle_Booking vb
        LEFT JOIN mtblLog_Book lb
        ON vb.Sno = lb.Sno

        If lb.somefield is null it will be replaced by vb.anotherfield

        "The ones who care enough to do it right care too much to compromise." Matthew Faithfull

        D Offline
        D Offline
        demoninside9
        wrote on last edited by
        #3

        your query may be right, let me complete my sentence. according to you query it will show the not null columns, Yes I need that, but suppose I update vehicle_no in LogBook, now both Vehcile_Booking table and LogBook tabel both have Different Vehicle_no, but after updation I just want to show it from LogBook table instead of Vehicle_Booking table.

        M 1 Reply Last reply
        0
        • D demoninside9

          your query may be right, let me complete my sentence. according to you query it will show the not null columns, Yes I need that, but suppose I update vehicle_no in LogBook, now both Vehcile_Booking table and LogBook tabel both have Different Vehicle_no, but after updation I just want to show it from LogBook table instead of Vehicle_Booking table.

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

          Did you actually try what Jorgen wrote, the isnull test does exactly what you are asking for:

          select ISNULL(LogBook, Vehcile_Booking) from ..

          . Will test logbook and return a non null result, if it is null then it will return vehicle_booking.

          Never underestimate the power of human stupidity RAH

          D 1 Reply Last reply
          0
          • M Mycroft Holmes

            Did you actually try what Jorgen wrote, the isnull test does exactly what you are asking for:

            select ISNULL(LogBook, Vehcile_Booking) from ..

            . Will test logbook and return a non null result, if it is null then it will return vehicle_booking.

            Never underestimate the power of human stupidity RAH

            D Offline
            D Offline
            demoninside9
            wrote on last edited by
            #5

            Mycroft Holmes wrote: if it is null then it will return vehicle_booking and if it is not null then, it will show value from LogBook?

            M 1 Reply Last reply
            0
            • D demoninside9

              Mycroft Holmes wrote: if it is null then it will return vehicle_booking and if it is not null then, it will show value from LogBook?

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

              No, if LogBook is not null then the not null value will be returned from logbook

              Never underestimate the power of human stupidity RAH

              1 Reply Last reply
              0
              • J Jorgen Andersson

                Use a Case When or IsNull condition like this:

                SELECT isnull(lb.somefield,vb.anotherfield) as columnname
                FROM mtblVehicle_Booking vb
                LEFT JOIN mtblLog_Book lb
                ON vb.Sno = lb.Sno

                If lb.somefield is null it will be replaced by vb.anotherfield

                "The ones who care enough to do it right care too much to compromise." Matthew Faithfull

                D Offline
                D Offline
                demoninside9
                wrote on last edited by
                #7

                SELECT vb.Sno,

                        ISNULL(lb.Vehicle\_Number,vb.Vehicle\_Number) as  Vehicle\_Number,
                         ISNULL(lb.Vehicle\_Booking\_Date,vb.Vehicle\_Booking\_Date) as  Vehicle\_Booking\_Date,
                         ISNULL(lb.Time\_From,vb.Time\_From) as vb.Time\_To,
                          ISNULL(lb.Time\_To,vb.Time\_To) as vb.Time\_To,
                           ISNULL(lb.Vehicle\_Used\_By,vb.Vehicle\_Used\_By) as Vehicle\_Used\_By,
                           ISNULL(lb.Cost\_Code,vb.Cost\_Code) as Cost\_Code,
                            ISNULL(lb.Budget\_Line,vb.Budget\_Line) as Budget\_Line,	
                     
                       lb.KM\_Start,
                       lb.KM\_End 
                     
                    
                FROM   mtblVehicle\_Booking vb
                       LEFT JOIN mtblLog\_Book lb
                         ON vb.Sno = lb.Sno
                            where vb.Vehicle\_Number = @Vehicle\_Number
                            AND DATEPART(MONTH,vb.Vehicle\_Booking\_Date)  = MONTH(getdate())
                

                showing error Incorrect syntax near '.'.

                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