Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Cast Function in SQL

Cast Function in SQL

? CAST() Function in SQL

The CAST() function converts a value from one data type to another.


? Syntax

CAST(expression AS target_data_type)
  • expression: The value or column you want to convert.

  • target_data_type: The data type you want to convert to (e.g., INT, VARCHAR, DATE, etc.).


? Examples

1. Convert string to integer

SELECT CAST('123' AS INT);-- Returns 123 as integer

2. Convert integer to string

SELECT CAST(456 AS VARCHAR(10));-- Returns '456' as a string

3. Convert string to date

SELECT CAST('2023-06-06' AS DATE);-- Returns a DATE value

4. Using CAST in a query

SELECT employee_name, CAST(salary AS VARCHAR(20)) AS salary_textFROM employees;

? Key Points

  • Standard SQL function supported by most databases (MySQL, PostgreSQL, SQL Server, Oracle).

  • Useful for data type conversions in expressions and queries.

  • Helps avoid errors when performing operations on incompatible data types.


Want to know about CONVERT() function (similar to CAST()) or examples with date/time casting?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql