Return Field When a Field Contains Text from Another Field

Hi,

I'm new to SQL and I'm trying to write a statement to satisfy the following:

If [Field1] contains text from [Field2] then return [Field3] as [Field4].

I had two tables where there were no matching keys. I did a cross apply and am now trying to parse out the description to build the key. 

Any help would be wonderful! Thank you.

August 25th, 2015 8:11pm


If [Field1] contains text from [Field2] then return [Field3] as [Field4]

Can you be more specific on how to predicate [Field1] contains text from [Field2]? For example, we can say the [Field1]="Hello World" contains [Field2]="World". But what about "Hello World" and "beautiful world", can we say this example still follows the predication for they both contain the text "world"?

The fore example can be simple, you can achieve your goal with a CASE WHEN expression.

SELECT CASE WHEN [FILED1] LIKE '%'+[FIELD2]+'' THEN FIELD3 ELSE....END

The latter example makes things in a complicated case. You would have to CROSS APPLY a table-valued function that splits the field2's every words into table and use a CHARINDEX to predicate.

Could you post your table DDL and some sample data so we can look into your question further?

If you have any feedback on our support, you can click here.
Free Windows Admin Tool Kit Click here and download it now
August 27th, 2015 2:54am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics