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))
LVL 5
25112Asked:
Who is Participating?
 
pcelbaCommented:
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.
0
 
ste5anSenior DeveloperCommented:
It's not possible. Cause there is no implicit cast from CHAR() to BINARY().

You can only apply a swap.
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
if you need to convert it both ways, I would suggest you add another computed column to store the varbinary value
0
 
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))
0
 
25112Author Commented:
very good ideas; thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.