Skip Navigation Links
Home
Functions
Tips & Tricks
SQL Server 2005
SQL Server 2008
SQL Server 2012
FAQ
Forums
Practice Test
Bookstore
Tip of the Day : Example Uses of the CHAR String Function
Welcome to SQL Server Helper!!!

This site is intended for those who are beginning to use SQL Server as part of their day-to-day activities.  You will find in this site a collection of useful functions, triggers, stored procedures and tips and tricks related to SQL Server.

Should you have any comments or questions regarding this site or if you want to ask SQL Server-related questions, e-mail us here.

We hope we are able to help and we are glad to help!!!

 
SQL Server Tip of the Day - November 28, 2014
Example Uses of the CHAR String Function

The CHAR string function converts an integer ASCII code to a character.  The syntax of the CHAR string function is as follows:

CHAR ( < integer_expression > )

The < integer_expression > is an integer value from 0 through 255.  A NULL value is returned if the integer expression is not in this range.

Here are sample uses of the CHAR string function

Usage #1 : Looking for Non-printable Characters within a String

SELECT *
FROM [dbo].[Products]
WHERE [Description] LIKE '%[' + CHAR(9) + CHAR(10) + CHAR(13) + ']%'

Usage #2 : Generating a Tab-Delimited File

SELECT CAST([CompanyID] AS VARCHAR(10)) + CHAR(9) + [CompanyName] + CHAR(9) + [Address] + CHAR(9) +
       [City] + CHAR(9) + [State] + CHAR(9) + [ZIP] + CHAR(13)
FROM [dbo].[Company]

Usage #3 : Generate a Random Password

DECLARE @NewPassword                  VARCHAR(20)
DECLARE @PasswordLength               INT
DECLARE @Index                        INT

SET @NewPassword = ''
SET @Index = 1
SET @PasswordLength = 10
WHILE @Index <= @PasswordLength
BEGIN
    SET @NewPassword = @NewPassword + CHAR(ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) % 93 + 33)
    SET @Index = @Index + 1
END

SELECT @NewPassword

Usage #4 : Create an Address Label

SELECT [CompanyName] + CHAR(13) + [Address] + CHAR(13) + 
       [City] + ', ' + [State] + ' ' + [ZIP] + CHAR(13) + CHAR(13)
FROM [dbo].[Company]

Usage #5 : Getting a Currency Symbol

SELECT CHAR(36) AS [Dollar Sign], CHAR(162) AS [Cent Sign], CHAR(163) AS [Pound Sign],
       CHAR(164) AS [Currency Sign], CHAR(165) AS [Yen Sign]

Dollar Sign Cent Sign Pound Sign Currency Sign Yen Sign
----------- --------- ---------- ------------- --------
$           ¢         £          ¤             ¥
 
SQL Server 2012
What's New in SQL Server 2012
SQL Server 2012 Articles
 
SQL Server 2008
What's New in SQL Server 2008

A collection of articles related to what's new with SQL Server 2008:

SQL Server 2008 Articles

A collection of articles related to SQL Server 2008:

 
User-Defined Functions
Date Functions

A collection of useful user-defined functions that deal with dates.

String Functions

A collection of useful user-defined functions that deal with strings (varchar/char/nvarchar/nchar).

Tree Functions

A collection of useful user-defined functions that deal with tree or hierarchical design structures.

Table-Valued Functions
A collection of useful table-valued user-defined functions that can be used to join with other tables.
SQL Server Built-in Functions
A reference to all built-in functions available within SQL Server grouped into categories.
 
Tips and Tricks

A collection of useful SQL Server-related tips and tricks:

SQL Server Error Messages

A list of SQL Server error messages and for certain error messages, discusses ways on how to solve the error or work around them:

 
Frequently Asked Questions
SQL Server

A collection of frequently asked questions about SQL Server grouped by categories:

SQL Server 2005

A collection of frequently asked questions about SQL Server 2005 grouped by categories: