【问题标题】:Write a query that pulls all fields from a table that has date in it编写一个查询,从包含日期的表中提取所有字段
【发布时间】:2011-12-09 07:09:53
【问题描述】:

我有大约 100 多个字段的表格。其中散布着许多日期字段(我怀疑大约有 8 个或更多)。我想要一个查询或 StoredProcedure,只选择数据类型为 Date 或 Datetime 的那些字段。

这不是 Project 的要求,它只是我的分析工具。

我使用的是 MSSQL 2005,但它不必只在 MSSQL 中。

【问题讨论】:

  • 所以所有的列都是 varchars 并且您想要扫描表中那些可能看起来像日期的列中的值?
  • 没有。如果一列被定义为日期时间字段(不是按实际名称),则只需要显示或选择那些列就可以了。

标签: sql sql-server-2005 datetime stored-procedures select


【解决方案1】:

可以查询元数据:

select *
from information_schema.columns
where table_name = 'yourtable'
and data_type in ('datetime', 'smalldatetime')

此外,此information_schema.columns 可在其他数据库系统中使用。

【讨论】:

    【解决方案2】:
    select column_name
    from information_schema.columns
    where table_name = 'yourTableName'
    and data_type like '%date%'
    

    【讨论】:

      猜你喜欢
      • 2011-02-21
      • 2013-08-29
      • 1970-01-01
      • 1970-01-01
      • 2010-09-26
      • 1970-01-01
      • 2014-06-27
      • 2021-02-11
      • 1970-01-01
      相关资源
      最近更新 更多