Compilation of T-SQL oddities and pitfallsPossible pitfalls of condensing 2 SQL Server instances into...

Simple Question About Conservation of Angular Momentum

Taking an academic pseudonym?

How should I ship cards?

Contribution form

Is the following statement true, false, or can't be determined? Why?

The totem pole can be grouped into

GPL - Is it required to post source code to the Public - when is a software released?

Can you make a Spell Glyph of a spell that has the potential to target more than one creature?

Was Opportunity's last message to Earth "My battery is low and it's getting dark"?

What happens if you declare more than $10,000 at the US border?

Why did Shae (falsely) implicate Sansa?

Is a particular string regular (e.g is '010') regular?

How to write pow math?

A dragon's soul trapped in a ring of mind shielding wants a new body; what magic could enable her to do so?

Which was the first story to feature space elevators?

How to play song that contains one guitar when we have two guitarists (or more)?

Why does Python copy numpy arrays where the length of the dimensions are the same?

How bad is a Computer Science course that doesn't teach Design Patterns?

How can guns be countered by melee combat without raw-ability or exceptional explanations?

How to draw these kind of adjacent ovals with arrows in latex?

Why did Tywin never remarry?

Discouraging missile alpha strikes

Why is Shelob considered evil?

How to know if I am a 'Real Developer'



Compilation of T-SQL oddities and pitfalls


Possible pitfalls of condensing 2 SQL Server instances into 1Database table and NULLsConditional compilation of SQL Server stored procedurePitfalls of renaming tables in sql serverAuthoritative source that <> and != are identical in performance in SQL ServerAre there any pitfalls to be aware of when using dynamic type-coercion in SQL Server?SQL Server 2014 - Strange SP behavior during compilationJOIN and concatenate between tables and UPDATEgMSAs and SQL Server 2017T-SQL DATEDIFF_BIG - 469ms diff with currentmills and documented start-end constraints incorrect?













1















Like all programming languages, T-SQL has its oddities when compared to a generic programming language. Some of these have tripped us up.



It would be nice if there were an already-compiled list so that we could be forewarned of these pitfalls before stepping into them. What are some T-SQL oddities that we need to be aware of?



We don't mean an exhaustive study of all the specific structures, functions, and constructions unique to the SQL language. We mean a concise list of the places where SQL diverges consequentially from other standard programming languages or from the usual and expected conventions of programming. Things that a reasonably skilled programmer coming from another language could be expected to stumble over.










