Click here to Skip to main content
15,911,789 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
string str = "select * from TblPatientReg where concat('PID','PName','RoomNo') like '%" + valueToSearch + "%'";


am writing this query to search the patient by id,name,roomnumber but am facing issue like above subject.

can you please guide me .

What I have tried:

<pre>string str = "select * from TblPatientReg where concat('PID','PName','RoomNo') like '%" + valueToSearch + "%'";


am writing this query to search the patient by id,name,roomnumber but am facing issue like above subject.

can you please guide me
Posted
Updated 17-May-20 4:29am

I googled "concat sql 2008" and this is the top result

How do I use the CONCAT function in SQL Server 2008 R2? - Stack Overflow[^]

It's always best to google your problem before asking a question.
 
Share this answer
 
Comments
Member 11297177 17-May-20 10:26am    
thank you so much , which you have refereed link is worked fine. see below query worked.

string str = "select * from TblPatientReg where (PID+','+PName+''+RoomNo) like '%" +txtsearch.Text + "%'";
Maciej Los 18-May-20 3:44am    
5ed!
The first thing to note is simple: don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

The second is: CONCAT works fine on my system, even in the form you show:
SQL
SELECT * FROM MYTABLE WHERE concat('PID','PName','RoomNo') LIKE '%a%'

So ... you probably aren't using SQL Server, or MySql , or ... and need to google for CONCAT and the name of your DB engine...

The second is that that code won't do what you want, even on SQL Server, or MySQL, or ... it will either return all rows or none. This bit:
SQL
concat('PID','PName','RoomNo') like
means you will always compare the fixed string "PIDPNameRoomNo" with your search string instead of the actual column data. Probably you want something like
SQL
concat(PID, PName, RoomNo) like
, but you may need some CASTing in there depending on your DB design.
 
Share this answer
 
Comments
Member 11297177 17-May-20 8:52am    
am using sql server 2008 but concat function not recognizing
OriginalGriff 17-May-20 9:07am    
CONCAT was added to SQL Server 2012, so you can't use it in earlier versions.
You will have to use string + string + string instead, probably with some CAST operations to convert your columns to strings.
Member 11297177 17-May-20 10:27am    
your correct concat function is not support supporting for 2008 R2 so i have used below query worked fine .

string str = "select * from TblPatientReg where (PID+','+PName+''+RoomNo) like '%" +txtsearch.Text + "%'";
OriginalGriff 17-May-20 10:38am    
Read what I said to start with: if your code leaves one instance of SQL Injection like that, at some point your best mate is going to delete your DB just to see the look on your face. This is not a joke.
Member 11297177 17-May-20 17:54pm    
sure , got it .

thanks
Please find the below query is working fine for my issue for sql server 2008r2

string str = "select * from TblPatientReg where (PID+','+PName+''+RoomNo) like '%" +txtsearch.Text + "%'";
 
Share this answer
 
Comments

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900