Implicit conversion from varchar to Varbinary how to? - sql

I got the following error trying to convert from a varchar to varbinary..
Implicit conversion from data type
varchar to varbinary(max) is not
allowed. Use the CONVERT function to
run this query.
and this is the alter command that I tried.
alter table foo Alter column bar varBINARY(max)
So how do I use the convert function for something like this? I did a bit of searching via google and had no luck.
Thanks.

You have to rebuild the table.
Use SSMS designer to change the type: this will generate a script with CONVERT.

Related

Explicitly convert datetime to varbinary

I am attempting to convert a datetime column to varbinary(100), but haven't had any luck. My problem is very similar to Convert datetime to varbinary inside update query, but I still wasn't able to develop a solution.
Here is my code:
OPEN SYMMETRIC KEY SymKey_TheDate_SYMMETRIC
DECRYPTION BY CERTIFICATE Certificate_TheDate_Encryption
UPDATE PROFILE_DATA
SET A29_FDATE = EncryptByKey (Key_GUID('SymKey_TheDate_SYMMETRIC'), CONVERT(varbinary(100), A29_FDATE))
FROM PROFILE_DATA
GO
CLOSE SYMMETRIC KEY SymKey_TheDate_SYMMETRIC
Here is the error I get when trying to convert A29_FDATE from datetime to varbinary(100):
Implicit conversion from data type varbinary to datetime2 is not allowed. Use the CONVERT function to run this query.
Your error is not for datetime to varbinary(100), but for varbinary to datetime2. The function EncryptByKey() returns a varbinary that is assigned to a date field A29_FDATE. The output of EncryptByKey() needs explicit conversion if you are going to attempt it.
Something like:
...
SET A29_FDATE = convert(datetime2(0), EncryptByKey(...))
...
Re-read your error:
Implicit conversion from data type varbinary to datetime2 is not allowed. Use the CONVERT function to run this query.
You get a varbinary value just fine. The error is converting it to datetime2. The statement is converting it to datetime2 when it tries to set the field 'A29_FDATE` to the varbinary value. I assume from the field name it is a datetime2 field. I don't think you can or should convert this to a datetime2 field.
If you are in the process of encrypting the field in your table, you will have to create a new column with the type varbinary, probably A29_FVARBIN (yuck), and update that to the result of your query. Then you can drop the A29_FDATE field if you want.

[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.

SQL Server: after converting from image to varbinary(MAX), should I update the converted column?

I have a 200GB table with a column of type image, which I'm planing to convert to a varbinary(MAX). After reading this post about converting NTEXT http://geekswithblogs.net/johnsPerfBlog/archive/2008/04/16/ntext-vs-nvarcharmax-in-sql-2005.aspx the writer suggest to do an
UPDATE testTable
SET testText = testText
After the conversion. Can the same be applied to a conversion between image and varbinary?
Just run this command
ALTER TABLE YourTable
ALTER COLUMN YourImageColumn VARBINARY(MAX);
and that should take care of everything.
There's no extra "conversion" step needed at all.
But again: do this on a TEST instance first!
And before you do it on your live system, TAKE A BACKUP!
You've been warned! :-)

varchar to varbyte with out udf?

I had a table definition with
SSN varbyte(100)
I just want to test my mload script ,
so I gave
CHAR2HEXINT(:SSN) //SSN is defined as varchar in layout.
UTY0805 RDBMS failure, 3532: Conversion between BYTE data and
other types is illegal.
Is there any way that i can convert varchar to varbyte with out using any UDF ?
NOTE:
I cant change my table definition , I just want to test my script .
i just got access to the encrypt function and used that to load test data.
as suggested by dnoeth ,
using TO_BYTES and FROM_BYTES to cast BYTE<->CHAR
seems to be working

SQL converting nvchar to ntext

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?