【问题标题】:SQL - update now() data with only dateSQL - 仅使用日期更新 now() 数据
【发布时间】:2016-02-09 12:14:52
【问题描述】:

我有一个 SQL 数据库,其字段由 now() 函数填充。

我只想更新当前数据的日期部分。

例子:

当前数据:

 20.08.2015 13:10:31
 21.08.2015 14:00:29
 22.08.2015 05:55:42

目标

20.08.2015 
21.08.2015
22.08.2015

谢谢,

【问题讨论】:

  • 您使用哪种 RDBMS?格式化语法和功能可能因平台而异
  • 你的列是什么数据类型
  • 可以在不改变表结构的情况下查询出已有数据。
  • 数据类型为日期时间
  • 从那里选择你可以使用 trunc(date)

标签: sql sql-server date datetime


【解决方案1】:

假设你使用的是MySQL数据库,可以使用date函数

update yourtable
set yourColumnName = date(yourColumnName)

在 SQL Server 中尝试使用Convert function

update yourtable
set yourColumnName =CONVERT(date, yourColumnName)

update yourtable
set yourColumnName CONVERT(varchar(10),yourColumnName,104)

否则你可以使用LEFT函数

update yourtable
set yourColumnName =LEFT (yourColumnName, 10)

SQL FIDDLE

【讨论】:

  • @UmutK:- 更新了我的答案!
【解决方案2】:

假设数据在表格中,您可以执行以下操作:

update t
    set col = cast(col as date);

转换为日期的确切语法因数据库而异。也可能是:

  • 日期(列)
  • date_trunc('day', col)
  • 截断(col)

【讨论】:

  • 删除了最后一个date(col),因为它不明确
猜你喜欢
  • 2012-06-19
  • 1970-01-01
  • 1970-01-01
  • 2017-04-22
  • 2016-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多