Collections:
CAST() - Casting Expression to Value
How to cast an expression to a value of the given data type using the CAST() function?
✍: FYIcenter.com
CAST(exp AS type) is a MySQL built-in function that casts an expression to a value of the given data type. For example:
SET @exp = '-1.99 USD'; SELECT @exp, CAST(@exp AS SIGNED INTEGER), CAST(@exp AS UNSIGNED INTEGER); -- +-----------+------------------------------+--------------------------------+ -- | @exp | CAST(@exp AS SIGNED INTEGER) | CAST(@exp AS UNSIGNED INTEGER) | -- +-----------+------------------------------+--------------------------------+ -- | -1.99 USD | -1 | 18446744073709551615 | -- +-----------+------------------------------+--------------------------------+ SELECT HEX(18446744073709551615); -- +---------------------------+ -- | HEX(18446744073709551615) | -- +---------------------------+ -- | FFFFFFFFFFFFFFFF | -- +---------------------------+ SELECT @exp, CAST(@exp AS DECIMAL(8,4)), CAST(@exp AS FLOAT(4)); -- +-----------+----------------------------+------------------------+ -- | @exp | CAST(@exp AS DECIMAL(8,4)) | CAST(@exp AS FLOAT(4)) | -- +-----------+----------------------------+------------------------+ -- | -1.99 USD | -1.9900 | -1.99 | -- +-----------+----------------------------+------------------------+ SELECT NOW(), CAST(NOW() AS YEAR), CAST(NOW() AS TIME(6)); -- +---------------------+---------------------+------------------------+ -- | NOW() | CAST(NOW() AS YEAR) | CAST(NOW() AS TIME(6)) | -- +---------------------+---------------------+------------------------+ -- | 2023-12-16 00:44:49 | 2023 | 00:44:49.000000 | -- +---------------------+---------------------+------------------------+
Reference information of the CAST() function:
CAST(exp AS type): val Casts an expression to a value of the given data type. Arguments, return value and availability: exp: Required. The expression to be converted. type: Required. The data type to be converted to. val: Return value. The converted value of the given type. Available since MySQL 4.0. Data types that are supported: Type Return Value ---- ----------- BINARY(N) VARBINARY(N) CHAR(N) VARCHAR(N) DATE DATE DATETIME(M) DATETIME(M) DECIMAL(M,D) DECIMAL(M,D) DOUBLE DOUBLE FLOAT(p) FLOAT(p) JSON JSON NCHAR(N) NVARCHAR(N) REAL REAL SIGNED INTEGER BIGINT TIME(M) TIME(M) UNSIGNED INTEGER BIGINT YEAR YEAR
Related MySQL functions:
⇒ COALESCE() - Finding First Non-NULL Value
⇐ MySQL Functions for Miscellaneous Purposes
2023-12-17, 209🔥, 0💬
Popular Posts:
How to connect SQL Server Management Studio Express to SQL Server 2005 Express in SQL Server? Once y...
How To Get Year, Month and Day Out of DATETIME Values in SQL Server Transact-SQL? You can use DATEPA...
How To Set Up SQL*Plus Output Format in Oracle? If you want to practice SQL statements with SQL*Plus...
How To Convert Numeric Expression Data Types using the CAST() Function in SQL Server Transact-SQL? I...
How To List All User Names in a Database in SQL Server? If you want to see a list of all user names ...