SQL 中的双冒号 (::) 表示法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5758499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:16:13  来源:igfitidea点击:

Double colon (::) notation in SQL

sqlpostgresqlcasting

提问by Pat

Have picked up someone's code and this is a part of a where clause, anyone know what the double colon indicates?

已经拿起某人的代码,这是 where 子句的一部分,有人知道双冒号表示什么吗?

b.date_completed >  a.dc::date + INTERVAL '1 DAY 7:20:00'

回答by Michael Fredrickson

It varies based on RDBMS, but if I guess right, that's PostgreSQL, in which case the ::converts a.dcto a date type of date.

它因 RDBMS 而异,但如果我猜对了,那就是 PostgreSQL,在这种情况下,它会::转换a.dcdate.

In other flavors...

在其他口味...

In MS SQL Server 2000:

在 MS SQL Server 2000 中:

For built-in user-defined functions that return a table, the function name must be specified with a leading double colon (::) to distinguish it from user-defined functions that are not built-in. It also must be specified as a one-part name with no database or owner qualifications. For example: SELECT * FROM ::fn_helpcollations() b.. For built-in user-defined functions that return a scalar value, the function name must be specified as a one-part name (do not specify database or owner). Do not specify a leading double colon (::).

对于返回表的内置用户定义函数,必须使用前导双冒号 (::) 指定函数名称,以将其与非内置的用户定义函数区分开来。它还必须指定为没有数据库或所有者资格的单部分名称。例如: SELECT * FROM ::fn_helpcollat​​ions() b.. 对于返回标量值的内置用户定义函数,函数名称必须指定为一个组成部分的名称(不要指定数据库或所有者)。不要指定前导双冒号 (::)。

In MS SQL Server 2005:

在 MS SQL Server 2005 中:

Double-colons are no longer required for UDFs that return a table.

返回表的 UDF 不再需要双冒号。

However...

然而...

Double-colons are required in SQL Server 2005 when granting permissions on schemas, certificates, endpoints, and a few other securables.

在 SQL Server 2005 中授予对架构、证书、端点和一些其他安全对象的权限时需要双冒号。

As well as...

也...

When using User-Defined Types, static methods of the type must be called using the double-colon syntax.

使用用户定义类型时,必须使用双冒号语法调用该类型的静态方法。

Sources: BOLand Kalen Delaney's Blog

资料来源:BOLKalen Delaney 的博客

回答by Michael Dean

In this case, it is a cast to a date type. :: is a type cast that can also be represented as CAST(expression AS type).

在这种情况下,它是转换为日期类型。:: 是一种类型转换,也可以表示为 CAST(表达式 AS 类型)。

回答by techkuz

It is a CASToperation(cast to a date type).

这是一个CAST操作(转换为日期类型)。

Example:

例子:

SELECT now()::timestamp(0);

SELECT now()::timestamp(0);

Is equivalent to:

相当于:

SELECT 
    CAST (now() AS timestamp(0));

They both result in casting now()to timestampin the following format: YYYY-MM-DD HH:MM:SS

它们都导致转换now()timestamp以下格式:YYYY-MM-DD HH:MM:SS

回答by Jonathan Leffler

It is probably a cast, converting a.dcto type date.

它可能是一个强制转换,转换a.dc为 type date

IBM Informix Dynamic Server (IDS) would work that way - but the INTERVAL notation at the end is not valid for IDS, so presumably this is in fact another DBMS (probably PostgreSQL).

IBM Informix Dynamic Server (IDS) 会以这种方式工作 - 但最后的 INTERVAL 符号对 IDS 无效,所以大概这实际上是另一个 DBMS(可能是PostgreSQL)。