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

您所在的位置:网站首页 sql转换数值型 SQL CAST()函数教程–在SQL中将值强制转换并将其转换为指定的数据类型

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

2023-12-10 20:03| 来源: 网络整理| 查看: 265

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



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3