如何在不丢失日期的情况下将varchar列类型转换为日期类型 [英] How to convert a varchar column type to date type without losing the dates

查看:57
本文介绍了如何在不丢失日期的情况下将varchar列类型转换为日期类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种更改列的数据类型的方法.当前,在我的数据库中,日期列类型定义为varchar,我需要将它们转换回日期类型.

I am looking for a way to change the datatype of a column. Currently, in my database, the date columns types were defined as varchar and I need to convert them back to the date type.

有什么想法吗?

推荐答案

您将需要根据自己的确切表结构来调整它,但类似的东西;

You will need to adapt this based your your exact table structure but something like;

CREATE TABLE temp (startdate varchar(255), stuff varchar(255));

INSERT INTO temp
SELECT startdate,stuff
FROM mytable;

TRUNCATE TABLE mytable;

ALTER TABLE mytable ALTER COLUMN startdate DATETIME NOT NULL;

INSERT INTO mytable
SELECT CAST(startdate AS DATETIME), stuff FROM temp;

DROP TABLE temp;

这篇关于如何在不丢失日期的情况下将varchar列类型转换为日期类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