Question: How do you convert ASCII to DECIMAL or DECIMAL to ASCII?
Answer:
ASCII – Returns the ASCII code value of the leftmost character of a character expression.
CHAR – Fixed-length non-Unicode character data with length of n bytes.
Examples:
--Decimal to ASCII
SELECT CHAR(80)+CHAR(73)+CHAR(78)+CHAR(65)+CHAR(76) ASSQLAuthorityAuthor
GO
--ASCII to Decimal
SELECT ASCII('P') AS SQLAuthorityAuthor
UNION ALL
SELECT ASCII('I')
UNION ALL
SELECT ASCII('N')
UNION ALL
SELECT ASCII('A')
UNION ALL
SELECT ASCII('L')
GO
Reference : Pinal Dave (https://blog.sqlauthority.com)
4 Comments. Leave new
If someone asked me this question, I would have explained cast and convert.
Though ASCII has been around from 2005 many aren’t aware of it. This helps experienced guys to recap and newbies to learn. Keep them coming, thanks for sharing
@Sri Varchan,
The ASCII() function has been around for a whole lot longer than just 2005.
@Pinal Dave,
You have the wrong definition for CHAR() posted. You have the one for the CHAR() datatype. You want the one for the CHAR(integer) function. See the following link.
https://docs.microsoft.com/en-us/sql/t-sql/functions/char-transact-sql?view=sql-server-2017