convert big varbinary into text field (MSSQLSERVER) - sql

I try to fetch a big textcontent (<8000 chars) of a varbinary(max) field as follow:
CAST(varbinary_as_text AS VARCHAR(10000)) AS varbinary_as_text
I get the following error due the varchar is limited to 8000 chars:
java.sql.SQLException: The size (10000) given to the type 'varchar' exceeds the maximum allowed for any data type (8000).
How to fetch the text (> 8000 chars) content of a varbinary(max) field ?

Cast it as text
CAST(varbinary_as_text AS TEXT) AS varbinary_as_text
OR
Cast it as varchar(max)
CAST(varbinary_as_text AS varchar(max)) AS varbinary_as_text

Related

Conversion of Image type to Text

I'm having some trouble converting the below Image data type to text;
0x1F8B0800FC7E1F3000FF758FCB6EC2301045F748FCC3FC00D524518A0AAB967655D10D5D7AE324E360C9B1A3B179B4887FAF431254FAD8F83173E6DE3B27C14125425AAF2F47D9D6499AA742B16B021D03888A94C29350CE865098E9243E51287FD0DEC3236B6996E74B3111AA7115B18595DBB12686373A0CBDA867B511AADC4AF61452D87C34851B27B371F20AE0ADC6052B9D711C1308A60A45CD441645617684CB5FA534CF3B6DB12B13D14AAE446BA4B6DD2A4116D93D42BF438AF0BE2558EB929D772AC0CB3106B035C186781FDDD7529BC21DE36D651DFF5BE9A1744D6B2850052DBB92BCD7B686A607C97776D3C92648EE081916223A428A389FE1C30C73C07C91CD17E9BC075757B17FD12CEFD1F56831FA52354C0CFD588A213D346E3FB67088A33F099CEA3BD00CE080DC21C2EB530F3E531FE60772AB52FD0D7DD7397F01A585816657020000
I have tried casting as varbinary and then casting that to nvarchar, but get what looks like Unicode characters;
select top 1 cast(cast(body as varbinary(max)) as nvarchar(max)) from dbo.messagedata
How can I convert the above Image data into readable text?

Conversion failed when converting the nvarchar value 'AAAR78509883' to data type int

I have a nvarchar column in one of my tables that I have imported from Access. I am trying to change to an int. To move to a new table.
The original query:
insert into members_exams_answer
select
ua.members_exams_id, ua.exams_questions_id,
ua.members_exams_answers_value, ua.members_exams_answers_timestamp
from
members_exams as me
full join
UserAnswers1 as ua on me.members_exams_username = ua.members_exams_id
full join
exams_questions as eq on eq.exams_questions_id = ua.exams_questions_id
This throws an error:
Conversion failed when converting the nvarchar value 'AAAR78509883' to data type int.
I have tired:
select convert (int, UserAnswers1.members_exams_id)
from UserAnswers1
and
select cast(members_exams_id as integer) int_members_exams_id
from UserAnswers1
and
select cast (members_exams_id as int)
from UserAnswers1
All result in the same error
Conversion failed when converting the nvarchar value 'AAAR78509883' to data type int.
Clearly you are trying to convert data that is alphanumeric to an int and that cannot be done.
Looking at your data why are you insisting on converting it to an int when it cannot be an int? Why not just process it as an nvarchar?
Your problem could be systemic where all data has a leading alpha characters that you need to strip out (and hopefully the same number of alpha characters)
In that case use a substring to strip off the alphas (this assumes the name number of alphabetic characters in each record). Or use a varchar or nvarchar field instead of an int. If the number of leading characters varies or if they can be leading or trailing or some other combination, it will much more complex to fix than we can probably describe on the Internet.
The other possibility is that you simply have some bad data. In which case identify the records which are not numeric and fix them or null the value out if they cannot be fixed. This happens frequently when you have stored the data in an incorrect datatype.

SQL SERVER: String or binary data would be truncated. nvarchar

I'm trying to insert into the following table:
but for some reason I cannot insert more than 250 characters into slabel1 field, even though it's size it's 500. Every time this happens I receive the following error:
String or binary data would be truncated. The statement has been
terminated.
I do not understand why.
Your results show the length in bytes of the column, not how many characters it can store. The column you are using is an nvarchar column, and so a character will take up 2 bytes instead of 1, so in your case (500 / 2) = 250 characters max.
This will show you the difference, we have two columns, each which can hold 50 characters, but the length of the nvarchar column is 100
CREATE TABLE [#text]
(
[Text] VARCHAR(50),
[NText] NVARCHAR(50)
)
SELECT COL_LENGTH( 'tempdb..#Text' , 'Text' ) [Varchar_Length],
COL_LENGTH( 'tempdb..#Text' , 'NText' ) [NVarchar_Length]
DROP TABLE [#text]
The results are:
Varchar_Length | NVarchar_Length
50 | 100
Because it's NVARCHAR
nchar and nvarchar
nchar [ ( n ) ]
Fixed-length Unicode string data. n defines the string
length and must be a value from 1 through 4,000. The storage size is
two times n bytes. 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 can be less than the value
specified for n. The ISO synonyms for nchar are national char and
national character..
nvarchar [ ( n | max ) ]
Variable-length Unicode string data. n
defines the string length and can be a value from 1 through 4,000. max
indicates that the maximum storage size is 2^31-1 bytes (2 GB). The
storage size, in bytes, is two times the actual length of data entered
+ 2 bytes. The ISO synonyms for nvarchar are national char varying and national character varying.
Try this one to see the differences
DECLARE #text1 NVARCHAR(200)
DECLARE #text2 VARCHAR(200)
SET #text1 = 'aaaaaaaa'
SET #text2 = 'aaaaaaaa'
SELECT LEN(#text1), DATALENGTH(#text1)
SELECT LEN(#text2), DATALENGTH(#text2)

What data type to be used for description of an object?

A table has a field of description in sql server. If I take varchar then its maximum limit is 8000 characters but a description can be larger than that. I am using this field for a job description which are normally lengthy .
A varchar(max) has no (effective) limit. Use that.
Use VARCHAR(MAX). It can store more than 8k chars. Actually it can store up to 2GB
varchar [ ( n | max ) ]
Variable-length, non-Unicode string data. n defines the string length and can be a value from 1 through 8,000. MAX indicates that the maximum storage size is 2^31-1 bytes (2 GB). The storage size is the actual length of the data entered + 2 bytes. The ISO synonyms for varchar are char varying or character varying.
Read more at http://msdn.microsoft.com/en-us/library/ms176089.aspx
Please note, that the TEXT and NTEXT types can store nearly the same amount of data, but they are obsolete and you can do less operations on them. Use VARCHAR(MAX) and NVARCHAR(MAX) instead of TEXT and NTEXT.
You can use
varchar(max) or Text data type.

Getting max length of a varchar(max) from syscolumns in sql server

select c.name, t.name, c.length
from syscolumns c
c.length gives me -1 for any column that has max e.g varchar(max)
What should I do to get length ?
The data type of length on sys.columns is a smallint, whilst the max length of the varchar(max) is 2.1 billion, so it has a problem holding the real length. The -1 is in the documentation for denoting a varchar(max), varbinary(max), nvarchar(max) and xml.
http://msdn.microsoft.com/en-us/library/ms176106(v=sql.100).aspx
If you really need the number, then you would need a case statement to replace -1 with (2^31)-1
If you want to get the length of physical data, then you need to max / min / avg the appropriate lengths on the tables with the data on it based on what you need that information for. When querying the length of the field, DATALENGTH returns the bytes used, LEN returns the characters count.
-1 means that the column is of type max. The max length is then the max type, as per documentation. MAX types have a maximum length of 2GB if the FILESTREAM attribute is not specified, or a max size limited only by the disk size available:
The sizes of the BLOBs are limited only by the volume size of the file
system. The standard varbinary(max) limitation of 2-GB file sizes does
not apply to BLOBs that are stored in the file system.
Therefore your question really doesn't have an answer. You can ask what is the actual size of any actual in the table value, using DATALENGTH.
As seen HERE:
Variable-length, non-Unicode character data. n can be a value from 1 through 8,000. max indicates that the maximum storage size is 2^31-1 bytes. The storage size is the actual length of data entered + 2 bytes. The data entered can be 0 characters in length. The ISO synonyms for varchar are char varying or character varying.
In other words, max = 2147483647 bytes if all the possible space is occupied..
The length of the column in each row could vary. Hence the result of -1