Wednesday, May 6, 2009

Important difference between NCHAR and NVARCHAR

When you are entering[INSERTing] data into a NCHAR column, database will pad spaces to the passed data to make it fixed length.Suppose your column is 'name NCHAR(30)', and you inserted "itzerp", then database will pad spaces at the end to make it 30 characters length.Here
SELECT * FROM EMP WHERE name=
"itzerp" and
SELECT * FROM EMP WHERE name=
"itzerp " and
SELECT * FROM EMP WHERE name=
"itzerp    " are same and will return you the same row.

Now if the column NAME is of
NVARCHAR type, then
SELECT * FROM EMP WHERE name=
"itzerp" and
SELECT * FROM EMP WHERE name=
"itzerp   "  are different

No comments: