Solved

Patindex Query - Urgent

Posted on 2004-09-28
12
438 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
  • 6
  • 5
12 Comments
 
LVL 77

Expert Comment

by:peter57r
Comment Utility
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
Comment Utility
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
Comment Utility
this is probably best done with dynamic SQL

exec ('select * from track_master  where track_id in (' + '36,37'+ ')')
0
 

Author Comment

by:venkatraman_r
Comment Utility
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
Comment Utility
"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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 17

Expert Comment

by:BillAn1
Comment Utility
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
Comment Utility
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 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
ok..Thanks for your help.

Regards,
Venkat.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now