简体   繁体   中英

How to convert (cast) int to ntext

我需要将类似1005 int字段转换为类似于Alarm1005 ntext ,但是CAST(ID as ntext)不起作用,所以如何将int转换(cast)为ntext

CAST(CAST(ID as nvarchar(10)) as ntext) ?

EDIT

As gbn has justly hinted, nvarchar(max) is actually a much more preferable type for storing large string data.

Two reasons:

  1. You have plenty of functions that work with nvarchar as compared to those that can handle ntext .
  2. The types ntext , text and image are officially deprecated.

And one other tiny reason is, you wouldn't have to double cast. It would be as simple as CAST(ID AS nvarchar(10)) or CAST(ID AS nvarchar(max)) .

ALTER TABLE MyTable ADD new_column NTEXT
UPDATE MyTable SET new_column = cast(old_column as nvarchar(16))
ALTER TABLE MyTable DROP COLUMN old_column

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM