I am using SQL Server 2008 & 2005 (Express). I’m trying to extract part of an alpha numeric string from a varchar field.
RIGHT(str_field, 3) yields null values but SUBSTRING(str_field, LEN(str_field)-2, LEN(str_field)) gives the right value. LEFT(str_field, 7) gives the expected values. What gives?
I would have thought that RIGHT(str_field, 3) and SUBSTRING(str_field, LEN(str_field)-2, LEN(str_field)) are equivalent expressions.
You have trailing spaces
RIGHT will yield spaces but LEN ignores trailing spaces
See SET ANSI_PADDING
Note: you won’t get NULL for non NULL input…