How to use Trim() Function in SQL server 2005 ?


Tuesday, November 24, 2009

There is no perticular function like Trim in SQL server but you can achieve Trim like functionality by doing Left Trim and Right Trim, to get complete trimed Text.

LTRIM is used to : Trim Text form left hand side or in other word remove space form left hand side.
RTRIM is used to : Trim Text from Right hand side or in other word remove space form Right hand side.

Example :

You can Use this query directly in SQL Server 2005

SELECT    *
FROM view_rapdata_full_member_pull
WHERE LTRIM(RTRIM(First_Name)) +' '+ LTRIM(RTRIM(Last_Name))
like '%Rajesh Singh%'
or
LTRIM(RTRIM(Last_Name)) +' '+ LTRIM(RTRIM(First_Name))
like '%
Rajesh Singh%'


You can Use this query in C# code to make search.

SELECT    *
FROM view_rapdata_full_member_pull
WHERE LTRIM(RTRIM(First_Name)) +' '+ LTRIM(RTRIM(Last_Name))
like '%" + txtsearch.Text + "%'
or
LTRIM(RTRIM(Last_Name)) +' '+ LTRIM(RTRIM(First_Name))
like '%" + txtsearch.Text + "%'


Hope this post will help you,
if yes please put comment below of this page,
Rajesh Singh,
Asp.Net Developer
Indianic Infotech Ltd (India)
rajesh@indianic.com

No comments :