SIGN UP MEMBER LOGIN:
   
ARTICLE

STR() and SUBSTRING() function use in Transact-SQL

Posted by Sapna Malik Articles | SQL Server August 25, 2010
In this article, We will see how to use STR() and SUBSTRING() in Transact-SQL.
Reader Level:

HTML clipboard

In this article, We will see how to use STR() and SUBSTRING() in Transact-SQL.

STR() Function:-The STR() Function is returns character data converted from numeric data. The STR function converts a numeric value into a string.

Syntax:-

USE DataBaseName
GO
SELECT
STR ( Expression,STARTING_VALUE,DECIMAL_VALUE )
GO

Example:-

USE Data
GO
SELECT
STR(4634665323.532375283,10,11)
GO

Output:-

str1.bmp

SUBSTRING() Function:-

The SUBSTRING() function is returns the specified number of characters from the string starting at the specified position.

Substring function like the three arguments. The first argument can be a column name, character or binary string etc. The second argument specifies the position at which the substring should begin. The third argument specifies the length, in number of characters, of the string to be returned. 

Syntax:-

USE DataBaseName
GO
SELECT
SUBSTRING(Column1,Start,Lenght)
FROM TableName
WHERE Column1 = 'value';
GO

Example:-

USE Data
GO
SELECT
SUBSTRING(StudentName,1,5)
FROM StudentRecords
WHERE StudentName = 'Sapna Malik';
GO

USE Data
GO
SELECT
SUBSTRING(StudentName,6,15)
FROM StudentRecords
WHERE StudentName = 'Sapna Tanwar';
GO

Output:-

 str2.bmp

Login to add your contents and source code to this article
This Feature is Sponsored By
post comment
COMMENT USING
TRENDING UP