3

How to find a string based on input characters in sql?

For example:

Table1 column name: desc

data warehousing
sql serverl
c#.Net
asp.net
  • My input: @FindString='dho' should return 'data warehousing'

  • My input: @FindString='sv' should return 'sql serverl'

  • My input: @FindString='aep' should return 'asp.net'

How to do that?

5
  • 1
    Mind explaining why the requirement is so weird? Commented Feb 10, 2014 at 7:20
  • 1
    aep --> asp.net ?!!?!?!? You gotta explain that! Commented Feb 10, 2014 at 7:23
  • You will need to use FTS with its "Inflectional forms search feature for this read here for more information on Full-text Search Commented Feb 10, 2014 at 7:24
  • @marc_s: yes, it may contain any order. String must have all characters. Commented Feb 10, 2014 at 7:29
  • @am1r_5h: going through stored procedure. Commented Feb 10, 2014 at 7:31

2 Answers 2

1

Go with this code:

SELECT * FROM table WHERE field like '%d%' AND field like '%h%' AND field like '%o%' 

I mean you should surround each character of input string with %.

Sign up to request clarification or add additional context in comments.

2 Comments

It may contain any order. I mean •My input: @FindString='aep' should return 'asp.net'
Use This : SELECT * FROM table WHERE field like '%d%' AND field like '%h%' AND field like '%o%' .
0

i think you want this :

declare @inputString nvarchar(max) = 'eap';

declare @where nvarchar(max) = 
    case @inputString 
        when 'dho' then 'data warehousing'
        when 'sv' then 'sql serverl'
        when 'eap' then 'asp.net'
    else ''
    end

select * from Table1
    where desc = @where

also your query can be like this:

declare @inputString nvarchar(max) = 'dho'

select * from 
   (select
        case [desc]
            when 'data warehousing' then 'dho'
            when 'sql serverl' then 'sv'
            when 'asp.net' then 'aep'
            else ''
        end 
        as [newDesc]
    from [table])t
where newDesc = @inputString

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.