0

I have to do certain actions based on the decision if a sub string exists in a column.

For example my column 'LangCodes' have # separated values like en-us#ar-ae#in-id.

I can use the SQL in operator if I can convert the value in form like : 'en-us','ar-ae','in-id'.

For example select Col1 from Table1 where 'en-us' in (LangCodes)

Do I need to use replace function of SQL to accomplish this or any better way exists?

1
  • 1
    How about SELECT * FROM TABLE_NAME WHERE LANGCODES LIKE '%en-us%' Commented Dec 27, 2013 at 14:01

2 Answers 2

2

You cannot do this efficiently in SQL Server, because you are storing your data in a fashion not consistent with the use of relational databases. You need a separate correlation table that has columns id and LangCode, with one row per language code.

You can do what you want with string operations. Here is a typical way:

where '#'+LangCodes+'#' like '%#en-us#%'

This, however, cannot take advantage of an index on LangCodes.

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

1 Comment

Thanks for the hint. It helped me to eliminate 3 OR conditions. i.e. : select Col1 from Table1 where LangCodes = 'en-us' OR LangCodes like '%#en-us#%' OR LangCodes like '%en-us#%' OR LangCodes like '%#en-us%'
0

The most efficient and best way to check your languages codes is to seperate them in your table.

Never, never, never store multiple values in one column!

This is how your tables could look like (just examples)

product table
-------------
id
name

language_code table
-------------------
id 
name

product_language_code table
---------------------------
product_id
language_code_id

2 Comments

I understand that we should not store multiple values in one column, but what if these values are coming from a Legacy Database & we dont have any control on how to store these values?
Then use Gordons answer

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.