SQL converting nvchar to ntext - sql

What I have is a staging table that is all nvarchar (so i can load it easily). In my live table i have a bunch of ntext items. I have the following:
obviously this isnt the whole query:
update
SLTDS_C69_Stdtable
set
[AARIssue] = convert(ntext, st.[AARIssue]),
[AttachmentIDs] = convert (ntext, st.[AttachmentIDs])
I get this error returned:
types ntext and nvarchar are incompatible in the equal to operator.
ANy idea how to fix this?

Do you want to replace the text or add to it?
INthe first case you don't need to convert at all, just set the filed to the value of the filed inthe other table. INthe second case you need to use UPDATE text.
However, you have a problem in that ntext is deprecated, you should consider converting these fields to nvarchar(max) as soon as possible unless you are still running SQL Server 2000.

Is this a repeat of this: How to update a text or ntext field in SQL Server 2000
The live table should use UPDATETEXT?

Related

[Excel Destination [28]] Error: An error occurred while setting up a binding for the column. The binding status was "DT_NTEXT"

I'm working on ssis package which exports data from SQL Server to Excel. I had a problem converting non-unicode to unicode string data types. So I created a derived Column task and converted to Unicode string [DT_WSTR] 4 columns which have a type Varchar(40) in SQL Server table. It worked with these columns. But I also have a Description column of type varchar(max) and I tried to convert it to Unicode text stream [DT_NTEXT]. It did not work.
If your source is SQL Server (as you said), you can convert it directly in your SQL Query
SELECT
CONVERT(NVARCHAR(40), 'att1')
,CONVERT(NTEXT, 'att2')
Convert your VARCHAR into NVARCHAR
Convert your TEXT into NTEXT
it's faster.
P.S. To test it (Do not forget to delete or reset your previous OLE DB Input component) -> It will be forced to reevaluate your datatype
Does it help you?
The only thing that worked was to cast a Description column in Stored Procedure as varchar(1000). I checked the max length of this field and it was about 300 characters. So I made it varchar(1000) and in Derived column Unicode string [DT_WSTR]. This was a workaround, but I still want to know how to make it in ssis package without converting data type in Stored Procedure.

Get varBinary data from database and display C#

I retrieve a data, whose datatype is varbinary from database and display it, but the data display is totally different
The data in database is
0x0400000000200040000000000018
But when it display is
BAAAAAAgAEAAAAAAABg=
Is there anywhere to display it as
0x0400000000200040000000000018?
Then simply you can do is
Change your datatype from
varbinary
to
varchar(500)
OR
Try this Also
CONVERT(VARCHAR(500),coulmn_name,2)
that will solve your issue
EDIT
You can do simply like this
SELECT *,CONVERT(VARCHAR(500),coulmn_name,2) FROM TB_USER

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')

alternatives to REPLACE on a text or ntext datatype

I need to update/replace the data in datatable.column. The table has a field named Content. I'm using the REPLACE function. Since the column datatype is NTEXT, SQL Server doesn't allow me to use the REPLACE function.
I can't change the datatype because this database is 3rd party software table. Changing the datatype will cause the application to fail.
UPDATE [CMS_DB_test].[dbo].[cms_HtmlText]
SET Content = REPLACE(Content,'ABC','DEF')
WHERE Content LIKE '%ABC%'
I Receive this error:
Msg 8116, Level 16, State 1, Line 1
Argument data type ntext is invalid for argument 1 of replace function.
Can I fix this with T-SQL? Does someone have an example how to read and to loop?
Since this is onetime conversion, maybe I can change to another type but I'm afraid I'm messing up the data.
There is a primary key field: name: ID - integer - it's an identity.... So I need to think about this too. Maybe set the Identity to N temporary.
Please advise on how to achieve the REPLACE function?
Approx. 3000 statements need to be updated with a new solution.
IF your data won't overflow 4000 characters AND you're on SQL Server 2000 or compatibility level of 8 or SQL Server 2000:
UPDATE [CMS_DB_test].[dbo].[cms_HtmlText]
SET Content = CAST(REPLACE(CAST(Content as NVarchar(4000)),'ABC','DEF') AS NText)
WHERE Content LIKE '%ABC%'
For SQL Server 2005+:
UPDATE [CMS_DB_test].[dbo].[cms_HtmlText]
SET Content = CAST(REPLACE(CAST(Content as NVarchar(MAX)),'ABC','DEF') AS NText)
WHERE Content LIKE '%ABC%'
Assuming SQL Server 2000, the following StackOverflow question should address your problem.
If using SQL Server 2005/2008, you can use the following code (taken from here):
select cast(replace(cast(myntext as nvarchar(max)),'find','replace') as ntext)
from myntexttable

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