Search code examples
sql-serversql-server-2008datalength

varChar and Char behave different on cast


Why the output is different of this query

SELECT DATALENGTH(CAST('test' AS VARCHAR)), DATALENGTH(CAST('test' AS CHAR))

outPut:

4,30


Solution

  • VarChar always adjusts to the length of the string passed. hence the output is 4.

    CHAR has default length of 30 and hence the output is 30.