Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does SQL Server automatically trim nvarchar fields upon query?

I have this query :

select '[' + p.Firstname + ']' from Person p
where p.Firstname = 'Johanne'

In the table, I have multiple personne who have this firstname, and some have a trailing space on the value (bad insertion of the values, it will be corrected).

Why then does this query bring me this result (I inserted the brackets to visualize the spaces) :

[Johanne]
[Johanne ]
[Johanne ]
[Johanne]

Is this a configuration thing ? The real query comes from entity framework 6, but this example does it also. How can I prevent it ?

Thanks !

Edit: I could make it work using EF6 and the System.Data.Entity.SqlServer.SqlFunctions.DataLength method like this:

ctx.Person.FirstOrDefault(p => p.FirstName == "Johanne" && SqlFunctions.DataLength(p.FirstName) == "Johanne".Length);
like image 999
Shimrod Avatar asked Feb 23 '15 20:02

Shimrod


People also ask

Does SQL auto TRIM trailing spaces?

SQL Server TRIM() Function The TRIM() function removes the space character OR other specified characters from the start or end of a string. By default, the TRIM() function removes leading and trailing spaces from a string.

Is nvarchar max dynamic?

It is not possible to use Dynamic Mapping when the Data Object contains the datatype as varchar(max) or nvarchar(max).

How do I get nvarchar length in SQL Server?

But if the storage length is desired, you can drag the table name into the query window using SSMS, highlight it, and use 'Alt-F1' to see the defined lengths of each column. If you insert ASCII characters into the varchar and nvarchar fields, it will allow you to put 10 characters into all of them.

When was TRIM introduced in SQL Server?

The TRIM function is used to remove trailing and leading spaces (char(32)) from a string of characters. This was introduced with SQL Server 2017.


1 Answers

SQL Server follows the ANSI/ISO SQL-92 specification (Section 8.2, , General rules #3) on how to compare strings with spaces. The ANSI standard requires padding for the character strings used in comparisons so that their lengths match before comparing them. The padding directly affects the semantics of WHERE and HAVING clause predicates and other Transact-SQL string comparisons. For example, Transact-SQL considers the strings 'abc' and 'abc ' to be equivalent for most comparison operations.

The only exception to this rule is the LIKE predicate. When the right side of a LIKE predicate expression features a value with a trailing space, SQL Server does not pad the two values to the same length before the comparison occurs. Because the purpose of the LIKE predicate, by definition, is to facilitate pattern searches rather than simple string equality tests, this does not violate the section of the ANSI SQL-92 specification mentioned earlier.

See: https://support.microsoft.com/en-us/topic/inf-how-sql-server-compares-strings-with-trailing-spaces-b62b1a2d-27d3-4260-216d-a605719003b0

like image 140
Anthony Horne Avatar answered Sep 23 '22 19:09

Anthony Horne