Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a column "Specialities" in this column i am adding spciality with cost of that
(e.g: Ayurvedic Consultant -- 100,
      Cancer--200,
      Cardiac Problem -- 500....etc)

if user want know about Cardiac Problem and its cost. how can i retrive only cardiac problem -- 500 in this sring.....i have used 'Like' command to match the speciality please help me out
Posted
Updated 15-Oct-12 20:56pm
v2
Comments
Nandakishore G N 16-Oct-12 2:15am    
are u not storing the values in other related table..i think this is not a better way...
if u want to do it, in this way get the cardiac word from the string..but one thing does the value remains same in all the rows...specify the in the question

U can use a regular expression like so:

bool contains = Regex.IsMatch("Hello1 Hello2", @"\bHello\b"); // yields false
bool contains = Regex.IsMatch("Hello1 Hello", @"\bHello\b"); // yields true


the \b is a word boundary check, and used like above it will be able to match whole words only.
 
Share this answer
 
Comments
chetan B Y 16-Oct-12 2:07am    
thank you, this regex method gives the result only true or false but i want that word as output or matching string as output.....
You can try following code

C#
string yourInput = "The value retrieved from DB";
string matchedString ;
Match match = Regex.Match(yourInput, "Cardiac Problem -- 500");
if (match.Success )
   matchedString = match.Value;



Hope that helps. If it doesn, mark it as answer/upvote.

Thanks,
Milind
 
Share this answer
 
fetch row from sql using like
then in C#,
C#
For (j=0; j< dt.Rows.Count; j++)
{
    string str = dt.rows(j)("Specialities").Tostring().Split(",");
    string SearchWord = "cardiac problem";
    string Result = string.Empty;
    For(i=0; i<str.Length; i++) // if you found more than one row from sql that have this word e.g. Cardiac problem ...
    {
          if(str[i].ToLower().Trim().startswith(SearchWord.ToLower().Trim())) 
          {
               Result = str[i];
               break;
          }
          label1.text += Result;
    } 
}
//output like below
//Cardiac Problem -- 500

Note: while comparing string be aware for case sensitivity & white spaces
Happy Coding!
:)
 
Share this answer
 
v2
I think your table data structure is ...


 id          specialities
 -------------------------------------------------------------------------
 1           Ayurvedic Consultant -- 100,Cancer--200,Cardiac Problem -- 500
 2           Something -- 120,Somthing --130,Something -- 140

---------------------------------------------------------------------------



You have construct XML object by using following query

SQL
WITH SPECCTE AS (
SELECT 
CAST('<i Speciality="'+ REPLACE( REPLACE(Specialities,'--','" Cost="'),',','"></i><i Speciality="') +'"></i>' AS XML) XMLSPECS 
FROM tblspecialities 
)

SELECT * FROM SPECCTE


If you execute above code block, follwing XML result will be returned

<i Speciality="Ayurvedic Consultant " Cost=" 100" /><i Speciality="Cancer" Cost="200" /><i Speciality="Cardiac Problem " Cost=" 500" />


To get the result like this..
 Speciality             Cost
-----------------------------------------
Ayurvedic Consultant    100
Cancer                  200
Cardiac Problem         500
-----------------------------------------


We need to split the XML result using XQuery like following method..

SQL
WITH SPECCTE AS (
SELECT 
CAST('<i Speciality="'+ REPLACE( REPLACE(Specialities,'--','" Cost="'),',','"></i><i Speciality="') 
+'"></i>' AS XML) XMLSPECS 
FROM tblspecialities 
)
 
SELECT LTRIM(RTRIM(x.i.value('@Speciality[1]','VARCHAR(100)'))) Speciality,
LTRIM(RTRIM(x.i.value('@Cost[1]','VARCHAR(10)'))) Cost FROM SPECCTE
CROSS APPLY XMLSPECS.nodes('//i') x(i)


If we need to get a record for specific speciality, then we need to run this code block.

SQL
 DECLARE @SrchSpeciality VARCHAR(50) = 'Cardiac Problem';
WITH SPECCTE AS (
SELECT 
CAST('<i Speciality="'+ REPLACE( REPLACE(Specialities,'--','" Cost="'),',','"></i><i Speciality="') 
+'"></i>' AS XML) XMLSPECS 
FROM tblspecialities WHERE Specialities LIKE '%'+@SrchSpeciality+'%'
)
 
SELECT LTRIM(RTRIM(x.i.value('@Speciality[1]','VARCHAR(100)'))) Speciality,
LTRIM(RTRIM(x.i.value('@Cost[1]','VARCHAR(10)'))) Cost FROM SPECCTE
CROSS APPLY XMLSPECS.nodes('//i') x(i)
WHERE x.i.value('@Speciality[1]','VARCHAR(100)') LIKE '%'+@SrchSpeciality+'%'


The result of above code block is

Speciality          Cost
-------------------------------
Cardiac Problem	     500
-------------------------------


Thank you ..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900