How to use where clause if value from another table is null










0















I think this maybe a simple question, but I kind of stuck for a non-SQL Developer.



Example my select statement is like this.



Select ID, Year
From Table A
Where Year = (Select Year from table B)


If year from table B is null, I want to return all rows from table A else filter by value from table B.



Now what happen is when table B is null there is no rows return from table A.



Edited: There is only one row in Table B, and Value year from table B can be manually control to be null or not null.










share|improve this question
























  • I think we need to know under what circumstances year would be null in table B. There is no parameter nor linking field involved, maybe something is missing from the above?

    – Dale Burrell
    Nov 13 '18 at 4:22











  • Hi I have edited my question, maybe can clarify a bit ?

    – xChaax
    Nov 13 '18 at 4:25











  • You have some answers to try below.

    – Dale Burrell
    Nov 13 '18 at 4:28















0















I think this maybe a simple question, but I kind of stuck for a non-SQL Developer.



Example my select statement is like this.



Select ID, Year
From Table A
Where Year = (Select Year from table B)


If year from table B is null, I want to return all rows from table A else filter by value from table B.



Now what happen is when table B is null there is no rows return from table A.



Edited: There is only one row in Table B, and Value year from table B can be manually control to be null or not null.










share|improve this question
























  • I think we need to know under what circumstances year would be null in table B. There is no parameter nor linking field involved, maybe something is missing from the above?

    – Dale Burrell
    Nov 13 '18 at 4:22











  • Hi I have edited my question, maybe can clarify a bit ?

    – xChaax
    Nov 13 '18 at 4:25











  • You have some answers to try below.

    – Dale Burrell
    Nov 13 '18 at 4:28













0












0








0








I think this maybe a simple question, but I kind of stuck for a non-SQL Developer.



Example my select statement is like this.



Select ID, Year
From Table A
Where Year = (Select Year from table B)


If year from table B is null, I want to return all rows from table A else filter by value from table B.



Now what happen is when table B is null there is no rows return from table A.



Edited: There is only one row in Table B, and Value year from table B can be manually control to be null or not null.










share|improve this question
















I think this maybe a simple question, but I kind of stuck for a non-SQL Developer.



Example my select statement is like this.



Select ID, Year
From Table A
Where Year = (Select Year from table B)


If year from table B is null, I want to return all rows from table A else filter by value from table B.



Now what happen is when table B is null there is no rows return from table A.



Edited: There is only one row in Table B, and Value year from table B can be manually control to be null or not null.







sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 4:25







xChaax

















asked Nov 13 '18 at 4:00









xChaaxxChaax

11313




11313












  • I think we need to know under what circumstances year would be null in table B. There is no parameter nor linking field involved, maybe something is missing from the above?

    – Dale Burrell
    Nov 13 '18 at 4:22











  • Hi I have edited my question, maybe can clarify a bit ?

    – xChaax
    Nov 13 '18 at 4:25











  • You have some answers to try below.

    – Dale Burrell
    Nov 13 '18 at 4:28

















  • I think we need to know under what circumstances year would be null in table B. There is no parameter nor linking field involved, maybe something is missing from the above?

    – Dale Burrell
    Nov 13 '18 at 4:22











  • Hi I have edited my question, maybe can clarify a bit ?

    – xChaax
    Nov 13 '18 at 4:25











  • You have some answers to try below.

    – Dale Burrell
    Nov 13 '18 at 4:28
















I think we need to know under what circumstances year would be null in table B. There is no parameter nor linking field involved, maybe something is missing from the above?

– Dale Burrell
Nov 13 '18 at 4:22





I think we need to know under what circumstances year would be null in table B. There is no parameter nor linking field involved, maybe something is missing from the above?

– Dale Burrell
Nov 13 '18 at 4:22













Hi I have edited my question, maybe can clarify a bit ?

– xChaax
Nov 13 '18 at 4:25





Hi I have edited my question, maybe can clarify a bit ?

– xChaax
Nov 13 '18 at 4:25













You have some answers to try below.

– Dale Burrell
Nov 13 '18 at 4:28





You have some answers to try below.

– Dale Burrell
Nov 13 '18 at 4:28












4 Answers
4






active

oldest

votes


















2














Don't use as Where Year = (Select Year from table B)
Because if TableB has more than one records, the above query will return an error.



