would someone please explain why select len(0x0a000b) returns 3? does len count bytes? and why select left(0x0a000b, 1) returns nothing? i expected 0x0a (if len counts bytes)...
i am using mssql 2005
thanks konstantin
select len(0x0a000b)
is returning the length of a string represented by the three bytes 0x0a, 0x00, and 0x0b.
select left(0x0a000b, 1)
returns the left-most character of the string, which is a newline character.
Note that select case when left(0x0a000b, 1) = 0x0a then 1 else 0 end
returns 1
, which indicates you are, indeed, getting the newline character.
Edit: Please see the comments below for additional details.
left is the string operator and so it doesn't work on binary data in the way you expect it to work, use substring(@v, 1, 1) instead
len, on the other hand, returns the length of binary data (apart of other data types); although, there's a very interesting exception to strings, when we measure the length of a string using this command it first performs rtrim on it; so len('a ') would return 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With