SQL Server String Functions-1

A string function accepts a string value as an input and returns a string value regardless of the data type. They’re used to convert an existing string value to a different format, change the letter cases, retrieve a portion of it, or determine the length of it. They are utilized in reports, logic implementation, and data analysis on a daily basis.

This will be a series of postings since we will go over each one in detail with one or more examples for simple comprehension. So continue reading.

ASCII:
This function returns the ASCII code value of the leftmost character of a character expression.

  Syntax:
ASCII(character_expression);

Example:

  SELECT ASCII(‘A’) AS A, ASCII(‘B’) AS B
Returns: 65, 66

  SELECT ASCII(‘a’) AS a, ASCII(‘b’) AS b
Returns: 97,98

  SELECT ASCII(1) AS [1], ASCII(2) AS [2];
Returns: 49,50

CHAR:
This function returns the single-byte character with the supplied integer code, as defined by the default collation of the current database’s character set and encoding. CHAR is primarily used to insert control characters into character strings. When the query returns as text instead of grid, it is 9 characters for tab, 10 characters for line feed, and 13 characters for carriage return.

  Syntax:
CHAR(integer_expression);

Let’s use it in the “emp” table.

  Examples: 
  SELECT e.Ename, + CHAR(13) + d.Dname
  FROM Emp e
  INNER JOIN Dept d ON e.deptNo = d.DeptNo
  WHERE e.DeptNo = 10
  GO

  SELECT e.Ename, + CHAR(9) + d.Dname
  FROM Emp e
  INNER JOIN Dept d ON e.deptNo = d.DeptNo
  WHERE e.DeptNo = 10
  GO

Results:

Char_Function_SQLServer

Please continue to read the articles on String functions that will be published in the near future.

7 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s