Use this query instead;



SELECT TA.ID, TA.Year
FROM TableA TA
LEFT JOIN TableB TB ON TB.Year = TA.Year


Updated:
According to the update you made to the question, the query will be;



SELECT TA.ID, TA.Year
FROM TableA TA
WHERE TA.Year= (Select Year from table B)
OR (Select Year from table B) IS NULL





share|improve this answer

























  • While a useful suggestion it doesn't solve their problem.

    – Dale Burrell
    Nov 13 '18 at 4:12











  • @DaleBurrell why not? This will return all the data in TableA even though the TableB does not have any data. Isn't it?

    – Thilina Nakkawita
    Nov 13 '18 at 4:14






  • 1





    The question states either all rows OR the rows for the year in question - they don't want all the rows all the time. To be fair its not clear under what circumstances it would be one or the other.

    – Dale Burrell
    Nov 13 '18 at 4:23


















0














It's a bit odd to have a specification that a table will always have exactly one row. It's not invalid, but it's unusual.



If table B were empty, if it contained zero rows, then what should the query return? An error? An empty set? or all rows from A?



If table B contained more than one row, what should be returned? An error? An empty set? Rows from A that matched one of the non-NULL values of year from B? All rows from A?



Most often, its the edge cases and corner cases that spell out the specification... we expect the cases to be tested, we need to know what to test for... and then we can develop a query.



There are several query patterns that would satisfy the spec; the big differences are going to be those edge cases... table B is empty, or year column in table A is NULL.



I'd probably tend to go with something like this:



 SELECT a.id
, a.year
FROM ( SELECT MAX(b.year) AS year
FROM table_b b
) v
JOIN table_a a
ON a.year <=> IFNULL(v.year,a.year)


SQL Fiddle demonstration http://sqlfiddle.com/#!9/44c277/1




If the question is actually about using the WHERE clause, then feel free to replace the keyword ON with WHERE.



DOH! Reviewing the question, it's asking about SQL Server, not MySQL. The syntax above is for MySQL. For SQL Server, then something like this:



 SELECT a.id
, a.year
FROM ( SELECT MAX(b.year) AS year
FROM table_b b
) v
JOIN table_a a
ON COALESCE(a.year,-1) = COALESCE(v.year,a.year,-1)


We are using -1 as a special placeholder value, as a replacement for NULL, so the equality comparison will be satisfied. Without a special placeholder value, we'll have to incorporate some IS NULL conditional tests.