share|improve this question




















  • 5





    You will likely need to modify the question so that it's actually a question, such as, "What are some T-SQL oddities that I need to be aware of?" and then post the content you have as an answer. As it is, it doesn't fit the model of a Q&A site.

    – Tony Hinkle
    6 hours ago






  • 1





    You can add in stuff about isnumeric, variables, and string concatenation, too.

    – Erik Darling
    5 hours ago






  • 1





    (4) and (5) should be placed at the top. The rest seem to be RDBMS specific.

    – Michael Kutz
    4 hours ago






  • 2





    @UnLogicGuys See meta.stackexchange.com/questions/11740/… This might be a good candidate for a Community Wiki answer.

    – Tony Hinkle
    3 hours ago








  • 2





    @UnLogicGuys please move it to an answer (and leave the question with the question part only). Do not worry, people will not misunderstand and if there are more to add (which I'm sur e there are), they'll happily add answers.

    – ypercubeᵀᴹ
    3 hours ago


















1















Like all programming languages, T-SQL has its oddities when compared to a generic programming language. Some of these have tripped us up.



It would be nice if there were an already-compiled list so that we could be forewarned of these pitfalls before stepping into them. What are some T-SQL oddities that we need to be aware of?



We don't mean an exhaustive study of all the specific structures, functions, and constructions unique to the SQL language. We mean a concise list of the places where SQL diverges consequentially from other standard programming languages or from the usual and expected conventions of programming. Things that a reasonably skilled programmer coming from another language could be expected to stumble over.










share|improve this question




















  • 5





    You will likely need to modify the question so that it's actually a question, such as, "What are some T-SQL oddities that I need to be aware of?" and then post the content you have as an answer. As it is, it doesn't fit the model of a Q&A site.

    – Tony Hinkle
    6 hours ago






  • 1





    You can add in stuff about isnumeric, variables, and string concatenation, too.

    – Erik Darling
    5 hours ago






  • 1





    (4) and (5) should be placed at the top. The rest seem to be RDBMS specific.

    – Michael Kutz
    4 hours ago






  • 2





    @UnLogicGuys See meta.stackexchange.com/questions/11740/… This might be a good candidate for a Community Wiki answer.

    – Tony Hinkle
    3 hours ago








  • 2





    @UnLogicGuys please move it to an answer (and leave the question with the question part only). Do not worry, people will not misunderstand and if there are more to add (which I'm sur e there are), they'll happily add answers.

    – ypercubeᵀᴹ
    3 hours ago
















1












1








1








Like all programming languages, T-SQL has its oddities when compared to a generic programming language. Some of these have tripped us up.



It would be nice if there were an already-compiled list so that we could be forewarned of these pitfalls before stepping into them. What are some T-SQL oddities that we need to be aware of?



We don't mean an exhaustive study of all the specific structures, functions, and constructions unique to the SQL language. We mean a concise list of the places where SQL diverges consequentially from other standard programming languages or from the usual and expected conventions of programming. Things that a reasonably skilled programmer coming from another language could be expected to stumble over.










share|improve this question
















Like all programming languages, T-SQL has its oddities when compared to a generic programming language. Some of these have tripped us up.



It would be nice if there were an already-compiled list so that we could be forewarned of these pitfalls before stepping into them. What are some T-SQL oddities that we need to be aware of?



We don't mean an exhaustive study of all the specific structures, functions, and constructions unique to the SQL language. We mean a concise list of the places where SQL diverges consequentially from other standard programming languages or from the usual and expected conventions of programming. Things that a reasonably skilled programmer coming from another language could be expected to stumble over.







sql-server t-sql sql-server-2017 null operator






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago







UnLogicGuys

















asked 6 hours ago









UnLogicGuysUnLogicGuys

15718




15718








  • 5





    You will likely need to modify the question so that it's actually a question, such as, "What are some T-SQL oddities that I need to be aware of?" and then post the content you have as an answer. As it is, it doesn't fit the model of a Q&A site.

    – Tony Hinkle
    6 hours ago






  • 1





    You can add in stuff about isnumeric, variables, and string concatenation, too.

    – Erik Darling
    5 hours ago






  • 1





    (4) and (5) should be placed at the top. The rest seem to be RDBMS specific.

    – Michael Kutz
    4 hours ago






  • 2





    @UnLogicGuys See meta.stackexchange.com/questions/11740/… This might be a good candidate for a Community Wiki answer.

    – Tony Hinkle
    3 hours ago








  • 2





    @UnLogicGuys please move it to an answer (and leave the question with the question part only). Do not worry, people will not misunderstand and if there are more to add (which I'm sur e there are), they'll happily add answers.

    – ypercubeᵀᴹ
    3 hours ago
















  • 5





    You will likely need to modify the question so that it's actually a question, such as, "What are some T-SQL oddities that I need to be aware of?" and then post the content you have as an answer. As it is, it doesn't fit the model of a Q&A site.

    – Tony Hinkle
    6 hours ago






  • 1





    You can add in stuff about isnumeric, variables, and string concatenation, too.

    – Erik Darling
    5 hours ago






  • 1





    (4) and (5) should be placed at the top. The rest seem to be RDBMS specific.

    – Michael Kutz
    4 hours ago






  • 2





    @UnLogicGuys See meta.stackexchange.com/questions/11740/… This might be a good candidate for a Community Wiki answer.

    – Tony Hinkle
    3 hours ago








  • 2





    @UnLogicGuys please move it to an answer (and leave the question with the question part only). Do not worry, people will not misunderstand and if there are more to add (which I'm sur e there are), they'll happily add answers.

    – ypercubeᵀᴹ
    3 hours ago










5




5





You will likely need to modify the question so that it's actually a question, such as, "What are some T-SQL oddities that I need to be aware of?" and then post the content you have as an answer. As it is, it doesn't fit the model of a Q&A site.

– Tony Hinkle
6 hours ago





You will likely need to modify the question so that it's actually a question, such as, "What are some T-SQL oddities that I need to be aware of?" and then post the content you have as an answer. As it is, it doesn't fit the model of a Q&A site.

– Tony Hinkle
6 hours ago




1




1





You can add in stuff about isnumeric, variables, and string concatenation, too.

– Erik Darling
5 hours ago





You can add in stuff about isnumeric, variables, and string concatenation, too.

– Erik Darling
5 hours ago




1




1





(4) and (5) should be placed at the top. The rest seem to be RDBMS specific.

– Michael Kutz
4 hours ago





(4) and (5) should be placed at the top. The rest seem to be RDBMS specific.

– Michael Kutz
4 hours ago




2




2





@UnLogicGuys See meta.stackexchange.com/questions/11740/… This might be a good candidate for a Community Wiki answer.

– Tony Hinkle
3 hours ago







@UnLogicGuys See meta.stackexchange.com/questions/11740/… This might be a good candidate for a Community Wiki answer.

– Tony Hinkle
3 hours ago






2




2





@UnLogicGuys please move it to an answer (and leave the question with the question part only). Do not worry, people will not misunderstand and if there are more to add (which I'm sur e there are), they'll happily add answers.

– ypercubeᵀᴹ
3 hours ago







@UnLogicGuys please move it to an answer (and leave the question with the question part only). Do not worry, people will not misunderstand and if there are more to add (which I'm sur e there are), they'll happily add answers.

– ypercubeᵀᴹ
3 hours ago












1 Answer
1






active

oldest

votes


















1














Below, we have started a list of the kind of examples we have in mind:





  1. Trailing spaces are ignored by some functions and operators, such as LEN() and equality. The first query returns 5, 5, 5, 5, 5, 5. The second query returns EQUAL. These results can be quite surprising to a new SQL programmer.




SELECT   LEN('Hello') AS [0 trailing spaces],
LEN('Hello ') AS [1 trailing spaces],
LEN('Hello ') AS [2 trailing spaces],
LEN('Hello ') AS [3 trailing spaces],
LEN('Hello ') AS [4 trailing spaces],
LEN('Hello ') AS [10 trailing spaces]
;


SELECT CASE
WHEN 'Hello' = 'Hello ' THEN 'EQUAL'
ELSE 'NOT EQUAL'
END
;




  1. Application of negative and positive signs has lower operator precedence than multiplication and division. This causes products preceded with negative signs to be evaluated first. Thus, the first expression in the query below yields 9.0, whereas the second expression yields -1.0.




SELECT   6.0/2.0*3.0,
6.0/-2.0*3.0
;




  1. Casting a blank string to integer, bigint, or float results in zero, whereas casting a blank string to decimal or numeric produces an error.




SELECT   CAST('' AS INT) AS [INT],
CAST('' AS BIGINT) AS [BIGINT],
CAST('' AS FLOAT) AS [FLOAT]
;

SELECT CAST('' AS DECIMAL(15,6)) AS [DECIMAL],
CAST('' AS NUMERIC(15,6)) AS [NUMERIC]
;




  1. SQL has one of the most strongly defined NULLs of any programming language. It nearly constitutes a third logic value (either "missing" or "non-applicable") in a three-valued logic, rather than merely an indicator of "uninitialized variable". A three-valued logic can produce surprising results to those who are more accustomed to two-valued logic. The queries below will not necessarily return every row in the table. Rows in which COL1 is NULL will not be returned.




SELECT   *
FROM EXAMPLE_TABLE
WHERE COL1 = 5
OR COL1 <> 5
;


SELECT *
FROM EXAMPLE_TABLE
WHERE COL1 = 5
OR NOT(COL1 = 5)
;




  1. The strongly-defined NULL mark and three-valued logic can cause bafflement in more subtle ways. The first query below will not return any rows, even when COL1 contains a value not in the excluded list (e.g., 6 or 7 or 8). The second query will return all rows, as expected, but the third query will never return any rows.




SELECT   *
FROM EXAMPLE_TABLE
WHERE COL1 NOT IN (1, 2, 3, 4, 5, NULL)
;



SELECT *
FROM EXAMPLE_TABLE
WHERE 1 = 1
;



SELECT *
FROM EXAMPLE_TABLE
WHERE NULL = NULL
;




  1. You'd better be sure you understand scale and precision and how they interact. Otherwise, the query below will make you recoil in horror.




SELECT   CAST(0.0000006 AS DECIMAL(38,22)) * CAST(1.000000 AS DECIMAL(38,22))
;





share|improve this answer

























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "182"
    };
    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: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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%2fdba.stackexchange.com%2fquestions%2f230163%2fcompilation-of-t-sql-oddities-and-pitfalls%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Below, we have started a list of the kind of examples we have in mind:





    1. Trailing spaces are ignored by some functions and operators, such as LEN() and equality. The first query returns 5, 5, 5, 5, 5, 5. The second query returns EQUAL. These results can be quite surprising to a new SQL programmer.




    SELECT   LEN('Hello') AS [0 trailing spaces],
    LEN('Hello ') AS [1 trailing spaces],
    LEN('Hello ') AS [2 trailing spaces],
    LEN('Hello ') AS [3 trailing spaces],
    LEN('Hello ') AS [4 trailing spaces],
    LEN('Hello ') AS [10 trailing spaces]
    ;


    SELECT CASE
    WHEN 'Hello' = 'Hello ' THEN 'EQUAL'
    ELSE 'NOT EQUAL'
    END
    ;




    1. Application of negative and positive signs has lower operator precedence than multiplication and division. This causes products preceded with negative signs to be evaluated first. Thus, the first expression in the query below yields 9.0, whereas the second expression yields -1.0.




    SELECT   6.0/2.0*3.0,
    6.0/-2.0*3.0
    ;




    1. Casting a blank string to integer, bigint, or float results in zero, whereas casting a blank string to decimal or numeric produces an error.




    SELECT   CAST('' AS INT) AS [INT],
    CAST('' AS BIGINT) AS [BIGINT],
    CAST('' AS FLOAT) AS [FLOAT]
    ;

    SELECT CAST('' AS DECIMAL(15,6)) AS [DECIMAL],
    CAST('' AS NUMERIC(15,6)) AS [NUMERIC]
    ;




    1. SQL has one of the most strongly defined NULLs of any programming language. It nearly constitutes a third logic value (either "missing" or "non-applicable") in a three-valued logic, rather than merely an indicator of "uninitialized variable". A three-valued logic can produce surprising results to those who are more accustomed to two-valued logic. The queries below will not necessarily return every row in the table. Rows in which COL1 is NULL will not be returned.




    SELECT   *
    FROM EXAMPLE_TABLE
    WHERE COL1 = 5
    OR COL1 <> 5
    ;


    SELECT *
    FROM EXAMPLE_TABLE
    WHERE COL1 = 5
    OR NOT(COL1 = 5)
    ;




    1. The strongly-defined NULL mark and three-valued logic can cause bafflement in more subtle ways. The first query below will not return any rows, even when COL1 contains a value not in the excluded list (e.g., 6 or 7 or 8). The second query will return all rows, as expected, but the third query will never return any rows.




    SELECT   *
    FROM EXAMPLE_TABLE
    WHERE COL1 NOT IN (1, 2, 3, 4, 5, NULL)
    ;



    SELECT *
    FROM EXAMPLE_TABLE
    WHERE 1 = 1
    ;



    SELECT *
    FROM EXAMPLE_TABLE
    WHERE NULL = NULL
    ;




    1. You'd better be sure you understand scale and precision and how they interact. Otherwise, the query below will make you recoil in horror.




    SELECT   CAST(0.0000006 AS DECIMAL(38,22)) * CAST(1.000000 AS DECIMAL(38,22))
    ;





    share|improve this answer






























      1














      Below, we have started a list of the kind of examples we have in mind:





      1. Trailing spaces are ignored by some functions and operators, such as LEN() and equality. The first query returns 5, 5, 5, 5, 5, 5. The second query returns EQUAL. These results can be quite surprising to a new SQL programmer.




      SELECT   LEN('Hello') AS [0 trailing spaces],
      LEN('Hello ') AS [1 trailing spaces],
      LEN('Hello ') AS [2 trailing spaces],
      LEN('Hello ') AS [3 trailing spaces],
      LEN('Hello ') AS [4 trailing spaces],
      LEN('Hello ') AS [10 trailing spaces]
      ;


      SELECT CASE
      WHEN 'Hello' = 'Hello ' THEN 'EQUAL'
      ELSE 'NOT EQUAL'
      END
      ;




      1. Application of negative and positive signs has lower operator precedence than multiplication and division. This causes products preceded with negative signs to be evaluated first. Thus, the first expression in the query below yields 9.0, whereas the second expression yields -1.0.




      SELECT   6.0/2.0*3.0,
      6.0/-2.0*3.0
      ;




      1. Casting a blank string to integer, bigint, or float results in zero, whereas casting a blank string to decimal or numeric produces an error.




      SELECT   CAST('' AS INT) AS [INT],
      CAST('' AS BIGINT) AS [BIGINT],
      CAST('' AS FLOAT) AS [FLOAT]
      ;

      SELECT CAST('' AS DECIMAL(15,6)) AS [DECIMAL],
      CAST('' AS NUMERIC(15,6)) AS [NUMERIC]
      ;




      1. SQL has one of the most strongly defined NULLs of any programming language. It nearly constitutes a third logic value (either "missing" or "non-applicable") in a three-valued logic, rather than merely an indicator of "uninitialized variable". A three-valued logic can produce surprising results to those who are more accustomed to two-valued logic. The queries below will not necessarily return every row in the table. Rows in which COL1 is NULL will not be returned.




      SELECT   *
      FROM EXAMPLE_TABLE
      WHERE COL1 = 5
      OR COL1 <> 5
      ;


      SELECT *
      FROM EXAMPLE_TABLE
      WHERE COL1 = 5
      OR NOT(COL1 = 5)
      ;




      1. The strongly-defined NULL mark and three-valued logic can cause bafflement in more subtle ways. The first query below will not return any rows, even when COL1 contains a value not in the excluded list (e.g., 6 or 7 or 8). The second query will return all rows, as expected, but the third query will never return any rows.




      SELECT   *
      FROM EXAMPLE_TABLE
      WHERE COL1 NOT IN (1, 2, 3, 4, 5, NULL)
      ;



      SELECT *
      FROM EXAMPLE_TABLE
      WHERE 1 = 1
      ;



      SELECT *
      FROM EXAMPLE_TABLE
      WHERE NULL = NULL
      ;




      1. You'd better be sure you understand scale and precision and how they interact. Otherwise, the query below will make you recoil in horror.




      SELECT   CAST(0.0000006 AS DECIMAL(38,22)) * CAST(1.000000 AS DECIMAL(38,22))
      ;





      share|improve this answer




























        1












        1








        1







        Below, we have started a list of the kind of examples we have in mind:





        1. Trailing spaces are ignored by some functions and operators, such as LEN() and equality. The first query returns 5, 5, 5, 5, 5, 5. The second query returns EQUAL. These results can be quite surprising to a new SQL programmer.




        SELECT   LEN('Hello') AS [0 trailing spaces],
        LEN('Hello ') AS [1 trailing spaces],
        LEN('Hello ') AS [2 trailing spaces],
        LEN('Hello ') AS [3 trailing spaces],
        LEN('Hello ') AS [4 trailing spaces],
        LEN('Hello ') AS [10 trailing spaces]
        ;


        SELECT CASE
        WHEN 'Hello' = 'Hello ' THEN 'EQUAL'
        ELSE 'NOT EQUAL'
        END
        ;




        1. Application of negative and positive signs has lower operator precedence than multiplication and division. This causes products preceded with negative signs to be evaluated first. Thus, the first expression in the query below yields 9.0, whereas the second expression yields -1.0.




        SELECT   6.0/2.0*3.0,
        6.0/-2.0*3.0
        ;




        1. Casting a blank string to integer, bigint, or float results in zero, whereas casting a blank string to decimal or numeric produces an error.




        SELECT   CAST('' AS INT) AS [INT],
        CAST('' AS BIGINT) AS [BIGINT],
        CAST('' AS FLOAT) AS [FLOAT]
        ;

        SELECT CAST('' AS DECIMAL(15,6)) AS [DECIMAL],
        CAST('' AS NUMERIC(15,6)) AS [NUMERIC]
        ;




        1. SQL has one of the most strongly defined NULLs of any programming language. It nearly constitutes a third logic value (either "missing" or "non-applicable") in a three-valued logic, rather than merely an indicator of "uninitialized variable". A three-valued logic can produce surprising results to those who are more accustomed to two-valued logic. The queries below will not necessarily return every row in the table. Rows in which COL1 is NULL will not be returned.




        SELECT   *
        FROM EXAMPLE_TABLE
        WHERE COL1 = 5
        OR COL1 <> 5
        ;


        SELECT *
        FROM EXAMPLE_TABLE
        WHERE COL1 = 5
        OR NOT(COL1 = 5)
        ;




        1. The strongly-defined NULL mark and three-valued logic can cause bafflement in more subtle ways. The first query below will not return any rows, even when COL1 contains a value not in the excluded list (e.g., 6 or 7 or 8). The second query will return all rows, as expected, but the third query will never return any rows.




        SELECT   *
        FROM EXAMPLE_TABLE
        WHERE COL1 NOT IN (1, 2, 3, 4, 5, NULL)
        ;



        SELECT *
        FROM EXAMPLE_TABLE
        WHERE 1 = 1
        ;



        SELECT *
        FROM EXAMPLE_TABLE
        WHERE NULL = NULL
        ;




        1. You'd better be sure you understand scale and precision and how they interact. Otherwise, the query below will make you recoil in horror.




        SELECT   CAST(0.0000006 AS DECIMAL(38,22)) * CAST(1.000000 AS DECIMAL(38,22))
        ;





        share|improve this answer















        Below, we have started a list of the kind of examples we have in mind:





        1. Trailing spaces are ignored by some functions and operators, such as LEN() and equality. The first query returns 5, 5, 5, 5, 5, 5. The second query returns EQUAL. These results can be quite surprising to a new SQL programmer.




        SELECT   LEN('Hello') AS [0 trailing spaces],
        LEN('Hello ') AS [1 trailing spaces],
        LEN('Hello ') AS [2 trailing spaces],
        LEN('Hello ') AS [3 trailing spaces],
        LEN('Hello ') AS [4 trailing spaces],
        LEN('Hello ') AS [10 trailing spaces]
        ;


        SELECT CASE
        WHEN 'Hello' = 'Hello ' THEN 'EQUAL'
        ELSE 'NOT EQUAL'
        END
        ;




        1. Application of negative and positive signs has lower operator precedence than multiplication and division. This causes products preceded with negative signs to be evaluated first. Thus, the first expression in the query below yields 9.0, whereas the second expression yields -1.0.




        SELECT   6.0/2.0*3.0,
        6.0/-2.0*3.0
        ;




        1. Casting a blank string to integer, bigint, or float results in zero, whereas casting a blank string to decimal or numeric produces an error.




        SELECT   CAST('' AS INT) AS [INT],
        CAST('' AS BIGINT) AS [BIGINT],
        CAST('' AS FLOAT) AS [FLOAT]
        ;

        SELECT CAST('' AS DECIMAL(15,6)) AS [DECIMAL],
        CAST('' AS NUMERIC(15,6)) AS [NUMERIC]
        ;




        1. SQL has one of the most strongly defined NULLs of any programming language. It nearly constitutes a third logic value (either "missing" or "non-applicable") in a three-valued logic, rather than merely an indicator of "uninitialized variable". A three-valued logic can produce surprising results to those who are more accustomed to two-valued logic. The queries below will not necessarily return every row in the table. Rows in which COL1 is NULL will not be returned.




        SELECT   *
        FROM EXAMPLE_TABLE
        WHERE COL1 = 5
        OR COL1 <> 5
        ;


        SELECT *
        FROM EXAMPLE_TABLE
        WHERE COL1 = 5
        OR NOT(COL1 = 5)
        ;




        1. The strongly-defined NULL mark and three-valued logic can cause bafflement in more subtle ways. The first query below will not return any rows, even when COL1 contains a value not in the excluded list (e.g., 6 or 7 or 8). The second query will return all rows, as expected, but the third query will never return any rows.




        SELECT   *
        FROM EXAMPLE_TABLE
        WHERE COL1 NOT IN (1, 2, 3, 4, 5, NULL)
        ;



        SELECT *
        FROM EXAMPLE_TABLE
        WHERE 1 = 1
        ;



        SELECT *
        FROM EXAMPLE_TABLE
        WHERE NULL = NULL
        ;




        1. You'd better be sure you understand scale and precision and how they interact. Otherwise, the query below will make you recoil in horror.




        SELECT   CAST(0.0000006 AS DECIMAL(38,22)) * CAST(1.000000 AS DECIMAL(38,22))
        ;






        share|improve this answer














        share|improve this answer



        share|improve this answer








        answered 1 hour ago


























        community wiki





        UnLogicGuys































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Database Administrators Stack Exchange!


            • 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%2fdba.stackexchange.com%2fquestions%2f230163%2fcompilation-of-t-sql-oddities-and-pitfalls%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







            Popular posts from this blog

            Anexo:Material bélico de la Fuerza Aérea de Chile Índice Aeronaves Defensa...

            Always On Availability groups resolving state after failover - Remote harden of transaction...

            update json value to null Announcing the arrival of Valued Associate #679: Cesar Manara ...