top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to Work with QUOTENAME and ASCII Common String functions in SQL server 2008?

0 votes
572 views
How to Work with QUOTENAME and ASCII Common String functions in SQL server 2008?
posted Mar 21, 2016 by Latha

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

ASCII ()

ASCII () is one of the most commonly used string functions in SQL Server. It is used to find the ASCII value for the leftmost character of the given string.

It takes only one parameter; ‘character’, which can be of type char or varchar or any character.

Syntax:

ASCII (character_expression)

Example:

SELECT ASCII('N')



Output:
-------------
78

If any string is given to find the ASCII value, it can be done using the other function and computing ASCII value for each character one by one. Or for any specific character, ASCII value can be find out by given its position.

To find the ASCII value for a specific character from a given string, which is ‘e’ here:

Example:

DECLARE @string char(11)
SET @string = 'some string'
SELECT ASCII(SUBSTRING(@string, 4, 1))

Output:
-------------
101

To find the ASCII value for the whole given string:

Example:

DECLARE @string char(11), @pos int;
SET @string = 'some string';
SET @pos = 1;
WHILE @pos <= LEN(@string)
  BEGIN
    SELECT ASCII(SUBSTRING(@string, @pos, 1))
    SET @pos = @pos +1;
  END

Output will be the ASCII value of one character in every iteration.

QUOTENAME ()
QUOTNAME () is one of the most commonly used string functions in SQL Server. It is used to find the Unicode string with the delimiters to make a string valid Microsoft SQL delimited identifier.

It takes two parameters; first is the Unicode character string and second parameter is the quote character used to quote the Unicode string. The second parameter is optional. If it is not given, default quote characters are ‘[ ]’.

Syntax:

QUOTENAME (character_expression, quote_character)

Example:

SELECT QUOTENAME('test() function')



Output:
-------------
[test() function]

To find the quote string with a specified quote character.

Example:

SELECT QUOTENAME('test() function', '}')



Output:
-------------
{test() function}
answer Mar 21, 2016 by Shivaranjini
...