Compilation of SQL oddities and pitfallsPossible pitfalls of condensing 2 SQL Server instances into 1Database...

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

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

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

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

Which was the first story to feature space elevators?

Why Third 'Reich'? Why is 'reich' not translated when 'third' is? What is the English synonym of reich?

Can a planet be tidally unlocked?

Diagram in Tikz environment

Taking an academic pseudonym?

Should corporate security training be tailored based on a users' job role?

How to write a character overlapping another character

Unable to login to ec2 instance after running “sudo chmod 2770 /”

Are there any rules or guidelines about the order of saving throws?

Does limiting the number of sources help simplify the game for a new DM with new and experienced players?

What have we got?

What prevents people from lying about where they live in order to reduce state income taxes?

Why would you use 2 alternate layout buttons instead of 1, when only one can be selected at once

Would life expectancy increase if we replaced healthy organs with artificial ones?

How to not forget my phone in the bathroom?

Why does the current not skip resistors R3 and R5 when R6 and R4 have no resistance?

Coworker is trying to get me to sign his petition to run for office. How to decline politely?

"Cheaper by the dozen" phrase origin?

Run a command that requires sudo after a time has passed

Why don't hotels offer (at least) 1 kitchen bookable by any guest?



Compilation of SQL oddities and pitfalls


Possible pitfalls of condensing 2 SQL Server instances into 1Database table and NULLsConditional compilation of SQL Server stored procedureWhat is the logic behind ISNUMERIC for certain special characters?Select all records, join with table A if join exists, table B if notPitfalls of renaming tables in sql serverMeasure compilation time of stored procedurePerformance gap between WHERE IN (1,2,3,4) vs IN (select * from STRING_SPLIT('1,2,3,4',','))SQL Server 2014 - Strange SP behavior during compilationgMSAs and SQL Server 2017













0















Like all programming languages, 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.



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 NULL value of any programming language. It nearly constitutes a third logic value ("unknown") 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 value 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 question




















  • 4





    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
    3 hours ago






  • 1





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

    – Erik Darling
    2 hours ago






  • 1





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

    – Michael Kutz
    1 hour ago











  • @Tony Hinkle — Thanks for the tip! We'll highlight the question and use your wording. However, we are hesitant to move our list of examples into an answer, lest people think the question has already been satisfactorily answered. They're just meant as illustrations of what we had in mind.

    – UnLogicGuys
    55 mins ago






  • 1





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

    – Tony Hinkle
    49 mins ago


















0















Like all programming languages, 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.



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 NULL value of any programming language. It nearly constitutes a third logic value ("unknown") 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 value 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 question




















  • 4





    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
    3 hours ago






  • 1





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

    – Erik Darling
    2 hours ago






  • 1





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

    – Michael Kutz
    1 hour ago











  • @Tony Hinkle — Thanks for the tip! We'll highlight the question and use your wording. However, we are hesitant to move our list of examples into an answer, lest people think the question has already been satisfactorily answered. They're just meant as illustrations of what we had in mind.

    – UnLogicGuys
    55 mins ago






  • 1





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

    – Tony Hinkle
    49 mins ago
















0












0








0








Like all programming languages, 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.



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 NULL value of any programming language. It nearly constitutes a third logic value ("unknown") 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 value 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 question
















Like all programming languages, 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.



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 NULL value of any programming language. It nearly constitutes a third logic value ("unknown") 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 value 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))
;






sql-server sql-server-2017 null operator






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 54 mins ago







UnLogicGuys

















asked 3 hours ago









UnLogicGuysUnLogicGuys

15217




15217








  • 4





    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
    3 hours ago






  • 1





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

    – Erik Darling
    2 hours ago






  • 1





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

    – Michael Kutz
    1 hour ago











  • @Tony Hinkle — Thanks for the tip! We'll highlight the question and use your wording. However, we are hesitant to move our list of examples into an answer, lest people think the question has already been satisfactorily answered. They're just meant as illustrations of what we had in mind.

    – UnLogicGuys
    55 mins ago






  • 1





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

    – Tony Hinkle
    49 mins ago
















  • 4





    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
    3 hours ago






  • 1





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

    – Erik Darling
    2 hours ago






  • 1





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

    – Michael Kutz
    1 hour ago











  • @Tony Hinkle — Thanks for the tip! We'll highlight the question and use your wording. However, we are hesitant to move our list of examples into an answer, lest people think the question has already been satisfactorily answered. They're just meant as illustrations of what we had in mind.

    – UnLogicGuys
    55 mins ago






  • 1





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

    – Tony Hinkle
    49 mins ago










4




4





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
3 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
3 hours ago




1




1





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

– Erik Darling
2 hours ago





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

– Erik Darling
2 hours ago




1




1





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

– Michael Kutz
1 hour ago





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

– Michael Kutz
1 hour ago













@Tony Hinkle — Thanks for the tip! We'll highlight the question and use your wording. However, we are hesitant to move our list of examples into an answer, lest people think the question has already been satisfactorily answered. They're just meant as illustrations of what we had in mind.

– UnLogicGuys
55 mins ago





@Tony Hinkle — Thanks for the tip! We'll highlight the question and use your wording. However, we are hesitant to move our list of examples into an answer, lest people think the question has already been satisfactorily answered. They're just meant as illustrations of what we had in mind.

– UnLogicGuys
55 mins ago




1




1





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

– Tony Hinkle
49 mins ago







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

– Tony Hinkle
49 mins ago












0






active

oldest

votes











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-sql-oddities-and-pitfalls%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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-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

ORA-01691 (unable to extend lob segment) even though my tablespace has AUTOEXTEND onORA-01692: unable to...

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

Circunscripción electoral de Guipúzcoa Referencias Menú de navegaciónLas claves del sistema electoral en...