share|improve this answer
































    -1














    Assuming your inner query return single row only.



    You can do this like.



    SELECT ID, Year
    FROM TableA
    WHERE Year =
    (
    SELECT Year
    FROM TableB
    )
    OR Year IS NULL


    As far as your line "If year from table B is null, I want to return all rows from table A else filter by value from table B." is concerns, use the following query



    SELECT ID, Year
    FROM TableA
    WHERE NVL(Year, '0') = (CASE WHEN (SELECT Year FROM TableB) IS NOT NULL THEN (SELECT Year FROM TableB) ELSE NVL(Year, '0') END)


    I did not test this query, but hopes that you will get the point here.






    share|improve this answer






























      -2














      use ISNULL and set the NULL as 0 like



      Select ID, Year
      From Table A
      Where Year = (Select ISNULL(Year,0) from table B)



      Hope this works!






      share|improve this answer























      • That won't work if there are no rows in the table.

        – Dale Burrell
        Nov 13 '18 at 4:11











      • I'm fairly certain this doesn't satisfy the specification, to return ALL rows in A when B.year is NULL.

        – spencer7593
        Nov 13 '18 at 4:31










      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273581%2fhow-to-use-where-clause-if-value-from-another-table-is-null%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      Don't use as Where Year = (Select Year from table B)
      Because if TableB has more than one records, the above query will return an error.



      Use this query instead;



      SELECT TA.ID, TA.Year
      FROM TableA TA
      LEFT JOIN TableB TB ON TB.Year = TA.Year


      Updated:
      According to the update you made to the question, the query will be;



      SELECT TA.ID, TA.Year
      FROM TableA TA
      WHERE TA.Year= (Select Year from table B)
      OR (Select Year from table B) IS NULL





      share|improve this answer

























      • While a useful suggestion it doesn't solve their problem.

        – Dale Burrell
        Nov 13 '18 at 4:12











      • @DaleBurrell why not? This will return all the data in TableA even though the TableB does not have any data. Isn't it?

        – Thilina Nakkawita
        Nov 13 '18 at 4:14






      • 1





        The question states either all rows OR the rows for the year in question - they don't want all the rows all the time. To be fair its not clear under what circumstances it would be one or the other.

        – Dale Burrell
        Nov 13 '18 at 4:23















      2














      Don't use as Where Year = (Select Year from table B)
      Because if TableB has more than one records, the above query will return an error.



      Use this query instead;



      SELECT TA.ID, TA.Year
      FROM TableA TA
      LEFT JOIN TableB TB ON TB.Year = TA.Year


      Updated:
      According to the update you made to the question, the query will be;



      SELECT TA.ID, TA.Year
      FROM TableA TA
      WHERE TA.Year= (Select Year from table B)
      OR (Select Year from table B) IS NULL





      share|improve this answer

























      • While a useful suggestion it doesn't solve their problem.

        – Dale Burrell
        Nov 13 '18 at 4:12











      • @DaleBurrell why not? This will return all the data in TableA even though the TableB does not have any data. Isn't it?

        – Thilina Nakkawita
        Nov 13 '18 at 4:14






      • 1





        The question states either all rows OR the rows for the year in question - they don't want all the rows all the time. To be fair its not clear under what circumstances it would be one or the other.

        – Dale Burrell
        Nov 13 '18 at 4:23













      2












      2








      2







      Don't use as Where Year = (Select Year from table B)
      Because if TableB has more than one records, the above query will return an error.



      Use this query instead;



      SELECT TA.ID, TA.Year
      FROM TableA TA
      LEFT JOIN TableB TB ON TB.Year = TA.Year


      Updated:
      According to the update you made to the question, the query will be;



      SELECT TA.ID, TA.Year
      FROM TableA TA
      WHERE TA.Year= (Select Year from table B)
      OR (Select Year from table B) IS NULL





      share|improve this answer















      Don't use as Where Year = (Select Year from table B)
      Because if TableB has more than one records, the above query will return an error.



      Use this query instead;



      SELECT TA.ID, TA.Year
      FROM TableA TA
      LEFT JOIN TableB TB ON TB.Year = TA.Year


      Updated:
      According to the update you made to the question, the query will be;



      SELECT TA.ID, TA.Year
      FROM TableA TA
      WHERE TA.Year= (Select Year from table B)
      OR (Select Year from table B) IS NULL






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 13 '18 at 4:31

























      answered Nov 13 '18 at 4:11









      Thilina NakkawitaThilina Nakkawita

      8751027




      8751027












      • While a useful suggestion it doesn't solve their problem.

        – Dale Burrell
        Nov 13 '18 at 4:12











      • @DaleBurrell why not? This will return all the data in TableA even though the TableB does not have any data. Isn't it?

        – Thilina Nakkawita
        Nov 13 '18 at 4:14






      • 1





        The question states either all rows OR the rows for the year in question - they don't want all the rows all the time. To be fair its not clear under what circumstances it would be one or the other.

        – Dale Burrell
        Nov 13 '18 at 4:23

















      • While a useful suggestion it doesn't solve their problem.

        – Dale Burrell
        Nov 13 '18 at 4:12











      • @DaleBurrell why not? This will return all the data in TableA even though the TableB does not have any data. Isn't it?

        – Thilina Nakkawita
        Nov 13 '18 at 4:14






      • 1





        The question states either all rows OR the rows for the year in question - they don't want all the rows all the time. To be fair its not clear under what circumstances it would be one or the other.

        – Dale Burrell
        Nov 13 '18 at 4:23
















      While a useful suggestion it doesn't solve their problem.

      – Dale Burrell
      Nov 13 '18 at 4:12





      While a useful suggestion it doesn't solve their problem.

      – Dale Burrell
      Nov 13 '18 at 4:12













      @DaleBurrell why not? This will return all the data in TableA even though the TableB does not have any data. Isn't it?

      – Thilina Nakkawita
      Nov 13 '18 at 4:14





      @DaleBurrell why not? This will return all the data in TableA even though the TableB does not have any data. Isn't it?

      – Thilina Nakkawita
      Nov 13 '18 at 4:14




      1




      1





      The question states either all rows OR the rows for the year in question - they don't want all the rows all the time. To be fair its not clear under what circumstances it would be one or the other.

      – Dale Burrell
      Nov 13 '18 at 4:23





      The question states either all rows OR the rows for the year in question - they don't want all the rows all the time. To be fair its not clear under what circumstances it would be one or the other.

      – Dale Burrell
      Nov 13 '18 at 4:23













      0














      It's a bit odd to have a specification that a table will always have exactly one row. It's not invalid, but it's unusual.



      If table B were empty, if it contained zero rows, then what should the query return? An error? An empty set? or all rows from A?



      If table B contained more than one row, what should be returned? An error? An empty set? Rows from A that matched one of the non-NULL values of year from B? All rows from A?



      Most often, its the edge cases and corner cases that spell out the specification... we expect the cases to be tested, we need to know what to test for... and then we can develop a query.



      There are several query patterns that would satisfy the spec; the big differences are going to be those edge cases... table B is empty, or year column in table A is NULL.



      I'd probably tend to go with something like this:



       SELECT a.id
      , a.year
      FROM ( SELECT MAX(b.year) AS year
      FROM table_b b
      ) v
      JOIN table_a a
      ON a.year <=> IFNULL(v.year,a.year)


      SQL Fiddle demonstration http://sqlfiddle.com/#!9/44c277/1




      If the question is actually about using the WHERE clause, then feel free to replace the keyword ON with WHERE.



      DOH! Reviewing the question, it's asking about SQL Server, not MySQL. The syntax above is for MySQL. For SQL Server, then something like this:



       SELECT a.id
      , a.year
      FROM ( SELECT MAX(b.year) AS year
      FROM table_b b
      ) v
      JOIN table_a a
      ON COALESCE(a.year,-1) = COALESCE(v.year,a.year,-1)


      We are using -1 as a special placeholder value, as a replacement for NULL, so the equality comparison will be satisfied. Without a special placeholder value, we'll have to incorporate some IS NULL conditional tests.






      share|improve this answer





























        0














        It's a bit odd to have a specification that a table will always have exactly one row. It's not invalid, but it's unusual.



        If table B were empty, if it contained zero rows, then what should the query return? An error? An empty set? or all rows from A?



        If table B contained more than one row, what should be returned? An error? An empty set? Rows from A that matched one of the non-NULL values of year from B? All rows from A?



        Most often, its the edge cases and corner cases that spell out the specification... we expect the cases to be tested, we need to know what to test for... and then we can develop a query.



        There are several query patterns that would satisfy the spec; the big differences are going to be those edge cases... table B is empty, or year column in table A is NULL.



        I'd probably tend to go with something like this:



         SELECT a.id
        , a.year
        FROM ( SELECT MAX(b.year) AS year
        FROM table_b b
        ) v
        JOIN table_a a
        ON a.year <=> IFNULL(v.year,a.year)


        SQL Fiddle demonstration http://sqlfiddle.com/#!9/44c277/1




        If the question is actually about using the WHERE clause, then feel free to replace the keyword ON with WHERE.



        DOH! Reviewing the question, it's asking about SQL Server, not MySQL. The syntax above is for MySQL. For SQL Server, then something like this:



         SELECT a.id
        , a.year
        FROM ( SELECT MAX(b.year) AS year
        FROM table_b b
        ) v
        JOIN table_a a
        ON COALESCE(a.year,-1) = COALESCE(v.year,a.year,-1)


        We are using -1 as a special placeholder value, as a replacement for NULL, so the equality comparison will be satisfied. Without a special placeholder value, we'll have to incorporate some IS NULL conditional tests.






        share|improve this answer



























          0












          0








          0







          It's a bit odd to have a specification that a table will always have exactly one row. It's not invalid, but it's unusual.



          If table B were empty, if it contained zero rows, then what should the query return? An error? An empty set? or all rows from A?



          If table B contained more than one row, what should be returned? An error? An empty set? Rows from A that matched one of the non-NULL values of year from B? All rows from A?



          Most often, its the edge cases and corner cases that spell out the specification... we expect the cases to be tested, we need to know what to test for... and then we can develop a query.



          There are several query patterns that would satisfy the spec; the big differences are going to be those edge cases... table B is empty, or year column in table A is NULL.



          I'd probably tend to go with something like this:



           SELECT a.id
          , a.year
          FROM ( SELECT MAX(b.year) AS year
          FROM table_b b
          ) v
          JOIN table_a a
          ON a.year <=> IFNULL(v.year,a.year)


          SQL Fiddle demonstration http://sqlfiddle.com/#!9/44c277/1




          If the question is actually about using the WHERE clause, then feel free to replace the keyword ON with WHERE.



          DOH! Reviewing the question, it's asking about SQL Server, not MySQL. The syntax above is for MySQL. For SQL Server, then something like this:



           SELECT a.id
          , a.year
          FROM ( SELECT MAX(b.year) AS year
          FROM table_b b
          ) v
          JOIN table_a a
          ON COALESCE(a.year,-1) = COALESCE(v.year,a.year,-1)


          We are using -1 as a special placeholder value, as a replacement for NULL, so the equality comparison will be satisfied. Without a special placeholder value, we'll have to incorporate some IS NULL conditional tests.






          share|improve this answer















          It's a bit odd to have a specification that a table will always have exactly one row. It's not invalid, but it's unusual.



          If table B were empty, if it contained zero rows, then what should the query return? An error? An empty set? or all rows from A?



          If table B contained more than one row, what should be returned? An error? An empty set? Rows from A that matched one of the non-NULL values of year from B? All rows from A?



          Most often, its the edge cases and corner cases that spell out the specification... we expect the cases to be tested, we need to know what to test for... and then we can develop a query.



          There are several query patterns that would satisfy the spec; the big differences are going to be those edge cases... table B is empty, or year column in table A is NULL.



          I'd probably tend to go with something like this:



           SELECT a.id
          , a.year
          FROM ( SELECT MAX(b.year) AS year
          FROM table_b b
          ) v
          JOIN table_a a
          ON a.year <=> IFNULL(v.year,a.year)


          SQL Fiddle demonstration http://sqlfiddle.com/#!9/44c277/1




          If the question is actually about using the WHERE clause, then feel free to replace the keyword ON with WHERE.



          DOH! Reviewing the question, it's asking about SQL Server, not MySQL. The syntax above is for MySQL. For SQL Server, then something like this:



           SELECT a.id
          , a.year
          FROM ( SELECT MAX(b.year) AS year
          FROM table_b b
          ) v
          JOIN table_a a
          ON COALESCE(a.year,-1) = COALESCE(v.year,a.year,-1)


          We are using -1 as a special placeholder value, as a replacement for NULL, so the equality comparison will be satisfied. Without a special placeholder value, we'll have to incorporate some IS NULL conditional tests.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 5:03

























          answered Nov 13 '18 at 4:47









          spencer7593spencer7593

          84.2k107994




          84.2k107994





















              -1














              Assuming your inner query return single row only.



              You can do this like.



              SELECT ID, Year
              FROM TableA
              WHERE Year =
              (
              SELECT Year
              FROM TableB
              )
              OR Year IS NULL


              As far as your line "If year from table B is null, I want to return all rows from table A else filter by value from table B." is concerns, use the following query



              SELECT ID, Year
              FROM TableA
              WHERE NVL(Year, '0') = (CASE WHEN (SELECT Year FROM TableB) IS NOT NULL THEN (SELECT Year FROM TableB) ELSE NVL(Year, '0') END)


              I did not test this query, but hopes that you will get the point here.






              share|improve this answer



























                -1














                Assuming your inner query return single row only.



                You can do this like.



                SELECT ID, Year
                FROM TableA
                WHERE Year =
                (
                SELECT Year
                FROM TableB
                )
                OR Year IS NULL


                As far as your line "If year from table B is null, I want to return all rows from table A else filter by value from table B." is concerns, use the following query



                SELECT ID, Year
                FROM TableA
                WHERE NVL(Year, '0') = (CASE WHEN (SELECT Year FROM TableB) IS NOT NULL THEN (SELECT Year FROM TableB) ELSE NVL(Year, '0') END)


                I did not test this query, but hopes that you will get the point here.






                share|improve this answer

























                  -1












                  -1








                  -1







                  Assuming your inner query return single row only.



                  You can do this like.



                  SELECT ID, Year
                  FROM TableA
                  WHERE Year =
                  (
                  SELECT Year
                  FROM TableB
                  )
                  OR Year IS NULL


                  As far as your line "If year from table B is null, I want to return all rows from table A else filter by value from table B." is concerns, use the following query



                  SELECT ID, Year
                  FROM TableA
                  WHERE NVL(Year, '0') = (CASE WHEN (SELECT Year FROM TableB) IS NOT NULL THEN (SELECT Year FROM TableB) ELSE NVL(Year, '0') END)


                  I did not test this query, but hopes that you will get the point here.






                  share|improve this answer













                  Assuming your inner query return single row only.



                  You can do this like.



                  SELECT ID, Year
                  FROM TableA
                  WHERE Year =
                  (
                  SELECT Year
                  FROM TableB
                  )
                  OR Year IS NULL


                  As far as your line "If year from table B is null, I want to return all rows from table A else filter by value from table B." is concerns, use the following query



                  SELECT ID, Year
                  FROM TableA
                  WHERE NVL(Year, '0') = (CASE WHEN (SELECT Year FROM TableB) IS NOT NULL THEN (SELECT Year FROM TableB) ELSE NVL(Year, '0') END)


                  I did not test this query, but hopes that you will get the point here.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 5:03









                  Waqas ShabbirWaqas Shabbir

                  5331730




                  5331730





















                      -2














                      use ISNULL and set the NULL as 0 like



                      Select ID, Year
                      From Table A
                      Where Year = (Select ISNULL(Year,0) from table B)



                      Hope this works!






                      share|improve this answer























                      • That won't work if there are no rows in the table.

                        – Dale Burrell
                        Nov 13 '18 at 4:11











                      • I'm fairly certain this doesn't satisfy the specification, to return ALL rows in A when B.year is NULL.

                        – spencer7593
                        Nov 13 '18 at 4:31















                      -2














                      use ISNULL and set the NULL as 0 like



                      Select ID, Year
                      From Table A
                      Where Year = (Select ISNULL(Year,0) from table B)



                      Hope this works!






                      share|improve this answer























                      • That won't work if there are no rows in the table.

                        – Dale Burrell
                        Nov 13 '18 at 4:11











                      • I'm fairly certain this doesn't satisfy the specification, to return ALL rows in A when B.year is NULL.

                        – spencer7593
                        Nov 13 '18 at 4:31













                      -2












                      -2








                      -2







                      use ISNULL and set the NULL as 0 like



                      Select ID, Year
                      From Table A
                      Where Year = (Select ISNULL(Year,0) from table B)



                      Hope this works!






                      share|improve this answer













                      use ISNULL and set the NULL as 0 like



                      Select ID, Year
                      From Table A
                      Where Year = (Select ISNULL(Year,0) from table B)



                      Hope this works!







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 13 '18 at 4:05









                      anand jhawaranand jhawar

                      1




                      1












                      • That won't work if there are no rows in the table.

                        – Dale Burrell
                        Nov 13 '18 at 4:11











                      • I'm fairly certain this doesn't satisfy the specification, to return ALL rows in A when B.year is NULL.

                        – spencer7593
                        Nov 13 '18 at 4:31

















                      • That won't work if there are no rows in the table.

                        – Dale Burrell
                        Nov 13 '18 at 4:11











                      • I'm fairly certain this doesn't satisfy the specification, to return ALL rows in A when B.year is NULL.

                        – spencer7593
                        Nov 13 '18 at 4:31
















                      That won't work if there are no rows in the table.

                      – Dale Burrell
                      Nov 13 '18 at 4:11





                      That won't work if there are no rows in the table.

                      – Dale Burrell
                      Nov 13 '18 at 4:11













                      I'm fairly certain this doesn't satisfy the specification, to return ALL rows in A when B.year is NULL.

                      – spencer7593
                      Nov 13 '18 at 4:31





                      I'm fairly certain this doesn't satisfy the specification, to return ALL rows in A when B.year is NULL.

                      – spencer7593
                      Nov 13 '18 at 4:31

















                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273581%2fhow-to-use-where-clause-if-value-from-another-table-is-null%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3

                      Museum of Modern and Contemporary Art of Trento and Rovereto