Difference between revisions of "CAST"

From SQLZOO
Jump to: navigation, search
 
(One intermediate revision by one user not shown)
Line 27: Line 27:
 
</div>
 
</div>
  
<p>See also</p>
+
{{Languages}}
<ul>
+
  <li>[[CONVERT function]]</li>
+
</ul>
+

Latest revision as of 09:39, 1 October 2012

Compatibility
CAST(expr AS type)
EngineOKAlternative
ingresYes
mysqlYes
oracleYes
postgresYes
sqlserverYes

CAST

CAST allows you to convert from one type to another.

Often a CAST is implicit - for example if you concatenate a string with a number the number will be automatically changed to a string. However sometimes you need to make the CAST explicit.

 CAST(expr TO type) 

In this example we get the population in millions by casting the floating point value to DECIMAL(8,1) - this ensures one decimal place of accuracy. You can also CAST a date to a string to extract components using SUBSTRING or make up another date.

SELECT CAST(population/1000000 
       AS DECIMAL(8,1)) AS a
      ,population/1000000 AS b
  FROM bbc
Language: English  • Deutsch
Personal tools
Namespaces

Variants
Actions
Reference
Toolbox
Google AdSense