Friday, March 23, 2012

Query for finding trailing spaces in a column

It seems like our application inserting trailing spaces
into the varchar field (spaces before and after the
string). Can anyone help me with a query to find out which
rows have trailing spaces in a column '
Thanks for any help......SET NOCOUNT ON
CREATE TABLE #splunge
(
foo VARCHAR(10)
)
GO
INSERT #splunge SELECT 'val1'
INSERT #splunge SELECT 'val2 '
INSERT #splunge SELECT ' val3'
INSERT #splunge SELECT ' val4 '
GO
-- leading spaces:
SELECT * FROM #splunge WHERE LTRIM(foo) != foo
-- trailing spaces:
SELECT * FROM #splunge WHERE DATALENGTH(RTRIM(foo))!=DATALENGTH(foo)
GO
DROP TABLE #splunge
GO
http://www.aspfaq.com/
(Reverse address to reply.)
"John" <anonymous@.discussions.microsoft.com> wrote in message
news:2796201c4642a$94e8df30$a601280a@.phx
.gbl...
> It seems like our application inserting trailing spaces
> into the varchar field (spaces before and after the
> string). Can anyone help me with a query to find out which
> rows have trailing spaces in a column '
> Thanks for any help......|||Thanks........

>--Original Message--
>SET NOCOUNT ON
>CREATE TABLE #splunge
>(
> foo VARCHAR(10)
> )
>GO
>INSERT #splunge SELECT 'val1'
>INSERT #splunge SELECT 'val2 '
>INSERT #splunge SELECT ' val3'
>INSERT #splunge SELECT ' val4 '
>GO
>-- leading spaces:
>SELECT * FROM #splunge WHERE LTRIM(foo) != foo
>-- trailing spaces:
>SELECT * FROM #splunge WHERE DATALENGTH(RTRIM(foo))!
=DATALENGTH(foo)
>GO
>DROP TABLE #splunge
>GO
>--
>http://www.aspfaq.com/
>(Reverse address to reply.)
>
>
>"John" <anonymous@.discussions.microsoft.com> wrote in
message
> news:2796201c4642a$94e8df30$a601280a@.phx
.gbl...
which[vbcol=seagreen]
>
>.
>|||select * from TheTable where theColumn like '% '
"John" <anonymous@.discussions.microsoft.com> wrote in message
news:2796201c4642a$94e8df30$a601280a@.phx
.gbl...
> It seems like our application inserting trailing spaces
> into the varchar field (spaces before and after the
> string). Can anyone help me with a query to find out which
> rows have trailing spaces in a column '

No comments:

Post a Comment