title: Sybase convert float to string
body: <p>I have a requirement to convert fields in <code>float</code> to <code>string</code>, with following restrictions</p>
  
  <ol>
  <li>Need to truncate the results after 6 places after decimal point</li>
  <li>Truncate any trailing zeros after decimal</li>
  </ol>
  
  <p>For example:</p>
  
  <ol>
  <li>17.666655942234 ==> converted to string "17.666655"</li>
  <li>17.6  ==> converted to string "17.6" and not "17.600000"</li>
  <li>17.66  ==> converted to string "17.66"</li>
  </ol>
  
  <p>I have tried the following, but fail for some of the above requirements and also not able to remove the zero's. </p>
  
  <pre><code>select 
  CASE WHEN RIGHT(convert(VARCHAR, cast(cast(product_price as DECIMAL(28,6))  as float)),2) = ".0"
  THEN LEFT(convert(VARCHAR, product_price),(CHAR_LENGTH(convert(VARCHAR(15), product_price))-2))
  ELSE substring( convert(VARCHAR, cast(cast(product_price as DECIMAL(28,7))  as float)),1, charindex(".",convert(VARCHAR, cast(cast(product_price as DECIMAL(28,7))  as float)))+6) END 
  from #TEMP
  </code></pre>
  
  <h3>#TEMP data</h3>
  
  <pre><code>id | product_price
  -------------------
  1  | 17.3435302323
  2  | 17.700000000
  3  | 17.60
  4  | 19.0
  </code></pre>
  
  <p>I guess, I have complicated the query and a simpler version should exists.</p>
  
  <p>I know this could be easily handled in high level language, but if this could be easily be accommodated in the query itself then please let me know.</p>