Patindex Query - Urgent

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.
venkatraman_rAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
BillAn1Connect With a Mentor Commented:
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
 
peter57rCommented:
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
 
venkatraman_rAuthor Commented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
BillAn1Commented:
this is probably best done with dynamic SQL

exec ('select * from track_master  where track_id in (' + '36,37'+ ')')
0
 
venkatraman_rAuthor Commented:
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
 
BillAn1Commented:
"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
 
venkatraman_rAuthor Commented:
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
 
BillAn1Commented:
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
 
venkatraman_rAuthor Commented:
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
 
venkatraman_rAuthor Commented:
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
 
BillAn1Commented:
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
 
venkatraman_rAuthor Commented:
ok..Thanks for your help.

Regards,
Venkat.
0
All Courses

From novice to tech pro — start learning today.