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

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)

Related

"String or binary data would be truncated." for NVARCHAR but not VARCHAR in LIKE operation

In SQL Server, nvarchar takes twice the space of varchar, and its pre-page-pointer limit is 4000 compared to varchar's 8000.
So, why does the following like comparison give a String or binary data would be truncated. error...
select 1 where '' like cast(replicate('x', 4001) as nvarchar(max))
...while casting as a massively larger varchar does not?
select 1 where '' like cast(replicate('x', 123456) as varchar(max))
In fact, why does the top live give a truncation error at all when it's clearly declared as nvarchar(max) which has a size limit of about 2GB?
From the description of the LIKE operator:
pattern
Is the specific string of characters to search for in
match_expression, and can include the following valid wildcard
characters. pattern can be a maximum of 8,000 bytes.
This query shows a factual count of symbols:
select len(replicate('x', 123456)) as CntVarchar,
len(replicate('x', 4001)) as CntNVarchar
+------------+-------------+
| CntVarchar | CntNVarchar |
+------------+-------------+
| 8000 | 4001 |
+------------+-------------+
The first case has 8000 bytes. The second has 8002 bytes, that violates the rule "can be a maximum of 8,000 bytes".

SQL Server size difference for a column

I have table in SQL Server say "Temp" and it has Addr1, Addr2, Addr3, Addr4 columns and some additional columns also there.
These Addr1, Addr2, Addr3 and Addr4 are nvarchar type. when I check the size of this column by object explorer. it shows all of them in nvarchar(100).
But when I check them using Alt + F1. It shows the details in Result Pane with the length as 200. screenshot is below.
why there is different?
If I enter more than 100 characters, I'm getting truncation errors? seems like it taking only 100 characters.
can you please let me know what is the length value specifies ?
Thanks,
Prakash.
Because the size listed in Object Explorer is number of characters and the size listed in the result of your query to sp_help is number of bytes.
VARCHAR values in SQL use 1 byte per character, whereas NVARCHAR values use 2 bytes per character. Both also need a 2 byte overhead - see below. So because you are looking at NVARCHAR columns, these need 200 (well actually 202) bytes to store 100 characters, where a VARCHAR would only require 100 (really 102).
References:
MSDN: char and varchar
The storage size is the actual length of the data entered + 2 bytes.
MSDN: nchar and nvarchar:
The storage size, in bytes, is two times the actual length of data entered + 2 bytes.
(emphasis mine)
MSDN: sp_help:
Reports information about a database object (any object listed in the sys.sysobjects compatibility view), a user-defined data type, or a data type.
/------------------------------------------------------------------------\
| Column name | Data type | Description |
|-------------+-----------+----------------------------------------------|
| Length | smallint | Physical length of the data type (in bytes). |
\------------------------------------------------------------------------/

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

Which data type saves more space TINYTEXT or VARCHAR for variable data length in MySQL?

I need to store a data into MySQL. Its length is not fixed, it could be 255 or 2 characters long. Should I use TINYTEXT or VARCHAR in order to save space (speed is irrelevant)?
When using VARCHAR, you need to specify maximum number of characters that will be stored in that column. So, if you declare a column to be VARCHAR(255), it means that you can store text with up to 255 characters. The important thing here is that if you insert two characters, only those two characters will be stored, i.e. allocated space will be 2 not 255.
TINYTEXT is one of four TEXT types. They are very similar to VARCHAR, but there are few differences (this depends on MySQL version you are using though). But for version 5.5, there are some limitations when it comes to TEXT types. First one is that you have to specify an index prefix length for indexes on TEXT. And the other one is that TEXT columns can't have default values.
In general, TEXT should be used for (extremely) long values. If you will be using string that will have up to 255 characters, then you should use VARCHAR.
Hope that this helps.
As for data storage space, VARCHAR(255) and TINYTEXT are equivalent:
VARCHAR(M): L + 1 bytes if column values require 0 – 255 bytes, L + 2 bytes if values may require more than 255 bytes.
TINYTEXT: L + 1 bytes, where L < 28.
Source: MySQL Reference Manual: Data Storage Requirements.
Storage space being equal, you may want to check out the following Stack Overflow posts for further reading on when you should use one or the other:
What’s the difference between VARCHAR(255) and TINYTEXT string types in MySQL?
varchar(255) v tinyblob v tinytext