Skip to content

SA0184 : Redundant pairs of parentheses can be removed

Redundant parentheses in SQL expressions can hinder readability and maintainability, potentially leading to confusion in query interpretation.

Redundant parentheses in SQL expressions can be misleading. Although they often do not affect the execution of queries, they can make the code harder to read and maintain, leading to confusion. This is particularly relevant in SQL Server, where query readability is vital for ongoing maintenance and collaboration among developers.

For example:

-- Example of a query with redundant parentheses
SELECT ((column1 + column2) * column3) FROM TableName;

In the above example, the outer parentheses around the arithmetic operation are unnecessary and do not change the evaluation order. This can obscure the logic of the query, especially for complex calculations or conditions.

  • Redundant parentheses can make the query seem more complex than it is, potentially leading to misunderstanding.

  • Unnecessary parentheses may hinder performance tuning and understanding of the intended logic, especially in large and complex queries.

Eliminate redundant parentheses from logical and arithmetic expressions to enhance readability and maintainability of T-SQL queries.Follow these steps to address the issue:Carefully review the query and identify any sets of parentheses that do not influence the logical or arithmetic operations within the expression. Look for expressions like ((column1 + column2) * column3).Remove the unnecessary parentheses while ensuring that the logical order of operations remains unchanged. For the above expression, this would mean simplifying it to (column1 + column2) * column3.Test the query after modifications to confirm that it produces the same results and maintains the intended logic. This ensures there are no side effects from the removal of parentheses.For example: -- Example of a corrected query SELECT (column1 + column2) * column3 FROM TableName;

The rule has a Batch scope and is applied only on the SQL script.

NameDescriptionDefault Value
IgnoreArithmeticExpressionMainParenthesesSpecifies whether the main parentheses, which wrap an arithmetic expression to be ignored.yes
IgnoreBooleanExpressionMainParenthesesSpecifies whether the main parentheses, which wrap an logical expression to be ignored.yes

The rule does not need Analysis Context or SQL Connection.

2 minutes per issue.

Design Rules, Code Smells

There is no additional info for this rule.

CREATE TABLE Test.Greeting
(
GreetingId INT IDENTITY (1,1) PRIMARY KEY,
Message nvarchar(255) NOT NULL,
)
INSERT INTO Test.Greeting (Message)
SELECT ('Hello, world!')
INSERT INTO Test.Greeting (Message)
VALUES ('How do yo do?'),
('Good morning!'),
('Good night!')
DELETE Test.Greeting WHERE (GreetingId = 3 and (aaa != 0 and ttt = 12) and a <1) or a = 1
select (6/(7/5)*12) + 1
select ((1 + 2) - 3) * 4 / 5 -(6 /(7/5)*12 )
SELECT lower('AA'),(g.col1 + (g.col2)) FROM Test.Greeting g
WHERE
g.Message like ('Hello%')
or g.Message in (((((select message from UserMessges)))))
DROP TABLE Test.Greeting
declare @a int = 5
DECLARE @b INT
set @b = (@a / 2 + 1);
IF (@b > 0) AND ((@b+@a > 0))
BEGIN
print '1'
END
set @b = (@a / 3 + 1);
IF (@b > 0)
BEGIN
print '2'
END
IF (@b > 0) AND (@b-@a > 0) AND (@b-@a) > 0
BEGIN
print '2'
END
CREATE TABLE Test.Greeting
(
GreetingId INT IDENTITY (1,1) PRIMARY KEY,
Message nvarchar(255) NOT NULL,
)
INSERT INTO Test.Greeting (Message)
SELECT ('Hello, world!')
INSERT INTO Test.Greeting (Message)
VALUES ('How do yo do?'),
('Good morning!'),
('Good night!')
DELETE Test.Greeting WHERE (GreetingId = 3 and aaa != 0 and ttt = 12 and a <1) or a = 1
select (6/ 7/5*12) + 1
select ( 1 + 2 - 3) * 4 / 5 -(6 / 7/5*12 )
SELECT lower('AA'),(g.col1 + g.col2) FROM Test.Greeting g
WHERE
g.Message like 'Hello%'
or g.Message in ( (((select message from UserMessges))))
DROP TABLE Test.Greeting
declare @a int = 5
DECLARE @b INT
set @b = (@a / 2 + 1);
IF (@b > 0) AND ( @b+@a > 0)
BEGIN
print '1'
END
set @b = (@a / 3 + 1);
IF (@b > 0)
BEGIN
print '2'
END
IF (@b > 0) AND (@b-@a > 0) AND (@b-@a) > 0
BEGIN
print '2'
END
 MessageLineColumn
1SA0184 : Redundant pairs of parentheses can be removed.1548
2SA0184 : Redundant pairs of parentheses can be removed.1710
3SA0184 : Redundant pairs of parentheses can be removed.188
4SA0184 : Redundant pairs of parentheses can be removed.1835
5SA0184 : Redundant pairs of parentheses can be removed.2215
6SA0184 : Redundant pairs of parentheses can be removed.2029
7SA0184 : Redundant pairs of parentheses can be removed.2317
8SA0184 : Redundant pairs of parentheses can be removed.3017

Analysis Rules