Changing CHAR to BINARY.

how can you change a CHAR column to BINARY safely, so that the ability to revert it back to CHAR is  not lost?
(can you allow a CHAR value to remain as such in BINARY? for example, a value of ABC123 in CHAR, can it be  made to remain the same after conversion to BINARY(6))
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
It's not possible. Cause there is no implicit cast from CHAR() to BINARY().

You can only apply a swap.
Aneesh RetnakaranDatabase AdministratorCommented:
if you need to convert it both ways, I would suggest you add another computed column to store the varbinary value
Scott PletcherSenior DBACommented:
Just explicitly CAST it to binary and back:

declare @binary6 binary(6)
select @binary6 = CAST('abcdef' as binary(6))
select @binary6, CAST(@binary6 as char(6))
I would say there is no such problems with ASCII characters up to ASCII value 127.
Higher values or even Unicode may depend on the collation used.

But you may use your own .NET conversion function if necessary.

OTOH, the computed column is also good solution.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
25112Author Commented:
very good ideas; thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.