如何更改表中现有记录的sql日期格式?

问题描述 投票:0回答:2

我有一个日期时间列,它将记录存储为

2020 年 3 月 10 日 12:00

但想将所有格式更改为 03/10/20

有上千条记录

我试过了,

COnvert(date,convert(varchar, PostingDate, 101),101)

sql sql-server
2个回答
0
投票

我假设该列实际上是一个字符串,而不是

datetime

SQL Server 迄今为止具有相当灵活的转换。首先检查以确保所有日期都已转换:

select col
from t
where try_convert(date, col) is null and col is not null;

如果您对转换感到满意,您可以执行以下操作:

update t
    set col = try_convert(date, col);

alter table t alter col date;

0
投票

要更改表中列的数据类型,请使用以下语法:

SQL Server / MS Access:

ALTER TABLE table_name
ALTER COLUMN column_name datatype;


ALTER TABLE Employee
ALTER COLUMN Dates Date;
© www.soinside.com 2019 - 2024. All rights reserved.