SQL CAST()函数教程–在SQL中将值强制转换并将其转换为指定的数据类型

本文详细介绍了SQL中的CAST()函数,用于将值转换为不同的数据类型,包括字符串到DateTime、Date、Time的转换,数字到字符、数字的转换以及浮点数的转换。通过示例展示了如何使用CAST()函数在SQL Server中进行数据类型转换。
摘要由CSDN通过智能技术生成

MS SQL Server provides some functions in order to make database administrators’ life easier. A typical database store different types of values. In some cases, we may need to cast and convert these values into different data types. MS SQL provides CAST() function can be used to change data types.

MS SQL Server提供了一些功能,以使数据库管理员的工作更加轻松。 典型的数据库存储不同类型的值。 在某些情况下,我们可能需要转换这些值并将其转换为不同的数据类型。 MS SQL提供的CAST()函数可用于更改数据类型。

CAST()函数语法 (CAST() Function Syntax)

CAST() function has a simple syntax where we will provide the value and the type we want to cast to.

CAST()函数具有一种简单的语法,其中我们将提供要转换为的值和类型。

CAST(expression AS datatype(length))
  • expression is the value, variable, or expression we want to convert.

    expression是我们要转换的值,变量或表达式。

  • datatype is the data type we want to convert to.

    datatype是我们要转换为的数据类型。

  • length is optional where varchar or similar data types require length for the variable storage.

    在varchar或类似数据类型需要变量存储长度的情况下, length是可选的。

将字串转换为DateTime (Cast String To DateTime)

Date and times can be expressed in string format but MS SQL also provides DateTime date type which provides special functions about date and time. In this example, we will provide some data in string format and convert it into MS SQL DateTime format.

日期和时间可以用字符串格式表示,但是MS SQL还提供了DateTime日期类型,该类型提供了有关日期和时间的特殊功能。 在此示例中,我们将以字符串格式提供一些数据并将其转换为MS SQL DateTime格式。

SELECT CAST('2017-08-25 12:05:34.789' AS datetime);

We can see from the screenshot that the date which consists of year, month, day and time which also consist of the hour, minute, second and millisecond converted into datetime type.

从屏幕截图中我们可以看到,由年,月,日和时间组成的日期也由小时,分钟,秒和毫秒组成,这些datetime转换为datetime类型。

将字符串强制转换为日期 (Cast String To Date)

In the previous example, we have converted a string into DateTime format which is a compound type for date and time. We can also convert or cast just the string into a date.

在前面的示例中,我们将字符串转换为DateTime格式,这是日期和时间的复合类型。 我们还可以将字符串仅转换或转换为日期。

SELECT CAST('2017-08-25' AS date);

将字符串转换为时间 (Cast String To Time)

We can also just cast or convert a string into a time which contains hour, minute, second, a millisecond.

我们还可以将字符串强制转换或转换为包含小时,分钟,秒,毫秒的时间。

SELECT CAST('12:13:14.567' AS time);

将数字强制转换为字符 (Cast Number To Character)

We can cast a number into a character or character array/string. We will use char data type in this example but other char types like nchar, varchar can be also used.

我们可以将数字转换为字符或字符数组/字符串。 在此示例中,我们将使用char数据类型,但也可以使用其他char类型,例如ncharvarchar

SELECT CAST( 67 AS char);

将字串转换为数字 (Cast String To Number)

We can also provide numbers in string format and convert them into number data type easily.

我们还可以提供字符串格式的数字,并将其轻松转换为number数据类型。

SELECT CAST( '123' AS number);

将字符串转换为浮点数 (Cast String To Floating Point)

number data type only stores integers and floating points can not be stored. We can cast a string into a floating-point number with the float data type.

number数据类型仅存储整数,不能存储浮点数。 我们可以将字符串转换为具有float数据类型的float

SELECT CAST( '123.456' AS float);
LEARN MORE  MySQL Data Types Tutorial with Examples
了解更多MySQL数据类型教程和示例

翻译自: https://www.poftut.com/sql-cast-function-tutorial-cast-and-convert-a-value-to-a-specified-data-type-in-sql/

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值