1

I have below sql Query

Select * from usertable where username = 'xyz (space)';

It is giving me the result which is wrong the result has to come only when i not add space at end because in database there is no space.

Is there any function or how can i do that.

Sharmaji TM
  • 199
  • 1
  • 13
  • you running query from php – Muhammad Younas Jun 16 '16 at 07:54
  • no it is in sql server 2008 – Sharmaji TM Jun 16 '16 at 07:55
  • @Sharmaji use Trim – Malcolm Jun 16 '16 at 07:56
  • Please give me sample query actualy i don't want the result if there is space at end. – Sharmaji TM Jun 16 '16 at 07:58
  • Possible duplicate of [SQL Server 2008 Empty String vs. Space](http://stackoverflow.com/questions/1399844/sql-server-2008-empty-string-vs-space) – Lasse V. Karlsen Jun 16 '16 at 08:35
  • There are several duplicates here on SO regarding this phenomenon: [this](http://stackoverflow.com/questions/1399844/sql-server-2008-empty-string-vs-space), [this](http://stackoverflow.com/questions/3938566/how-can-i-make-sql-server-return-false-for-comparing-varchars-with-and-without-t), [this](http://stackoverflow.com/questions/1143313/why-would-sqlserver-select-statement-select-rows-which-match-and-rows-which-match), etc. – Lasse V. Karlsen Jun 16 '16 at 08:36

6 Answers6

1

If the data is CHAR data with trailing spaces you could use the RTRIM function like this;

select * from usertable where RTRIM(username) = 'xyz'

EDIT: In order to match including trailing spaces you can use the DATALENGTH function, like this;

select * from usertable where username = 'xyz ' AND
                              DATALENGTH(username) = DATALENGTH('xyz ')
Rhys Jones
  • 5,348
  • 1
  • 23
  • 44
  • data is varchar and rtrim will remove space from database but what i want is if there is space at end then result will not come. – Sharmaji TM Jun 16 '16 at 07:57
  • ..thanks its working but change query like below: select * from usertable where username = 'xyz ' AND DATALENGTH(username ) = DATALENGTH('xyz ') – Sharmaji TM Jun 16 '16 at 09:11
1

I hope you want to filter the records those have the space in the last position.

DECLARE @TestTable TABLE (Data VARCHAR(20));
INSERT INTO @TestTable (Data) VALUES ('xyz'), ('Sharma ');

SELECT * FROM @TestTable WHERE Data = 'Sharma ';
SELECT * FROM @TestTable WHERE Data = 'Sharma';

you will get the same result for WHERE Data = 'Sharma ' and 'Sharma'

Using SUBSTRING(Data, DATALENGTH(Data), 1) you can get the last character of the column and add the condition in WHERE clause will solve your problem:

DECLARE @TestingValue AS VARCHAR(20) = 'Sharma'; -- 'xyz' -- 'Sharma '

SELECT * FROM @TestTable WHERE Data = @TestingValue AND SUBSTRING(@TestingValue, DATALENGTH(@TestingValue), 1) = ' ' ;

The above block return result if you set 'Sharma ' as parameter value.

Arulkumar
  • 12,966
  • 14
  • 47
  • 68
1

This is because SQL Server follows ANSI 92 rules which stipulates that:

SQL Server follows the ANSI/ISO SQL-92 specification (Section 8.2, <Comparison Predicate>, General rules #3) on how to compare strings with spaces. The ANSI standard requires padding for the character strings used in comparisons so that their lengths match before comparing them. The padding directly affects the semantics of WHERE and HAVING clause predicates and other Transact-SQL string comparisons. For example, Transact-SQL considers the strings 'abc' and 'abc ' to be equivalent for most comparison operations.

(my emphasis)

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
0

If you don't want the result if there is space at end you can use below query

select * from usertable where username not like '% '
Malcolm
  • 1,801
  • 3
  • 21
  • 48
0

Or this one:

    select * from usertable where right(username,1) <> ' '
Djacks007
  • 11
  • 6
0

Hi this code works for me........

select * from usertable where username = 'xyz (Space) ' AND
                              DATALENGTH(username) = DATALENGTH('xyz (Space) ')
Sharmaji TM
  • 199
  • 1
  • 13