Introduction:
In this article I will explain what is the substring function and uses of substring function in SQL Server.
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. |
|||
|
|||
8 comments :
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
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 ....
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"
how to display the windows flyouts in desktop application in C#...
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
hi
great man... keep up... yr suggestions really help me a lot...
Thanks
Note: Only a member of this blog may post a comment.