views:

45

answers:

3

I want to compare and select a field from DB using Like keyword or any other technique.

My query is the following:

SELECT * FROM Test WHERE name LIKE '%xxxxxx_Ramakrishnan_zzzzz%';

but my fields only contain 'Ramakrishnan'

My Input string contain some extra character xxxxxx_Ramakrishnan_zzzzz

I want the SQL query for this. Can any one please help me?

+2  A: 

You mean you want it the other way round? Like this?

Select * from Test where 'xxxxxx_Ramakrishnan_zzzzz' LIKE '%' + name + '%';
Robin Day
One of my field "FormTypes" Contain values some thing like this 'Form1','Form2','Form3',etc. i get input string like this 'xxxhhuuForm1yyuusss'.Form1 is inside the input string ,i want identify this FormType from the input string and compare using like keyword,Is there any Options available in Sql for this purpose?
Ramakrishnan
please help me on this,thanks.
Ramakrishnan
@Ramakrishnan: this approach looks good to me. Give it a try :)
onedaywhen
@Ramakrishnan: Yes, that is what the query I wrote should do.'xxxForm1xxx' LIKE '%Form1%' will match and therefore you can do 'xxxForm1xxx' LIKE '%' + FormTypesField + '%'
Robin Day
thanks, This works for me.Great.
Ramakrishnan
A: 

You can use the MySQL functions, LOCATE() precisely like,

SELECT * FROM WHERE LOCATE("Ramakrishnan",input) > 0 
Shubham
The OP is on SQL Server.
Martin Smith
A: 

Are the xxxxxx and zzzzz bits always 6 and 5 characters? If so, then this is doable with a bit of string cutting.

with Test (id,name) as (
select 1, 'Ramakrishnan'
union
select 2, 'Coxy'
union
select 3, 'xxxxxx_Ramakrishnan_zzzzz'
)

Select * from Test where name like '%'+SUBSTRING('xxxxxx_Ramakrishnan_zzzzz', 8, CHARINDEX('_',SUBSTRING('xxxxxx_Ramakrishnan_zzzzz',8,100))-1)+'%'

Results in:

id  name
1   Ramakrishnan
3   xxxxxx_Ramakrishnan_zzzzz

If they are variable lengths, then it will be a horrible construction of SUBSTRING,CHARINDEX, REVERSE and LEN functions.

Coxy