Solved

Patindex Query - Urgent

Posted on 2004-09-28
12
457 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
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
 

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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 50 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
powershell and sql server - alerting 7 77
Update in Sql 7 30
business objects to connect to MSSQL 6 62
SQL Server - Check Constraint or NOT NULL? 11 68
This article describes some very basic things about SQL Server filegroups.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
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…

920 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

11 Experts available now in Live!

Get 1:1 Help Now