tags:

views:

161

answers:

2

I am writing a SQL Function that will take in a decimal and return the base32 representation of that decimal.

My problem is with the decimal to ascii conversion.

I am allowed to run the following query and return an ascii character

"SELECT CHAR( 65 )" which returns "A"

However in my function when I am trying to build my output string of letters, I am having trouble casting a bigint into a char, then concatenate that char to the end of a another char(which would be my output).

Sample line of code: "SET @OutputChar = @OutputChar + CAST( ( @Output + 55 ) AS CHAR(255) )"

What is the proper way to cast a bigint to char and then concatenate that char to another?

Thanks

A: 

How are you declaring @OutputChar?

If you have:

DECLARE @OutputChar CHAR(255)

then every time you concantenate, it will truncate it to 255. CHAR includes the whitespace at the end, maybe you mean to use varchar instead of CHAR?

ck
You pointed out the error in my logic. I was allocating 255 in size for the char, which when I go to concatenate it would be 255 + 255, and cut off the concatenated char. I changed it from CHAR(255) to CHAR and this allowed the char to not be truncated. Thank you for the help!
Zombie8
A: 

Try this:

SET @OutputChar = @OutputChar + CONVERT(varchar(255),@Output + 55)
Jess