Author Message jason123 SSC Veteran Group: General Forum Members Points: 287 Visits: 67 Hi Everyone,Please see xml format USD 20120904 20120904 20121004 20121102 20120904 20120904 20121004 20121102 -250 150 95.18 -145.18 -250 -100 -4.82 -150 -12.47 0 0 20120904 20140904 -132 -250 -250 -250 -4.95 -5.5 -2.02 26.4 26.4 26.4 20120930 20121031 20121111 -4.9499999999998 -5.4999999999996 -2.0166666666665 0 20120904 0 20120904 4058.5You can see nodes c58 and c62, they have attribute m=2,m=3, m=4 and so on dependent on data input. I want to take the last two value of each node, for exampleif m=3,I need to read value in this formatID Currency C58 C62LD122487292000 USD -5.5 20121031LD122487292000 USD -2.02 20121111We could say max of m=3 and max of m-1=2Thanks and Best regards; PiMané SSCertifiable Group: General Forum Members Points: 7165 Visits: 1364 to get the data from m=2 just do`Select t.c.value('./@id', 'nvarchar(100)'), t.c.value('(c1/text())[1]', 'NVARCHAR(10)'), t.c.value('(c58[@m="2"]/text())[1]', 'nvarchar(10)'), t.c.value('(c62[@m="2"]/text())[1]', 'nvarchar(10)') FROM @x.nodes('/row') t(c)`instead of having "fixed" m you can declare a variable and use it. If you need to work better, try working less... jason123 SSC Veteran Group: General Forum Members Points: 287 Visits: 67 Thank for your help, I could read it in fixed m , but i am researching how could I know max of m value because I need to take values from the last two nodes(c57 & c62)?m is changed from record to record, sometimes m=3, value of m I need to read is m=3 for first record and m=2 for second record. When m=2, it should be m=2 for first record and m=1 for second recordThank PiMané SSCertifiable Group: General Forum Members Points: 7165 Visits: 1364 try this (I think this is what you want..)`declare @x xml = 'USD2012090420120904201210042012110220120904201209042012100420121102-25015095.18-145.18-250-100-4.82-150-12.47002012090420140904-132-250-250-250-4.95-5.5-2.0226.426.426.4201209302012103120121111-4.9499999999998-5.4999999999996-2.01666666666650201209040201209044058.5'declare @m INTselect @m = MAX(ISNULl(t.c.value('@m', 'int'), 0)) from @x.nodes('/row/c62') t(c)Select t.c.value('./@id', 'nvarchar(100)'), t.c.value('(c1/text())[1]', 'NVARCHAR(10)'), t.c.value('(c58[@m=sql:variable("@m")]/text())[1]', 'nvarchar(10)'), t.c.value('(c62[@m=sql:variable("@m")]/text())[1]', 'nvarchar(10)') FROM @x.nodes('/row') t(c)` If you need to work better, try working less... jason123 SSC Veteran Group: General Forum Members Points: 287 Visits: 67 Hi Friend, thank you so much it is very helpful I can solve my current concern. PiMané SSCertifiable Group: General Forum Members Points: 7165 Visits: 1364 jason123 (12/21/2012)Hi Friend, thank you so much it is very helpful I can solve my current concern.glad to help..Pedro If you need to work better, try working less...