2

I have an issue that I am stuck in,

I have a table called wpg_original_word with field tags, which contains comma separated values and what I want to achieve is search for tags which are starting with the letter 'd'

I have written the query

SELECT DISTINCT tags FROM wpg_original_word  WHERE tags LIKE 'd%' ;

It returns the whole string like 'Diabetes, Nutrition, Dietician' , but I want only the Diabetes, Dietician.

Any help?

4
  • I'm having a similar issue in SQL Server. You need to split the string, maybe with a stored procedure or a function. Commented Feb 21, 2014 at 8:21
  • ok, I am not familiar with that, anyway I will try Commented Feb 21, 2014 at 8:23
  • Maybe this post could help you stackoverflow.com/questions/11835155/… Commented Feb 21, 2014 at 8:24
  • thanks for your effort, I will look into it Commented Feb 21, 2014 at 8:26

2 Answers 2

3

You could split the string with PHP, use the following code

<?php

$string = 'Diabetes, Nutrition, Dietician';
$array = explode(',', $string);
$result = array();
foreach ($array as $part) {
    if (substr(strtolower(trim($part)),0,1) == 'd') {
        array_push($result, trim($part));
    }
}

var_dump($result);

See it in action here. http://codepad.org/SVk7FPx9

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

1 Comment

Thanks for the idea, I have to change a little bit according to my want
0

You can use in_array() to check for a string in your query and then print it.

1 Comment

I want to check values starting with a specific letter

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.