【问题标题】:Is there a way to see the type of a SQL expression? [duplicate]有没有办法查看 SQL 表达式的类型? [复制]
【发布时间】:2020-04-06 02:44:33
【问题描述】:

假设我选择了一个 SQL 表达式:

my_db=> select cast('2019-12-12' as date) + cast('10:00' as time);
      ?column?       
---------------------
 2019-12-12 10:00:00
(1 row)

有没有办法让我查看该列的类型?例如。是timestamp with time zone 还是timestamp without time zone;

理想情况下是这样的:

my_db=> select type(cast('2019-12-12' as date) + cast('10:00' as time));
          ?column?       
-----------------------------
 timestamp without time zone
(1 row)

可能我的示例仅适用于 postgresql,但我想知道是否有标准的 SQL 方法,如果没有,是否有 postgresql 方法。

【问题讨论】:

  • standard SQL way of doing it 我怀疑你会找到描述结果集的通用方法。一种解决方法可能是基于查询创建视图/表并读取 INFROMATION_SCHEMA.COLUMNS ,但它是丑陋的解决方案。 SQL Server 有dm_exec_describe_first_result_set
  • 太棒了,谢谢@LukaszSzozda
  • 为什么不简单地使用Desc table。

标签: sql postgresql


【解决方案1】:

在 Postgresql 中使用 pg_typeof(<expression>)

所以对于你的例子:

my_db=> select pg_typeof(cast('2019-12-12' as date) + cast('10:00' as time));
          pg_typeof          
-----------------------------
 timestamp without time zone
(1 row)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 2020-08-28
    • 2023-03-30
    相关资源
    最近更新 更多