Searching the strings with leading spaces and trailing spaces in the sql server


Hi Folks,

We have issues in the production at our end where the master table is update with leading spaces and it is causing the incorrect results as it master table and configuration is present in those tables.

I quickly checked the leading spaces in the table using the below query

We have created sample tables for this demo

use tempdb

go

create table #tempo

(

id int,

name varchar(100)

)

insert #tempo

select 1,’chaitu’

union all

select 2,’pradee’

union all

select ‘3’,’ krishna’

select * from #tempo

Below is the query for getting the leading spaces in the name column

select * from #tempo where LEFT(name,1)=”

For trailing spaces

use tempdb

go

create table #tempo

(

id int,

name varchar(100)

)

insert #tempo

select 1,’chaitu’

union all

select 2,’pradee’

union all

select ‘3’,’krishna ‘ à trailing space is inserted

select * from #tempo

Below is the query for getting the trailing spaces in the name column

select * from #tempo where right(name,1)=”

Please let us know if you have other options

Please check and let us know if you have any concerns on this.

Thanks for viewing this.

Regards,

Chaitanya

Visit site: http://www.sqlblogging.com

Send an Email: sqlblogging

Twitter: @chaitanya512

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: