Copy CodeThe code is as follows:
CREATE table TB (ID int, value varchar (10))
INSERT into TB values (1, "AA")
INSERT into TB values (1, "BB")
INSERT into TB values (2, "AAA")
INSERT into TB values (2, "BBB")
INSERT into TB values (2, "CCC")
Go
Create function [dbo]. [F_str] (@id int) returns nvarchar (1000)
As
Begin
declare @str nvarchar (1000)
Set @str = ""
Select @str = @str + "," + cast (value as nvarchar (900)) from TB where id = @id
Set @str = Right (@str, Len (@str)-1)
Return @str
End
Go
--Calling a function
Select id, value = DBO.F_STR (ID) from TB GROUP by ID
Run Result:
All the nvarchar in the above function are varchar types, and the red on the function above does not specify a length when calling the cast method. Friends after testing found that the results will be truncated in 30 characters, originally thought to be varchar and nvarchar difference, I tried to varchar changed to nvarchar, friend test results in 54 characters at the end of truncation. I checked, the default length problem for varchar, see the following instructions in SQL Server Books Online:
Char and varchar
Fixed-length (char) or variable-length (varchar) character data type.
char[(N)]
Fixed-length and non-Unicode character data of n bytes in length. n must be a numeric value between 1 and 8,000. The storage size is n bytes. The synonym for Char in SQL-92 is character.
varchar[(N)]
A variable length of n bytes and non-Unicode character data. n must be a numeric value between 1 and 8,000. The actual length of the byte that the storage size is the input data, not the N bytes. The length of the data character entered can be zero. The synonyms for varchar in SQL-92 are char varying or character varying.
Comments
If n is not specified in a data definition or variable declaration statement, the default length is 1. If n is not specified using the CAST function, the default length is 30.
The default collation of the database will be assigned to objects that use char or varchar, unless a specific collation is assigned with the COLLATE clause. The collation controls the code page that is used to store character data.
Sites that support multiple languages should consider using Unicode nchar or nvarchar data types to minimize character conversion issues. If you use char or varchar:
Use char If you want the data values in the column to be approximately the same size.
Use varchar If you want data values in columns to be significantly different in size.
If SET ansi_padding is off when the CREATE table or ALTER table is executed, a char column that is defined as NULL will be treated as varchar.
When the Collation code page uses double-byte characters, the storage size is still n bytes. Depending on the string, the storage size of n bytes may be less than n characters.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.