Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi..

In our database table one column is stored as an xml string.which is shown below.
XML
<DocumentElement>
  <PartInfo>
    <ID>0</ID>
    <Name />
    <PartNo>0</PartNo>
    <SerialNo>1</SerialNo>
    <Parameter>0</Parameter>
    <InstalledDate>2013-01-15T00:00:00+05:30</InstalledDate>
    <InstalledTill>2013-01-25T00:00:00+05:30</InstalledTill>
  </PartInfo>
  </DocumentElement>


i want to get column values of this string.For example i have to get the value 2013-01-15T00:00:00+05 of installed Date column.Pls tell me how can i obtain this using forxml clause..Help me please i want it urgently..Thats why..Thanks in advance..

swathi.
Posted
Updated 5-Feb-13 22:34pm
v2

1 solution

Suppose you table is MyXMLData and the column name is XmlDataColumn
SQL
SELECT [XmlDataColumn].query('.//InstalledDate').value('.','VARCHAR(MAX)')
FROM [MyXMLData]


With regarding your comments, please check following query
SQL
SELECT [XmlDataColumn].value('(//ID)[1]', 'VARCHAR(MAX)') AS ID, [XmlDataColumn].value('(//PartNo)[1]', 'VARCHAR(MAX)') AS PartNo
FROM [MyXMLData]
WHERE [XmlDataColumn].value('(//parent::node()/InstalledDate)[1]', 'VARCHAR(MAX)') ='2013-01-15T00:00:00+05:30'
 
Share this answer
 
v2
Comments
A C swathi 6-Feb-13 6:05am    
Thank u..I tried this.But I want to search whole rows based on the value in column and have to bind the rows satisfying this conditon(whose installed date is same as what we type for search in UI).But i cant do it with this code.Because i cant select directly the column because which is now present as an xml string..plz tell me a solution for that..
Tharaka MTR 7-Feb-13 0:59am    
I have update my answer, please check whether it is OK.
A C swathi 7-Feb-13 1:36am    
Thank u..bt the pblm is still existing.i am getting my table as xmlstring in pgm so cant specify the columns inside that in a select query..i just want the values inside the columns ,i cant specify the value directly in query.Because the value will be changed in xml string depending on the user.i have to fetch the datas based on what he is passing..There may be thousands of user so how can i do this..Is there any solution for that..Thanks in advance..
Tharaka MTR 7-Feb-13 4:06am    
What do you mean can't specify the columns inside that?
you said you got the following XML

<documentelement>
<PartInfo>
<id>0
<name>
<PartNo>0</PartNo>
<serialno>1
<Parameter>0</Parameter>
<installeddate>2013-01-15T00:00:00+05:30
<installedtill>2013-01-25T00:00:00+05:30
</PartInfo>


what do you mean I can't specify the value directly in query?
is that you mean "WHERE [XmlDataColumn].value('(//parent::node()/InstalledDate)[1]', 'VARCHAR(MAX)') ='2013-01-15T00:00:00+05:30'" section?
A C swathi 7-Feb-13 4:27am    
yes.i meant that section.i have to use this query in a pgm.in that cant write the column xmldatacolumn directly.coz which is now present in xmlstring..at that time what i can do..Plz help me.

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