Len() vs datalength() in SQL Server 2005

前端 未结 6 899
失恋的感觉
失恋的感觉 2020-12-08 19:36

Recently I faced a issue when using len() in a query to find out the length of a query, len() was not counting the trailing spaces in the value. Bu

6条回答
  •  [愿得一人]
    2020-12-08 19:50

    len counts the number of characters used not the storage required, this will be even more evident when you use nvarchar instead of varchar

    len does not count trailing spaces either

    take a look at this

    declare @v nchar(5)
    select @v ='ABC  '
    
    
    select len(@v),datalength(@v)
    

    and the output for len is 3 while the output for datalength =10

提交回复
热议问题