[英]Convert column from varchar to datetime and update it in the table
I have a question related to the conversion of varchar to datetime. 我有一个与varchar转换为datetime有关的问题。 This topic was covered already in the thread SQL Server Convert Varchar to Datetime but I would like to advance it bit further.
SQL Server将Varchar转换为Datetime的线程中已经介绍了该主题,但是我想进一步介绍它。
I have performed BULK INSERT
into predefined tables where VARCHAR(255)
is the destination. 我已经对以
VARCHAR(255)
为目的地的预定义表进行了BULK INSERT
。 I have a table dbo.USR_02_ALL_RAW
and the field GLTGB
which holds strings in the following format: 07/16/2016
. 我有一个表
dbo.USR_02_ALL_RAW
和字段GLTGB
,其中包含以下格式的字符串: 07/16/2016
。
I can convert it as a single string by the following code: 我可以通过以下代码将其转换为单个字符串:
DECLARE @Date varchar(255)
set @Date= '07/16/2016'
SELECT CONVERT(datetime,RIGHT(@Date,4)+LEFT(@Date,2)+SUBSTRING(@Date,4,2))
and it gives me a result: 它给了我一个结果:
2016-07-16 00:00:00.000
2016-07-16 00:00:00.000
However I would like to pass to the code the whole field GLTGB
from the table dbo.USR_02_ALL_RAW
, convert it from VARCHAR
into DATETIME
and update the field GLTGB
with these results.(converting the whole field from varchar
to datetime
) 但是我想将表
dbo.USR_02_ALL_RAW
的整个字段GLTGB
传递给代码,将其从VARCHAR
转换为DATETIME
并使用这些结果更新字段GLTGB
。(将整个字段从varchar
转换为datetime
)
Thank you! 谢谢!
If you convert a value to datetime, then update the same database column it came from with the value then, since that column is still varchar
, SQL will have to convert the value back to varchar again in order to store it. 如果将值转换为日期时间,则用该值更新来自它的同一数据库列,然后,由于该列仍为
varchar
,SQL将不得不再次将该值转换回varchar以便存储它。 So you can't achieve anything useful with that kind of simple approach. 因此,使用这种简单方法无法获得任何有用的东西。
f you want to actually change the data type of the column, and also convert all the values, then I think you need to go through the following process: 如果您想实际更改列的数据类型,并转换所有值,那么我认为您需要经历以下过程:
1) Create a new varchar
column in your table (which will be temporary) 1)在表格中创建一个新的
varchar
列(这将是临时的)
2) copy all the data values from the GLTGB
column into the new column (using an UPDATE statement) 2)将所有数据值从
GLTGB
列复制到新列中(使用UPDATE语句)
3) Drop the GLTGB
column 3)删除
GLTGB
列
4) Re-create it with the same name but with datetime
type 4)使用相同名称但使用
datetime
类型重新创建它
5) Use an UPDATE statement to re-populate the new GLTGB
column from your temporary column 5)使用UPDATE语句从临时列中重新填充新的
GLTGB
列
6) Finally, drop the temporary column 6)最后,删除临时列
There may be a simpler way but that seems like the obvious process. 可能有一种更简单的方法,但这似乎是显而易见的过程。
First clear this, you want to Bulk insert
or Bulk update
. 首先清除此内容,您要
Bulk insert
或Bulk update
。 Since you already have a column GLTGB
. 由于您已经具有
GLTGB
列。 If you want to update the value only. 如果只想更新值。
update tab set GLTGB =
CONVERT(datetime,RIGHT(GLTGB,4)+LEFT(GLTGB,2)+SUBSTRING(GLTGB,4,2))
Or 要么
If you want to update the field from varchar
to datetime
. 如果要将字段从
varchar
更新为datetime
。 Then process is little bit lengthy. 然后过程有点冗长。
Alter table tab add newcol datetime --- Add new datetime type column
update tab set newcol =
CONVERT(datetime,RIGHT(GLTGB,4)+LEFT(GLTGB,2)+SUBSTRING(GLTGB,4,2)) --- update value in new column
Alter table tab drop column GLTGB --- drop GLGTB column
Alter table tab add GLGTB datetime --- add GLGTB column as datetime type
update tab set GLGTB = newcol --- update value from GLGTB from newcol
Alter table tab drop column newcol ---- remove unnecessary newcol
You can use the following code for updating but before that, you need to change the data type of your field to DateTime
您可以使用以下代码进行更新,但在此之前,
you need to change the data type of your field to DateTime
update dbo.USR_02_ALL_RAW
set GLTGB=cast(CONVERT(datetime,RIGHT(@Date,4)+LEFT(@Date,2)+SUBSTRING(@Date,4,2)) as datetime)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.