Função SQL Server CAST()
Exemplo
Converta um valor em um tipo de dados int:
SELECT CAST(25.65 AS int);
Definição e uso
A função CAST() converte um valor (de qualquer tipo) em um tipo de dados especificado.
Dica: Veja também a função CONVERT() .
Sintaxe
CAST(expression
AS datatype(length))
Valores de parâmetro
Value | Description |
---|---|
expression | Required. The value to convert |
datatype | Required. The datatype to convert expression to. Can be one of the following: bigint, int, smallint, tinyint, bit, decimal, numeric, money, smallmoney, float, real, datetime, smalldatetime, char, varchar, text, nchar, nvarchar, ntext, binary, varbinary, or image |
(length) | Optional. The length of the resulting data type (for char, varchar, nchar, nvarchar, binary and varbinary) |
Detalhes técnicos
Trabalha em: | SQL Server (a partir de 2008), Azure SQL Database, Azure SQL Data Warehouse, Parallel Data Warehouse |
---|
Mais exemplos
Exemplo
Converta um valor para um tipo de dados varchar:
SELECT CAST(25.65 AS varchar);
Exemplo
Converta um valor em um tipo de dados datetime:
SELECT CAST('2017-08-25' AS datetime);