views:

71

answers:

2

Hi, I usually defining size when declaring parameters in my SP, like :

@myParam nvarchar(size)

or when I casting or converting:

CAST(@myParam AS nvarchar(size))

Recently I removed size from my CAST functions like:

CAST(@myParam AS nvarchar)

and I'm bit worried if that is going to come and bite me when least expected :-(, since I noticed truncation on nvarchar variables when using recursive CTE and casting nvarchar without specifying size.

Any comments?

+4  A: 

If you omit the size, it defaults to 30. See this:

http://msdn.microsoft.com/en-us/library/ms186939.aspx

David M
thanks, can you provide some link?
krul
thanks a lot, that clears things up:When n is not specified in a data definition or variable declaration statement, the default length is 1. When n is not specified with the CAST function, the default length is 30.
krul
+1  A: 

It will set the size to the default size and truncate the rest. This will indeed likely come to bite you, unless the default size is appropriate.

Event then, I would suggest you always specify the size to make it clear what you think the size will be, instead of having the next person that reads to code have to come to SO to ask what the default size of an nvarchar is ;).

Yishai