Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 sql string comparison problem - SOLVED

Author  Topic 

allanim
Starting Member

7 Posts

Posted - 2010-11-08 : 03:06:22
Hello everybody.

I have a little problem with a sql string comparison.

I tried to use the following code to compare a row to a variable to
get a result..

The code works fine if the variable is a number like: 12345
But if I use letters and numbers like: AZ1234 It doesn't work.

 $database->setQuery( 'SELECT task_id FROM #_projects WHERE title = '.$jid );


What am I missing..??

Hope you can help me..

Best regards
Allan.

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-11-08 : 03:13:51
Are you using MS-SQL Server ?
Go to Top of Page

allanim
Starting Member

7 Posts

Posted - 2010-11-08 : 03:18:31
Hi, I am using Apache MySQL.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-11-08 : 04:39:12
try

$database->setQuery( 'SELECT task_id FROM #_projects WHERE title = ''.$jid''' );

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-11-08 : 04:41:07
No. He should use the LIKE operator.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

allanim
Starting Member

7 Posts

Posted - 2010-11-08 : 05:16:46
Hi Madhivanan, Thanks for your suggestion, but that gave me a parse error..


Hi webfred, Thanks for your suggestion, I have tried the LIKE operator, but it gave me the same result as =

Best regards
Allan
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-11-08 : 05:22:00
Yes. When using LIKE then you need also to use a joker.

I don't know sure how write it in PHP or whatever you are using but the builded query in your example should look like this:
SELECT task_id FROM #_projects WHERE title LIKE '%12345'


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

allanim
Starting Member

7 Posts

Posted - 2010-11-08 : 06:27:20
Thanks webfred, I have tried, but can't get a working syntax with the PHP.

I could really use some help on this one.

Best regards
Allan
Go to Top of Page

allanim
Starting Member

7 Posts

Posted - 2010-11-08 : 07:16:38
Problem solved with following code:

$database->setQuery("SELECT task_id FROM #_projects WHERE title = '".$jid."'");

Best regards
Allan
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-11-08 : 07:20:47
And where is the joker?
$database->setQuery("SELECT task_id FROM #_projects WHERE title = '%".$jid."'");

Without joker (%) you are not solving your problem to find 12345 in AZ12345


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

allanim
Starting Member

7 Posts

Posted - 2010-11-08 : 07:52:06
Hi webfred,

The joker is not needed, I wanted to match to entire string with characters and numbers combined.
With out the quotes, the request returned empty when characters and numbers combined. Only worked with numbers..

Best regards
Allan
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-11-08 : 08:01:29
So your 'It's not working' in your first post doesn't mean you wanted to search a part of the string.
The meaning was a type mismatch?

Then you should not use the LIKE operator
and what you have shown above:

$database->setQuery("SELECT task_id FROM #_projects WHERE title = '".$jid."'");

is fine.

My bad was, I have not noticed in your posted solution there was no LIKE anymore...




No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

allanim
Starting Member

7 Posts

Posted - 2010-11-08 : 08:26:52
Yes, Should properly have explained my self a little better :-)

Thank you very much for your help..

Best regards
Allan
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-11-08 : 08:44:13
welcome


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -