0

For example, the stored string could be '123456789', but the user might input '123 456 789' to search, or "1234 56789". How to handle the space in different location? I use SQL Server2008 Express.

2 Answers 2

1

You would use replace:

where str = replace(@str, ' ', '')

@str is the user input and str is the column inthe table.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for answering. But how about if the stored string is "123 456 789", but user input is "123456789"?
@Russj, replace(str, ' ', '') = replace(@str, ' ', '') should take care of both the sides
1

Can try using Trim function to remove white space from a field.

select

SELECT  TRIM(fieldname)
,            LTRIM(fieldname)
,            RTRIM(fieldname)
,            LTRIM(RTRIM(fieldname))
FROM     tablename

update

UPDATE
TableName
SET
ColumnName = LTRIM(RTRIM(ColumnName))

TRIM() will remove both leading and trailing white spaces;
LTRIM() removes leading white spaces (i.e. from the beginning of a string);
RTRIM() removes trailing white space (i.e. from the end of a string);
If TRIM() is not supported, then LTRIM(RTRIM()) will achieve the same result.

SELECT LTRIM(RTRIM(YourColumn)) FROM YourTable

For more information : TRIM FUNCTION

1 Comment

How to trim spaces in the middle?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.