473,473 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SQL: Specific LIKE statement

If I need to load all records with name John, than I can use "Where
Name='John'";
If I need to load all records which contains pattern Jo, than I can use
"Where Name like '%Jo%'";

But I need to load all records where field Name is one of the names in
this string: "John is director, Jack is programmer, Jim is Analyst, James is
manager".

So I need to load all fields from table Names where field Name is John,
Jack, Jim and James.

Ho to do that with SQL?

Oct 16 '08 #1
2 853
"Toni" <to****@hotmail.comwrote in message
news:gd**********@sunce.iskon.hr...
If I need to load all records with name John, than I can use "Where
Name='John'";
If I need to load all records which contains pattern Jo, than I can use
"Where Name like '%Jo%'";

But I need to load all records where field Name is one of the names in
this string: "John is director, Jack is programmer, Jim is Analyst, James
is manager".

So I need to load all fields from table Names where field Name is John,
Jack, Jim and James.

How to do that with SQL?
Firstly, since you've posted in the ASP.NET newsgroup as opposed to one of
the SQL Server newsgroups, I'm assuming that you need to do this in one of
the ASP.NET languages rather than T-SQL...

string strWhereClause = String.Empty;
string strRaw = "John is director, Jack is programmer, Jim is Analyst, James
is manager";
string[] strSplit = strRaw.Split(',');
foreach (string strName in strSplit)
{
strWhereClause += "'" + strName.Trim().Split(' ')[0].Trim() + "',";
}
strWhereClause = "WHERE [Name] IN(" + strWhereClause.TrimEnd(',') + ")";

N.B. the above isn't particularly efficient or robust, but it should be
enough to get you started...

I'm sure that there are "cleverer" methods using RegEx etc...

Secondly, you should avoid using any of the SQL Server reserved words (e.g.
Name) for your own objects...

Finally, it's best to avoid dynamic SQL in ASP.NET apps because of the risk
of SQL injection...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Oct 16 '08 #2
there are several options.

use the "or". where name like 'jo%' or name like 'jack%'
load the name into a temp table or table variable and join. if you don;t
need wild cards use
where name in (select name from @tbl)
if you do then just use a join

join @tbl on mytable.name like @tbl.name
-- bruce (sqlwork.com)


Toni wrote:
If I need to load all records with name John, than I can use "Where
Name='John'";
If I need to load all records which contains pattern Jo, than I can use
"Where Name like '%Jo%'";

But I need to load all records where field Name is one of the names in
this string: "John is director, Jack is programmer, Jim is Analyst, James is
manager".

So I need to load all fields from table Names where field Name is John,
Jack, Jim and James.

Ho to do that with SQL?
Oct 16 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Robert Brown | last post by:
suppose I have the following table: CREATE TABLE (int level, color varchar, length int, width int, height int) It has the following rows 1, "RED", 8, 10, 12 2, NULL, NULL, NULL, 20...
18
by: Robin Lawrie | last post by:
Hi again, another problem! I've moved from an Access database to SQL server and am now having trouble inserting dates and times into seperate fields. I'm using ASP and the code below to get the...
6
by: Hype | last post by:
Hi, How can we specify the package names for sql stored procedures instead of system generated names ? (Db2 V8.2 on Windows.) thanks.....
18
by: Rhino | last post by:
I am having a problem with a nested SQL Procedure on DB2 V8.2.1 on Windows. This simple-looking proc is giving me this error message when I try to build it in the Development Center: ...
3
by: Guy Debord | last post by:
Hello all, I know that this is a long shot, but I have a problem which someone reading this group *may* just be able to shed some light on. We have a new internal personnel planner/attendance...
138
by: Ian Boyd | last post by:
i've been thrown into a pit with DB2 and have to start writing things such as tables, indexes, stored procedures, triggers, etc. The online reference is only so helpful. The two pdf manuals are...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
0
by: mike | last post by:
Problem: the snapshot output for dynamic SQL shows some SELECT statements executed many times but all three CPU counters (total/system/user) in the dynamic-sql-snapshot are zero, but most other...
0
by: Frank Swarbrick | last post by:
We're trying to use SQL Assist in the DB2 Control Center for DB2/LUW 9.5 and we are getting the following error: "Routine "SYSIBM.SQLTABLES" (specific name "TABLES") has returned an error SQLSTATE...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.