title: SQL Server: How to get the value of a XML element specifying an attribute?
body: <p>For the usage in a SQL query I need to get the value of a specific XML element. The XML element is specified by its attribute.</p>
  
  <p>My XML elements looks like this:</p>
  
  <pre><code>&lt;translations&gt;
    &lt;value lang="en-US"&gt;example&lt;/value&gt;
    &lt;value lang="de-DE"&gt;Beispiel&lt;/value&gt;
  &lt;/translations&gt;
  </code></pre>
  
  <p>and the value I am looking for would be "example" when I specify lang to be "en-US".</p>
  
  <p>I found a way to get this value by using the query() function and afterwards the value() function. </p>
  
  <pre><code>declare @S varchar(max)
  
  set @S =
  '&lt;translations&gt;
    &lt;value lang="en-US"&gt;example&lt;/value&gt;
    &lt;value lang="de-DE"&gt;Beispiel&lt;/value&gt;
  &lt;/translations&gt;'
  
  declare @X xml
  
  set @X = CAST(@S as xml)
  
  select @X.query('/translations/value[@lang="en-US"]').value('.','varchar(max)')
  </code></pre>
  
  <p>This select statement return the value "example" I am looking for by using the query() and value() function. But is there also a - more convenient - way to only use value() OR query()?</p>
  
  <p>Thank you in advance!</p>