Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Patindex Query - Urgent

Posted on 2004-09-28
12
Medium Priority
?
510 Views
Last Modified: 2011-10-03
Hi,
Please see this query:

select track_id,track_name  from track_master where patindex('%'+rtrim(ltrim(str(track_id)))+'%', '36,37') <> 0

This query returns me the following results

track_id    track_name          
----------- --------------------
3           HRMS
6           Process Manufacturin
7           Projects
36          Sun
37          Microsoft

But I want to have only these results:

track_id    track_name          
----------- --------------------
36          Sun
37          Microsoft

What change should I do in this query?

Thanks for any assistance.

Regards,
Venkat.
0
Comment
Question by:venkatraman_r
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 12168272
Hello venkatraman_r,

I'm not a T_SQL developere but it seems to me you need to force the string you are searching for to be 2 didgits in this case.


I'm guessing at something like:

str(trackid\10) & str(trackid mod 10)


Pete
0
 

Author Comment

by:venkatraman_r
ID: 12168289
The values for track id will be passed from another screen as parameters. There could be any number of parameters and the ids could be different.

The track id could also be 1,2 or simply 1 or may be 1,2,4,36

Thanks,
Venkat.
0
 
LVL 17

Expert Comment

by:BillAn1
ID: 12168325
this is probably best done with dynamic SQL

exec ('select * from track_master  where track_id in (' + '36,37'+ ')')
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:venkatraman_r
ID: 12168374
Hi,

I do not want to use the in statement. Because I will not know the parameters passed to this statement and the number of parameters being passed.

Its actually the track id parameter list will be stored in a variable and the variable will hold the values and will be used in the statement.

Thanks,
Venkat.
0
 
LVL 17

Expert Comment

by:BillAn1
ID: 12168392
"I do not want to use the in statement. Because I will not know the parameters passed to this statement and the number of parameters being passed"
what does that mean? why does not knowing how many parameters are passed cause you a problem? you just insert the variable into the code - e.g.

exec ('select * from track_master  where track_id in (' + @MyPassedParameters+ ')')
0
 

Author Comment

by:venkatraman_r
ID: 12168475
Ok. Let me put it this way.

If I use the in clause, the text will be treated as string. For eg:

select * from track_master  where track_id in (' 36,15 ')

will return Zero rows as I do not have any track named '36,15'. Besides track id is integer. So I am forced to look upon the patindex option. Patindex can take the string values still can be used to retrieve values from the integer column.

Hope I make sense.

Thanks,
Venkat.
0
 
LVL 17

Expert Comment

by:BillAn1
ID: 12168499
no it wont. I think you are missing the point of dynamic SQL.
It will dynamically create the required SQL for you when you pass in the string '36,15' it will create a piece of SQL that is

select * from track_maseter where track_id in (36,15)

and then execute this SQL


e.g.
exec ('select * from track_master  where track_id in (' + '36,37'+ ')')

first off, evaluates the concatination to result in the string 'select * from track_master  where track_id in (36,37)'
then it executes this string.

0
 

Author Comment

by:venkatraman_r
ID: 12168696
I guess I missed to tell you this.

When the track id parameter is being passed to sql server from java, it is passed as string.
The data type column in SQL server for track id field is INT.
Hence a conversion is definitely required, which is being done using patindex.

Any help?
0
 
LVL 17

Accepted Solution

by:
BillAn1 earned 200 total points
ID: 12168744
how many times do I have to explain? LOOK at the example given, maybe even try running it........
it takes an input string, and DYNAMICALLY CREATES A PIECE OF SQL which has the correct datatypes etc.
if you give it an input of a string '36,37'
it will CREATE a piece of SQL which is
select * from track_master  where track_id in (36,37)
i.e. there are NO Strings in this SQL - it has dynamically created the most efficient SQL you need, then the exec procedure actually executes this SQL.
0
 

Author Comment

by:venkatraman_r
ID: 12168847
I agree with you. I tried and it worked. Thanks for your assistance. You get the points.

But just for knowledge sake:

What if I give the statement as below:

exec ('select * from track_master  where track_id in (' + '3,bc'+ ')')

Will it automatically take the field type as 'varchar'?

Thanks again.

Regards,
Venkat.
0
 
LVL 17

Expert Comment

by:BillAn1
ID: 12169109
nope, in that case it gets messier -
you will get a piece of SQL wich will become
select * from track_master  where track_id in (3,bc)   - this will fail, beacuse it will interpret bc as being a column name, not a value.
instead you would have to have the resulting SQL be
select * from track_master  where track_id in ('3','bc')
to do this, you need to do one of 2 thinigs - either modify your java so that it puts '' around each entry in the list, or else try this (looks a bit funky!)

exec ('select * from track_master  where track_id in (''' + replace('3,bc',',',''',''')+ ''')')
0
 

Author Comment

by:venkatraman_r
ID: 12169151
ok..Thanks for your help.

Regards,
Venkat.
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question