Why won't this SQL CAST work?

Posted by Kev on Stack Overflow See other posts from Stack Overflow or by Kev
Published on 2010-03-16T16:27:14Z Indexed on 2010/03/16 16:31 UTC
Read the original article Hit count: 294

Filed under:
|
|

I have a nvarchar(50) column in a SQL Server 2000 table defined as follows:

TaskID nvarchar(50) NULL

I need to fill this column with some random SQL Unique Identifiers (I am unable to change the column type to uniqueidentifier).

I tried this:

UPDATE TaskData SET TaskID = CAST(NEWID() AS nvarchar)

but I got the following error:

Msg 8115, Level 16, State 2, Line 1 Arithmetic overflow error converting expression to data type nvarchar.

I also tried:

UPDATE TaskData SET TaskID = CAST(NEWID() AS nvarchar(50))

but then got this error:

Msg 8152, Level 16, State 6, Line 1 String or binary data would be truncated.

I don't understand why this doesn't work but this does:

DECLARE @TaskID nvarchar(50)
SET @TaskID = CAST(NEW() AS nvarchar(50))

I also tried CONVERT(nvarchar, NEWID()) and CONVERT(nvarchar(50), NEWID()) but got the same errors.

© Stack Overflow or respective owner

Related posts about tsql

Related posts about sql-server