What is the meaning of varchar(-1) in SQL Server 2008? Is it an alternative for varchar(max)?
It's how to represent varchar(max) in .net SQLDBType (not explicitly stated, but length is -1)
I can't try it in SQL language in SQL Server right now for interest's sake: but I'm sure it's only for client code because varchar is normally strongly defined between 1 and 8000, but for max type it's not.
i think it is varchar(max). I never seen it before.
It is only SQL Query Analyser which displays -1 instead of Max. In design view, still you can see it as Max.
The documentation says:
varchar [ ( n | max ) ]
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.
So -1 isn't technically valid. It might work, but I wouldn't use it if I were you.
Yes man... it´s a varchar(MAX)
Related
When I describe a table with nvarchar data type, what's the difference between Precision and Length? I see that Length is always the double. For example the values is nvarchar(64), the precision is 64 and the length is 128.
CREATE TABLE T(X nvarchar(64))
EXEC sp_columns 'T'
Precision has no meaning for text data.
As for the Length property, I think you confuse it with the Size reported by SQL Server Management Studio, which is the size of a column in bytes. The Length of an nvarchar(64) column is 64 while Size is 128.
The size of unicode types (nchar, nvarchar) is double the number of characters because Unicode uses two bytes for each character.
You can get these values using the LEN function to get the number of characters and the DATALENGTH function to get the number of bytes, eg.
select len(N'some value'), datalength(N'some value')
which returns 10 20
EDIT
From your comments I see you use sp_columns to get at the table's schema info. You shouldn't use any of the catalog stored procedures and use the catalog views instead.
As the documentation states, catalog stored procedures are used to support ODBC applications, hence their results are limited and may need interpretation, as you found out. sp_columns doesn't differentiate between character and data length for example.
Schema views like those in the INFORMATION_SCHEMA or sys schemas return detailed and unambiguous information. For example, INFORMATION_SCHEMA.COLUMNS returns the character lnegth in CHARACTER_MAXIMUM_LENGTH and byte size in CHARACTER_OCTET_LENGTH. It also includes collation and character set information not returned by sp_columns.
The INFORMATION_SCHEMA views are defined by ISO so they don't include some SQL Server-specific info like whether a column is a computed columns, stored in a filestream or replicated. You can get this info using the system object catalog views like sys.columns
NVARCHAR doesnt have precision. Precision is used for decimal. And length is the character length.
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.
From the source:-
Precision is the number of digits in a number.
Length for a numeric data type is the number of bytes that are used to
store the number. Length for a character string or Unicode data type
is the number of characters
NVARCHAR doesn't have precision, and the length would be the character length. Try the following SQL
SELECT LEN('Whats the length of this string?')
You may be confucing this with a Numeric or Decimal, see the chart here
3 Years later but still relevant.. had a similar issue;
I encountered a UDDT 'd_style', using alt+f1 I see this is a 'nvarchar, length=2, prec=1'.
And indeed, I cannot insert e.g. 'EU' in this field, as data would be truncated.
So length 2 does not mean 2 characters here. This is because each character is saved as 2 bytes, thus prec = length/2.
In this case, 'precision' is indeed the maximum amount of characters allowed.
However, when you create a new table with the 'nvarchar' datatype, you can simply enter the desired length (e.g. my_field nvarchar(2)) if you want to be able to insert example value 'EU'.
Mind that this is all quite 'practical', in theory, precision is only applicable for numeric values, and is the number of digits in a number. So we shouldn't be talking about a 'precision' for nvarchar.
Indeed, this is a confusing topic in SQL server, even the documentation is inconsistent.
For example see the syntax definition of CREATE TABLE
<data type> ::=
[ type_schema_name . ] type_name
[ ( precision [ , scale ] | max |
[ { CONTENT | DOCUMENT } ] xml_schema_collection ) ]
You see "precision", but don't see "length | precision".
And as others pointed out, everywhere else in the documentation, they refer to the same attribute as length, when talking about character datatypes. Except where they don't, like the documentation of sp_help and companion.
Length int Column length in bytes.
Prec char(5) Column precision.
Scale char(5) Column scale.
Here length is the storage length. (Most people would call it size.)
So unfortunately there is no correct or final answer to your question, you must always consider the context, and consult the documentation, when in doubt.
I want to write a trigger for one of my tables which has an ntext datatype field an as you know the trigger can't be written for ntext datatype.
Now I want to replace the ntext with nvarchar datatype. The ntext maximum length is 2,147,483,647 character whereas nvarchar(max) is 4000 character.
what datatype can I use instead of ntext datatype.
Or are there any ways to write trigger for when I have ntext datatype?
It's better to say my database is designed before with SQL 2000 and it is full of data.
You're out of luck with sql server 2000, but you can possibly chain together a bunch of nvarchar(4000) variables. Its a hack, but it may be the only option you have. I would also do an assesment of your data, and see what the largest data you actually have in that column. A lot of times, columns are made in anticipation of a large data set, but in the end it doesn't have them.
in MSDN i see this :
* Important *
ntext, text, and image data types will be removed in a future version of Microsoft SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead.
Fixed and variable-length data types for storing large non-Unicode and Unicode character and binary data. Unicode data uses the UNICODE UCS-2 character set.
and it preferd nvarchar(MAX) , You can see details below :
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.
I really want to have a column of inifinite length for one of the properties of my ActiveRecord object of type string, but I don't know how to set it. When I set the length to -1 (the number for MAX in SQL server) I get an error.
Can anyone help?
EDIT: I meant to say Castle ActiveRecord.
I found the answer. Apparently if you set the length to anything greater than 4000 it will set the column to MAX length.
the varchar(max) or nvarchar(max) type is suitable for you.
set the data type of the column to text....
I need to store binary files to the SQL Server Database. Which is the better Data Type out of Varbinary and Image?
Since image is deprecated, you should use varbinary.
per Microsoft (thanks for the link #Christopher)
ntext , text, and image data types will be removed in a future
version of Microsoft SQL Server. Avoid using these data types in new
development work, and plan to modify applications that currently use
them. Use nvarchar(max), varchar(max), and varbinary(max) instead.
Fixed and variable-length data types for storing large non-Unicode and
Unicode character and binary data. Unicode data uses the UNICODE UCS-2
character set.
varbinary(max) is the way to go (introduced in SQL Server 2005)
There is also the rather spiffy FileStream, introduced in SQL Server 2008.
https://learn.microsoft.com/en-us/sql/t-sql/data-types/ntext-text-and-image-transact-sql
image
Variable-length binary data from 0 through 2^31-1 (2,147,483,647)
bytes. Still it IS supported to use image datatype, but be aware of:
https://learn.microsoft.com/en-us/sql/t-sql/data-types/binary-and-varbinary-transact-sql
varbinary [ ( n | max) ]
Variable-length binary 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 the
data entered + 2 bytes. The data that is entered can be 0 bytes in
length. The ANSI SQL synonym for varbinary is binary varying.
So both are equally in size (2GB). But be aware of:
https://learn.microsoft.com/en-us/sql/database-engine/deprecated-database-engine-features-in-sql-server-2016#features-not-supported-in-a-future-version-of-sql-server
Though the end of "image" datatype is still not determined, you should use the "future" proof equivalent.
But you have to ask yourself: why storing BLOBS in a Column?
https://learn.microsoft.com/en-us/sql/relational-databases/blob/compare-options-for-storing-blobs-sql-server
You get a compilation error if you define the string parameter to have a size greater than 8000
e.g.
The size (9000) given to the type 'varchar' exceeds the maximum allowed for any data type (8000).
Any ideas?
you need to store it as TEXT instead of varchar for string larger than 8000 in sql 2000
You can't use text as a parameter value for a stored proc in SQL 2000<<
Sure you can.
What you cannot do is define a local variable as text
You can't do that in SQL 2000, use the "text" data type instead.
Choose SQL 2000 Data Types
You can't use text as a parameter value for a stored proc in SQL 2000, so usually the technique is to break up the information into chunks of 8000 characters or less and reassemble in the proc.