Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Substring function example to get particular part of string in SQL Server

Apr 23, 2012
Introduction: 

In this article I will explain what is the substring function and uses of substring function in SQL Server.
Description:
During write query in SQL server I got requirement like split the string and get only some part of it to achieve this functionality I realized it’s better to use SubString function. 

SubString function:

This function is used to get the specified length of string based on specification of start position and required length of string. Generally SubString function will take 3 arguments.

Declaration of SubString function:

SUBSTRING(string, startIndex, length)

In this function

1st Argument specifies the string value

2nd Argument specifies the start position of string from where part of string begins

3rd Argument specifies number of characters to be returned from the specified string

Examples of SubString Function:

EX1:

SELECT SUBSTRING('Suresh Dasari',1,10)

Output: 'Suresh Das'

In above query we declared string as 'Suresh Dasari', starting position of string as 1 and number of characters returned from string as 10

EX2:

SELECT SUBSTRING('Suresh Dasari',3,7)

Output:  'resh Da'

In above query string as 'Suresh Dasari', starting position of string as 3 and number of characters returned from string as 7

Ex3:

Suppose if we want split the column in table then our function declaration will be like this

SELECT SUBSTRING(ColumnName,1,4) as 'SubString' FROM TableName

ex: SELECT SUBSTRING(UserName,1,4) as 'SubString' FROM UserInformation

Above query will return UserName starting from 1 and return 4 characters from string like this

Output:   

SubString
Sure
Mahe
Naga
Mahe

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

8 comments :

Unknown said...

hi friend as i am developing a quicker like website,and i need of count the categories.
for example:vechicle(102) the 102 value will updated by the database,give me any solution friend....:)
by jayakumar

Shashi Tripathi said...

hi,
i want to search item in sub string
for example
the string is "a good person never mistake"

and i want to search "good mistake"

how it possible please reply me ....



Anonymous said...

i'm trying to send msg from 1 pc(with Windows7) to another in LAN using command prompt

but getting error no "1722 :getting Session name"

Anonymous said...

how to display the windows flyouts in desktop application in C#...

Unknown said...

good article...i have a table and full name is like 'sachin ramesh tendulkar' ..and i have to extract sachin as firstname and ramesh as middlename and
tendulkar as lastname..this is one interview question i have faced...plz give me answer how to do it..plz mail me to janardhancse560@gmail.com

Anonymous said...

hi

Anonymous said...

great man... keep up... yr suggestions really help me a lot...

Anonymous said...

Thanks

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.