SQL

How To Use String Functions In SQL

In this article, we will learn how to use the String Functions in SQL.

SQL Server has many built-in String Functions.

Function Description
ASCII() It returns the ASCII value for the specific character.
SELECT ASCII('CodeHubs') AS FirstCharNumCode;

Output: 67

CHAR() It returns the character based on the ASCII code.
SELECT CHAR(97) AS CodeToCharacter;

Output: a

CHARINDEX() It returns the position of a substring in a string.
SELECT CHARINDEX('hub', 'CodeHubs') AS Position;

Output: 5

CONCAT() It adds two or more strings together.
SELECT CONCAT('thecodehubs', '.com') AS CONCAT;
--OR
SELECT 'thecodehubs' + '.com' AS CONCAT;

Output: thecodehubs.com

CONCAT_WS() It adds two or more strings together with a separator.
SELECT CONCAT_WS('.', 'www', 'thecodehubs', 'com') AS CONCAT;

Output: www.thecodehubs.com

DATALENGTH() It returns the length of expression in bytes.
SELECT DATALENGTH('.com') as Bytes;

Output: 4

DIFFERENCE() It compares two SOUNDEX values and returns an integer value.
SELECT DIFFERENCE('thecodehubs', 'codehubs') as SoundexValue;

Output: 2

FORMAT() It formats a value with the specified format.
SELECT FORMAT(9876543210, '##-##-#####') AS FORMAT;

Output: 987-65-43210

LEFT() It extracts a number of characters from a string (starting from the left).
SELECT LEFT('thecodehubs.com', 3) AS ExtractString;

Output: the

LEN() It returns the length of a string.
SELECT LEN('thecodehubs.com') AS LENGTH;

Output: 15

LOWER() It converts a string to lower case.
SELECT LOWER('THECODEHUBS') AS LowerCase;

Output: thecodehubs

LTRIM() It removes leading spaces from a string.
SELECT LTRIM('    CodeHubs') AS LeftTrimmedString;

Output: CodeHubs

NCHAR() It returns the Unicode character based on the number code.
SELECT NCHAR(97) AS NumCodeToUnicode;

Output: a

PATINDEX() It returns the position of a pattern in a string.
SELECT PATINDEX('%code%', 'thecodehubs') AS Position;

Output: 4

QUOTENAME() It returns a Unicode string with delimiters added to make the string a valid SQL Server delimited identifier.
SELECT QUOTENAME('order');

Output: [order]

REPLACE() It replaces all occurrences of a substring within a string, with a new substring.
SELECT REPLACE('thecodehubs Mrticles', 'M', 'A');

Output: thecodehubs Articles

REPLICATE() It repeats a string a specified number of times.
SELECT REPLICATE('thecodehubs ', 3);

Output: thecodehubs thecodehubs thecodehubs

REVERSE() It reverses a string and returns the result.
SELECT REVERSE('thecodehubs') AS ReversedString;

Output: sbuhedoceht

RIGHT() It extracts a number of characters from a string (starting from the right).
SELECT RIGHT('thecodehubs.com', 3) AS ExtractString;

Output: com

RTRIM() It removes trailing spaces from a string.
SELECT RTRIM('CodeHubs   ') AS RightTrimmedString;

Output: CodeHubs

SPACE() It returns a string of the specified number of space characters.
SELECT SPACE(5);

Output:

STR() It returns a number as a string.
SELECT STR(123);

Output: 123

STUFF() It deletes a part of a string and then inserts another part into the string, starting at a specified position.
SELECT STUFF('thecodehubs.in', 13, 2, 'com');

Output: thecodehubs.com

SUBSTRING() It extracts some characters from a string.
SELECT SUBSTRING('thecodehubs', 4, 4) AS ExtractString;

Output: code

TRIM() It removes leading and trailing spaces, or other specified characters from a string.
SELECT TRIM('    CodeHubs   ') AS TrimmedString;
--OR 
SELECT TRIM('.! ' FROM '    !CodeHubs.   ') AS TrimmedString;

Output: CodeHubs

UNICODE() It returns the Unicode value for the first character of the input expression.
SELECT UNICODE('CodeHubs');

Output: 67

UPPER() It converts a string to the UPPER CASE.
SELECT UPPER('thecodehubs') AS UPPER;

Output: THECODEHUBS

 

Also, check Views In SQL

Yasin Panwala

Yasin Panwala is a Web Developer and Author at TheCodeHubs. He has experience in Web Developing and Designing and also in Writing. He has got his skills in working on technologies like .NET Core, ADO.NET, AJAX, Angular, AngularJS, ASP.NET, ASP.NET MVC, Bootstrap, C#, CSS, Entity Framework, Express.js, GraphQL, HTML, JavaScript, JQuery, JSON, LINQ, Microsoft Office, MongoDB, MySQL, Node.js, PostgreSQL, SQL, SQL Server, TypeORM, TypeScript, Visual Basic .NET, Web API. He also got his skills in working with different integration and some known versioning tools. He is always ready to learn new things and he always tries his best on tasks that are assigned to him and gives the best possible outputs.

View Comments

  • Thanks for another informative website. The place else may I get that
    type of information written in such an ideal approach?
    I have a project that I'm just now working on, and I have been at the look out for such information.

  • Great beat ! I would like to apprentice while you amend your site, how can i subscribe for a blog
    site? The account helped me a acceptable deal. I had been a
    little bit acquainted of this your broadcast offered bright
    clear idea

  • Appreciating the commitment you put into your blog and detailed information you provide.
    It's good to come across a blog every once in a
    while that isn't the same unwanted rehashed material.
    Wonderful read! I've saved your site and I'm including your RSS feeds to my Google account.

  • I like the helpful info you provide in your articles.
    I'll bookmark your weblog and check again here frequently.
    I am quite sure I'll learn plenty of new stuff right here!
    Best of luck for the next!

  • you're truly a just right webmaster. The web site loading pace is incredible.

    It sort of feels that you're doing any unique trick. In addition, The contents
    are masterwork. you've done a excellent job in this matter!

  • I was wondering if you ever thought of changing the layout of your blog?

    Its very well written; I love what youve got to say. But maybe you could a little more
    in the way of content so people could connect with it better.
    Youve got an awful lot of text for only having one or two pictures.

    Maybe you could space it out better?

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

2 years ago