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

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

Related

Search and Replace a a partial string / substring in mssql tables

I was tasked with moving an installation of Orchard CMS to a different server and domain. All the content (page content, menu structure, links, etc.) is stored in an MSSQL database. The good part: When moving the physical files of the Orchard installation to the new server, the database will stay the same, no need to migrate it. The bad thing: There are lots and lots of absolute URLs scattered all over the pages and menus.
I have isolated / pinned down the tables and fields in which the URLs occur, but I lack the (MS)SQL experience/knowledge to do a "search - replace". So I come here for help (I have tried exporting the tables to .sql files, doing a search-replace in a text editor, and then re-importing the .sql files to the database, but ran into several syntax errors... so i need to do this the "SQL way").
To give an example:
The table Common_BodyPartRecord has the field Text of type ntext that contains HTML content. I need to find every occurance of the partial string /oldserver.com/foo/ and replace it with /newserver.org/bar/. There can be multiple occurances of the pattern within the same table entry.
(In total I have 5 patterns that will need replacing, all partial string / substrings of urls, domains/paths, etc.)
I usually do frontend stuff and came to this assignment by chance. I have used MySQL back in the day I was playing around with PHP related stuff, but never got past eh basics of SQL - it would be helpful if you could keep your explainations more or less newbie-friendly.
The SQL server version is SQL Server 9.0.4053, I have access to the database via the Microsoft SQL Server Management Studio 12
Any help is highly appreciated!
You can't manipulate the NTEXT datatype directly, but you can CAST it to VARCHAR(MAX), then use the REPLACE function to perform the string replacement, then CAST it back to NTEXT. This can all be done in a single UPDATE statement.
update MyTable
set MyColmun = cast(replace(cast(MyColumn as nvarchar(max)), N'/oldserver.com/foo/', N'/newserver.org/bar/') as ntext)
where cast(MyColumn as nvarchar(max)) LIKE N'%/oldserver.com/foo/%'
The WHERE clause in the UPDATE statement below is used to prevent SQL Server from making non-changes, i.e. if the value does not need to be changed then there is no need to update it to itself.
The CAST function is used to change the data type of a value. NTEXT is a legacy data type used for storing large character values, NVARCHAR(MAX) is a new and more versatile data type for storing large character values. The REPLACE function can not operate on NTEXT values, hence the need to CAST it to NVARCHAR(MAX) first, do the replace, then CAST it back to NTEXT afterwards.

SQL: Alternative to text and ntext data type?

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)

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.

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?).

Can source code examples be kept in a SQL database while retaining all formatting? If so

Can source code examples be kept in a SQL database while retaining all formatting (tabs, newlines, etc.)? If so what data type would be used?
Yes, use a TEXT type (or MEDIUMTEXT or LONGTEXT - you get the idea)
A BLOB type (varbinary) would definitely work, although databases shouldn't mangle text that's stored as varchar either.
The best in Sql Server: nvarchar(max)
You can upload this into a blob data type. SQL 2008 comes with the capability of storing the entire executable file in the database.