Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WHERE Field = @var problem - URGENT SOLUTION NEEDED - 1

Status
Not open for further replies.

mlibeson

Programmer
Mar 6, 2002
311
US
my code looks something like:

Code:
DECLARE @var AS VARCHAR (20);

SET @var = 'A/B';

SELECT * FROM Tablename
WHERE LTRIM(RTRIM(Field)) = LTRIM(RTRIM(@var));

It does not return any rows.

It does return rows if I write the code as follows:

Code:
SELECT * FROM Tablename
WHERE LTRIM(RTRIM(Field)) = 'A/B';

Can anyone help me with this problem?


Michael Libeson
 
What type is Field? Also there is no need to TRIM varchar field or variable it is already trimmed. What happens with:
Code:
DECLARE @var AS VARCHAR (20)
SET @var = 'A/B'

SELECT * FROM Tablename
       WHERE Field = @var

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
works for me

Code:
create table Tablename (Field varchar(20))
insert Tablename values ('A/B')

DECLARE @var AS VARCHAR (20);

SET @var = 'A/B';

SELECT * FROM Tablename
WHERE LTRIM(RTRIM(Field)) = LTRIM(RTRIM(@var));
why are you trimming anyway don't you know varchars get trimmed
Code:
SELECT * FROM Tablename
WHERE Field = @var;

Denis The SQL Menace
SQL blog:
 
I am aware that varchar does not require trim but I was having problems and was trying to eliminate scenarios.

The Field type is varchar as well.

Michael Libeson
 
Silly me.

In a rush to modify existing code to search based on ID and change the search to the description, I never changed the length of varchar so the string was being truncated.

Oh the shame for even posting this issue.

This one goes on the wall of shame.

Thanks for your help anyway.

I appreciate your quick response and efforts.


Michael Libeson
 
I suspect that there are 'non=printable' characters contained within the data. Take a look at this example (where the data has a Tab character at the end of it.

Code:
Declare @Temp Table(Data VarChar(20))

Insert Into @Temp Values('A/B' + Char(9))

Declare @var VarChar(20)

Set @Var = 'A/B'

Select 'With = compare' As CompareType, 
       * 
From   @Temp 
Where  Data = @Var

Select 'With Like compare' As CompareType,
       * 
From   @Temp 
Where  Data Like '%' + @var + '%'

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Since you say your code is SOMETHING like that, that could be the issue - we aren't working with the real data.

Try this with your real data:
Code:
DECLARE @var AS VARCHAR (20)
SET @var = 'A/B'

PRiNT 'This is the value trimmed ' + LTRIM(RTRIM(@var)) + '.'
PRINT 'This is the value untrimmed ' + @var + '.'

Are they the same? Notice that there should be one space between the end of the first string and the variable and no space after the variable and the period.

Also, using semi-colons after each line isn't common in SQL Server, but it is common in Oracle. You are using SQL Server, aren't you?

-SQLBill

Posting advice: FAQ481-4875
 
Looks like george and I were typing our responses when you found and posted your solution. Oh well, it's all in the timing.

I'm glad you solved the issue AND posted what happened.

-SQLBill

Posting advice: FAQ481-4875
 
SQLDenis,

Thanks for that bit of knowledge....I wasn't aware of that need as I'm mostly a SQL Server 2000 guy and just starting with 2005.

-SQLBill

Posting advice: FAQ481-4875
 
SQLDenis,

Thanks for the info. I have always used the semi-colon as I thought it was part of the standard SQL even though SQL Server 2000 and prior did not require it. It looks like it will be required going forward so I am glad I have already been using them in practice.


Michael Libeson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top