How to convert (cast) int to ntext - sql

I need to convert int field like 1005 to ntext alike Alarm1005, but CAST(ID as ntext) doesn't work, so how can I convert(cast) int to 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:
You have plenty of functions that work with nvarchar as compared to those that can handle ntext.
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

Related

Convert ntext to numeric

One of my column is defined as ntext datatype which is no longer supported by many streams. I'm trying my best to convert that column to numeric or int and every attempt is reaching anywhere.
reg_no #my ntext field | name | country | etc |
I tried to alter the col using the command we all use, but failed
alter table tabl1
alter column [reg_no] numeric(38,0)
Error:
Error converting data type nvarchar to numeric
Any suggestions on fixing this or has anyone come across this in the past, if yes how did you get over it
You should be able to do this in two steps:
alter table tabl1 alter column [reg_no] nvarchar(max);
alter table tabl1 alter column [reg_no] numeric(38,0);
ntext is deprecated and conversion to numeric is not supported, but converting to nvarchar() is supported.
This assumes that the values are compatible with numeric. Otherwise you will get a type conversion error. If this happens, you can get the offending values by using:
select *
from t
where try_convert(numeric(38, 0), try_convert(nvarchar(max), x)) is null
Try,
select convert(int,convcert(varchar(40), reg_no)) as newfieldname from tabl1

Size limit for nvarchar(max) , Print issue

I want to save XML record that length is more than 43679 char and its saved into table.
Because when i'm checking its length its giving more than 43679 char but when i'm going to read or Print data of this column its only showing 43679 char.
The below image can help you to understand the problem.
like example
declare #t table (a nvarchar(max));
insert into #t (a) values (REPLICATE(CONVERT(nvarchar(max),'a'),200000));
select LEN(a) from #t -- result 200000
select print(a) from #t -- print only 43679 char.
Please help me out of this situation.
This is a well known bug in SSMS, You can't paste more than 43679 char from a grid view column and unfortunately this limit can't be increased, You can get around this by displaying your Data in Xml format instead of nvarchar
The datatypes like NCHAR, NVARCHAR, NVARCHAR(MAX) stores half of CHAR, VARCHAR & NVARCHAR(MAX). Because these datatype used to store UNICODE characters. Use these datatypes when you need to store data other then default language (Collation). UNICODE characters take 2 bytes for each character. That's why lenth of NCHAR, NVARCHAR, NVARCHAR(MAX) stores half of CHAR, VARCHAR & NVARCHAR(MAX).
SQL Server Management Studio has a character limit when printing to the messages pane. There is a workaround to achieve what you need.
Using FOR XML to select your data using TYPE you can specify [processing-instruction] and give it a name. Your text will be presented as a link which you can open. This text will have wrappers and the name you specified. Here is an example.
declare #t table (a nvarchar(max));
insert into #t (a) values (REPLICATE(CONVERT(nvarchar(max),'a'),200000));
select LEN(a) from #t -- result 200000
SELECT a [processing-instruction(TextOutput)] from #t FOR XML PATH(''), TYPE;

How to cast varchar in varbinary?

I try to update column value by appending varchar strings.
MyTable{
Id int,
MyValueColumn varbinary(max),
MyParamColumn varchar(50)
}
how i append:
'{"ZoneId":'+cast ([MyValueColumn] as varchar)+', "ZoneName":"'+[MyParamColumn]+'"}'
And this return correct value:
'{"ZoneId":1018, "ZoneName":"szz"}'
But now i cast result in varbinary and cast it again in varchar (for check varbinary correcy):
cast (cast('{"ZoneId":'+cast ([MyValueColumn] as varchar)+', "ZoneName":"'+[MyParamColumn]+'"}' as varbinary) as varchar)
and result:
'{"ZoneId":1018
Whats can be wrong?
When you are using cast to varbinary without explicit specification of length default length is 30.
So some trimming of your data can occurs if you're using varbinary, not varbinary(n) or varbinary(max) explicitly.
See MSDN for reference.
NOTE:
Also you have missed ' in your query: cast (cast({"ZoneId":' should be cast (cast('{"ZoneId":'
Update
I've created simple example:
declare #text varchar(50)
select #text = '{"ZoneId":'+cast (1018 as varchar)+', "ZoneName":"'+'szz'+'"}'
select cast(cast(#text as varbinary) as varchar)
select cast(cast(#text as varbinary(max)) as varchar(max))
it gives you trimmed text in first cast and original untrimmed text when we're using varbinary(max)

Convert nvarchar(max) to varbinary(max)

Have table with values
report nvarchar(max) not null
description nvarchar(max)
In stored procedure I want select values from table and then convert it to varbinary max. I seletct :
select
CONVERT(varbinary(max), [report]) as [report],
ISNULL(CONVERT(varbinary(max), [description]), '') as [description]
from myTbl
but I get an error:
Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.
Please help me to solve this problem
The failure is occurring because you convert description to varbinary, but then try to cast any null values back to a varchar. You just need to move ISNULL inside the CONVERT or change the conversion value when null to a binary value.
ISNULL in CONVERT
SELECT
CONVERT(varbinary(MAX), report),
CONVERT(varbinary(max), ISNULL([description], '')) as [description]
FROM myTbl
Proper ISNULL Value
SELECT
CONVERT(varbinary(MAX), report),
ISNULL(CONVERT(varbinary(max), [description]), 0x) as [description]
FROM myTbl
Both versions will produce the same output 0x if description is null.

Convert nvarchar to bigint in Sql server 2008

I want insert all rows of a table into another table, and I also want convert a nvarchar field into bigint, but when I use convert(bigint, col1) SQL Server shows an error:
Error converting data type nvarchar
to bigint
How can I fix this problem?
You could try to use ISNUMERIC to determine those rows that are indeed numeric:
UPDATE dbo.YourTable
SET BigIntColumn = CAST(NVarcharColumn AS BIGINT)
WHERE ISNUMERIC(NVarcharColumn) = 1
That would convert those rows that can be converted - the others need to be dealt with manually.
You should convert bigint to nvarchar not vice versa
cast(Other_Column_name as nvarchar) not cast (Column_Name as bigint)
you can try this:
CAST(CAST(col1 as NUMERIC) as BIGINT)
Here I convert navrchar value of column into bigInt and than perform Addition of thos two columns find blow :
SELECT (CAST(col1 AS BIGINT) + CAST(col2 AS BIGINT)) AS TotalValue from tableName