SQL: Alternative to text and ntext data type? - sql

I'm currently designing a MS SQL table for use in a project. The problem I'm currently facing is that the data types of "Transact SQL " are pointing to the deprecation of the types "text" and "ntext", which I always used for big texts.
Char, nchar, varchar and nvarchar may only be 8000 bytes big, this just isn't enough for a user input text, e. g. if he's writing a big article.
Is there any alternative to the obsolete data type text/ntext?

Using nvarchar(MAX) will allow you to store up to 2 GB of data. The same goes for varchar(MAX) too.

varchar(MAX) and nvarchar(MAX). Try 'em; you'll like 'em.

If you finished reading the paragraph on the MSDN page you linked, which explained that they were being removed, you would have found your answer:
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.

If you're using SQL 2005+, you could use n / varchar(max) instead. nvarchar will use unicode, and varchar will use ascii. If this is used to store blog text, then I would go with nvarchar(max)

Related

Storing 30k-char strings, should I use NVARCHAR(max)?

From what I fond on internet, I should be using NVARCHAR(max) to store some strings that contain up to 30k characters. I don't need to search through the strings, only store and retrieve it. The type TEXT seems obsolete, and VARCHAR isn't as friendly as NVARCHAR about Unicode, so I should be using NVARCHAR(max), right ?
Secondly, for now I'm using some sql database of a free webhosting service, i'm creating the structure via the phpmyadmin, and when creating columns it doesn't suggest NVARCHAR, only VARCHAR and other types. Is NVARCHAR still available, maybe with a DDL command ? The sql version is 5.5.32
Thanks and sorry if my questions are unclear, i'm kind of a newb

What is the best SQL type to use for a large string variable?

Apologies for the rather basic question.
I have an error string that is built dynamically. The data in the string is passed by various third parties so I don't have any control, nor do I know the ultimate size of the string.
I have a transaction table that currently logs details and I want to include the string so that I can reference back to it if necessary.
2 questions:
How should I store it in the database?
Should I do anything else such as contrain the string in code?
I'm using Sql Server 2008 Web.
If you want to store non unicode text, you can use:
varchar(max) or nvarchar(max)
Maximum length is 2GB.
Other alternatives are:
binary or varbinary
Drawbacks: you can't search into these fields and index and order them
and the maximum size : 2GB.
There are TEXT and NTEXT, but they will be deprecated in the future,
so I don't suggest to use them.
They have the same drawbacks as binary.
So the best choice is one of varchar(max) or nvarchar(max).
You can use SQL Server nvarchar(MAX).
Check out this too.
Eventualy, you can enable and use a FILESTREAM feature of SQL Server 2008 (it's supported by WEB edition), and deal with extra large amount of data in sense of documents.
Of course, you need to be sure that you will use a benefit of this service.

Hearts change to question mark when I insert data into the database. How do I change the charset of the table?

There is a video file named ♥-You-Got-Me-♥[www.savevid.com].mp4. But as the file with this name is inserted into a SQL Server 2005 database the hearts change to ?.
So the name turns into ?-You-Got-Me-?[www.savevid.com].mp4.
I don't know how to change the character set of the database? How do I change the char set of my table so that it can over all the characters ?
It will be great if along with the command,graphical method to do so is included in the answer.
You don't need to change the character set of the database. As long as you are using the NVARCHAR type, you should be good on the database side. However, you have to make sure that however you get the data into the table takes Unicode into account:
DECLARE #VAR VARCHAR(100) = N'♥-You-Got-Me-♥[www.savevid.com].mp4'
, #NVAR NVARCHAR(100) = N'♥-You-Got-Me-♥[www.savevid.com].mp4'
, #oops NVARCHAR(100) = '♥-You-Got-Me-♥[www.savevid.com].mp4'
SELECT
#VAR
, #NVAR
, #oops;
Returns:
?-You-Got-Me-?[www.savevid.com].mp4 ♥-You-Got-Me-♥[www.savevid.com].mp4 ?-You-Got-Me-?[www.savevid.com].mp4
The last declaration omits the N in front of the literal. There are similar ways to mess this up in your front end. Even if the DB stores Unicode, you have to make sure that everything between input and the DB, and then back out to your UI, handles multi-byte characters properly.
It isn't a CHARSET problem but a datatype problem in SQL Server. SQL Server doesn't have CHARSET as such like MySQL and Collations are for code page, sorting and comparing
You need to use nvarchar to store unicode (basically non-latin) data properly.
The problem is likely using VARCHAR, if possible changing to a NVARCHAR type should resolve the problem for new entries. If you cannot change the column type it may get more complicated.
SQL Fiddle
Change the type of the field storing your filename from varchar to nvarchar.
For example:
Table:
FilenameId INT IDENTITY
Filename NVARCHAR(200)
SQL to insert data:
INSERT INTO TestTable ([Filename]) VALUES(N'♥-You-Got-Me-♥[www.savevid.com].mp4')

Use varbinary(max) to store HTML? SQL Server 2008

I am trying to figureout the best solution to store html data in some of my tables and track the history of changes made to that HTML. I read that text, ntext, etc are no longer supported for AFTER Triggers so I was thinking of using the varbinary(max) instead. Has anyone used varbinary to store HTML? I was planning on tracking the changes using a trigger to write off the history when the HTML is updated.
As always I greatly appreciate the input....the feedback I get is always great.
Thanks,
S
using varchar(max) would be more logical - it is made for textual data and you can us string processing functions with it.
Why not varchar(max) or nvarchar(max) instead?

Data Type Mapping

I need to store XML data to database(MS SQL Server). The data type defined in the column is text.
I need to know the the equalent datatype for text. I have tried with adLongVarChar but it does not works. Also I tried with adLongVarWChar(nText). But both are not working.
Need help.
Thanks.
In case your are using SQL Server 2005 or higher then you might prefer going for the XML data type. Read more of it here http://www.codeproject.com/KB/database/XMLdDataType.aspx
Also going forward avoid using ntext and text data types as they would be removed from future versions of SQL Server. Instead go for nvarchar(max) or varchar(max). Read on this here http://msdn.microsoft.com/en-us/library/ms187993.aspx
cheers
TSQL datatype for a string is varchar or nvarchar (unicode). To specify the length of the string, varchar(50).
Also note there is an XML datatype in SS 2008 (2005?).