Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the operator >+ differ from >= in SQL Server 2012

Entirely by accident today I was running a SQL statement to filter some items by date, for simplicity sake we'll say I used

SELECT * 
FROM [TableName] 
WHERE [RecordCreated] >+ '2016-04-10'

Only after the statement ran I realised I had used >+ instead of >=, now I was confused as I would have expected an error.

I tried a couple of other variations such as

>- -- Throws an error
<+ -- Ran successfully
<- -- Throws an error

The count of rows returned was exactly the same whether I used >= or >+

After searching online I couldn't find any documentation that covered this syntax directly, only when the two operators are used apart.

The RecordCreated column is a datetime.

Is this just a nicety in syntax for a possible common mistake or is it potentially trying to cast the date as a numeric value?

like image 819
samuelmr Avatar asked Apr 19 '16 10:04

samuelmr


People also ask

Can you use >= in SQL?

You can use the SQL greater than or equal to comparison with other data types, such as VARCHAR and DATETIME.

What is the difference between <> and != In SQL?

Difference between SQL Not Equal Operator <> and != to do inequality test between two expressions. Both operators give the same output. The only difference is that '<>' is in line with the ISO standard while '!= ' does not follow ISO standard.

How use greater than or equal to in SQL query?

In SQL, you can use the >= operator to test for an expression greater than or equal to. Let's use the same customers table as the previous example. In this example, the SELECT statement would return all rows from the customers table where the customer_id is greater than or equal to 6000.


1 Answers

This seems to be a bug with '+' operator.

As per the updates from Microsoft team,

After some investigation, this behavior is by design since + is an unary operator. So the parser accepts "+ , and the '+' is simply ignored in this case. Changing this behavior has lot of backward compatibility implications so we don't intend to change it & the fix will introduce unnecessary changes for application code.

You can find a really good answer by RGO to his own question here.

like image 170
an33sh Avatar answered Oct 19 '22 23:10

an33sh