The cast function can be used to convert the date datatype to the datetime datatype.

next → ← prev

The CAST() function in MySQL is used to convert a value from one data type to another data type specified in the expression. It is mostly used with WHERE, HAVING, and JOIN clauses. This function is similar to the CONVERT() function in MySQL.

The following are the datatypes to which this function works perfectly:

DatatypeDescriptions
DATE It converts the value into DATE datatype in the "YYYY-MM-DD" format. It supports the range of DATE in '1000-01-01' to '9999-12-31'.
DATETIME It converts the value into the DATETIME data type in the "YYYY-MM-DD HH:MM:SS" format. It support the range in '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.
TIME It converts the value into TIME data type in the "HH:MM:SS" format. It supports the range of time in '-838:59:59' to '838:59:59'.
CHAR It converts a value to the CHAR data type that contains the fixed-length string.
DECIMAL It converts a value to the DECIMAL data type that contains a decimal string.
SIGNED It converts a value to SIGNED datatype that contains the signed 64-bit integer.
UNSIGNED It converts a value to the UNSIGNED datatype that contains the unsigned 64-bit integer.
BINARY It converts a value to the BINARY data type that contains the binary string.

Syntax

The following are the syntax of CAST() function in MySQL:

Parameter Explanation

This syntax accepts two parameters, which are going to be discussed below:

ParameterRequirementDescriptions
Expression Required It is a value that will be converted into another specific datatype.
Datatype Required It is a value or data type in which the expression value needs to be converted.

Return Value

After conversion, it will return a value in which data type we want to convert.

MySQL version support

The CAST function can support the following MySQL versions:

  • MySQL 8.0
  • MySQL 5.7
  • MySQL 5.6
  • MySQL 5.5
  • MySQL 5.1
  • MySQL 5.0
  • MySQL 4.1
  • MySQL 4.0

Let us understand the MySQL CAST() function with the following examples. We can use the CAST function with the SELECT statement directly.

Example 1

This statement converts the value into DATE datatype.

Output

The cast function can be used to convert the date datatype to the datetime datatype.

Example 2

This statement converts the value into SIGNED datatype.

Output

The cast function can be used to convert the date datatype to the datetime datatype.

Example 3

This statement converts the value into UNSIGNED datatype.

Output

The cast function can be used to convert the date datatype to the datetime datatype.

Example 4

Sometimes there is a need to convert the string into an integer explicitly, use the following statement for converting the value into INTEGER datatype.

Output

The cast function can be used to convert the date datatype to the datetime datatype.

Example 5

The following statement first converts an integer value into string datatype and then perform concatenation with another specified string.

Output

The cast function can be used to convert the date datatype to the datetime datatype.

Example 6

In this example, we are going to see how the CAST function works with the table. Let us first create a table "Orders" that contains the following data:

The cast function can be used to convert the date datatype to the datetime datatype.

In the above table, we can see that the Order_Date is in DATE datatype. Now, if we want to get a product name between selected ranges of time, execute the statement below. Here, the literal string converted into timestamp value before evaluating the WHERE condition.

This statement will produce the following output:

The cast function can be used to convert the date datatype to the datetime datatype.

Next TopicMySQL convert

← prev next →

Sql Server Cast Date Dd/Mm/Yyyy With Code Examples

The solution to Sql Server Cast Date Dd/Mm/Yyyy will be demonstrated using examples in this article.

  --string to date
  SELECT cast(convert(date, '23/07/2009', 103) AS DATE)
  select convert(date, '23/07/2009', 103)
 

Using a variety of different examples, we have learned how to solve the Sql Server Cast Date Dd/Mm/Yyyy.

How can I get dd mm yyyy date in SQL Server?

SQL Date Format with the FORMAT function

  • Use the FORMAT function to format the date and time data types from a date column (date, datetime, datetime2, smalldatetime, datetimeoffset, etc.
  • To get DD/MM/YYYY use SELECT FORMAT (getdate(), 'dd/MM/yyyy ') as date.

How do I CAST a date format in SQL Server?

How to get different date formats in SQL Server

  • Use the SELECT statement with CONVERT function and date format option for the date values needed.
  • To get YYYY-MM-DD use this T-SQL syntax SELECT CONVERT(varchar, getdate(), 23)
  • To get MM/DD/YY use this T-SQL syntax SELECT CONVERT(varchar, getdate(), 1)

How do I CAST a timestamp to a date in SQL Server?

How to Convert Timestamp to Date in SQL with Syntax

  • CONVERT. CONVERT() function in SQL is used to convert any value of any data type into the required data types (as mentioned by the user in the query).
  • CAST.

How do I format a date in SQL?

SQL Server comes with the following data types for storing a date or a date/time value in the database:

  • DATE – format YYYY-MM-DD.
  • DATETIME – format: YYYY-MM-DD HH:MI:SS.
  • SMALLDATETIME – format: YYYY-MM-DD HH:MI:SS.
  • TIMESTAMP – format: a unique number.

How do I change the default datetime format in SQL Server?

The default date format of SQL is mdy(U.S English). Now to change sql server default date format from “mdy”(mm/dd/yyyy) to “dmy”(dd/mm/yyyy),we have to use SET DATEFORMAT command. Before changing the default date time format of sql server lets go through the way to know what format of date time is currently in use.12-Sept-2015

How do you convert date format from Yyyymmdd to yyyy mm dd in SQL?

Convert Char 'yyyymmdd' back to Date data types in SQL Server. Now, convert the Character format 'yyyymmdd' to a Date and DateTime data type using CAST and CONVERT. –A. Cast and Convert datatype DATE: SELECT [CharDate], CAST([CharDate] AS DATE) as 'Date-CAST', CONVERT(DATE,[CharDate]) as 'Date-CONVERT' FROM [dbo].22-Jul-2021

Which is better CAST or convert in SQL?

CAST is also less powerful and less flexible than CONVERT. On the other hand, CONVERT allows more flexibility and is the preferred function to use for data, time values, traditional numbers, and money signifiers. CONVERT is also useful in formatting the data's format.

What is CAST () and convert () functions in SQL Server?

The cast and convert functions provide similar functionality. They are used to convert a value from one data type to another. So let's take a look at a practical example. The example is developed in SQL Server 2012 using the SQL Server Management Studio.09-Apr-2019

What is CAST function in SQL?

The CAST() function converts a value (of any type) into a specified datatype.

How do I cast a timestamp to a date?

The constructor of the Date class receives a long value as an argument. Since the constructor of the Date class requires a long value, we need to convert the Timestamp object into a long value using the getTime() method of the TimeStamp class(present in SQL package).17-Jan-2022

Can the CAST function be used to convert the DATE datatype to the datetime datatype?

You can use the built-in CAST() function in SELECT statements to translate between date and time data types and character-based data types, for example: DATE , TIME or TIMESTAMP data type into a CHAR data type.

Can you CAST DATE to datetime?

We can convert the Date into Datetime in two ways. Using CONVERT() function: Convert means to change the form or value of something. The CONVERT() function in the SQL server is used to convert a value of one type to another type. Convert() function is used to convert a value of any type to another datatype.

What is CAST () in SQL?

The SQL CAST function converts the data type of an expression to the specified data type. For a list of the data types supported by InterSystems SQL, see Data Types. CAST is similar to CONVERT, with these differences: CONVERT is more flexible than CAST.

What is CAST () and convert () functions in SQL Server?

The T-SQL language offers two functions to convert data from one data type to a target data type: CAST and CONVERT. In many ways, they both do the exact same thing in a SELECT statement or stored procedure, but the SQL Server CONVERT function has an extra parameter to express style.