简体   繁体   中英

Convert column from varchar to datetime and update it in the table

I have a question related to the conversion of varchar to datetime. This topic was covered already in the thread SQL Server Convert Varchar to Datetime but I would like to advance it bit further.

I have performed BULK INSERT into predefined tables where VARCHAR(255) is the destination. I have a table dbo.USR_02_ALL_RAW and the field GLTGB which holds strings in the following format: 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

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 )

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. 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)

2) copy all the data values from the GLTGB column into the new column (using an UPDATE statement)

3) Drop the GLTGB column

4) Re-create it with the same name but with datetime type

5) Use an UPDATE statement to re-populate the new GLTGB column from your temporary column

6) Finally, drop the temporary column

There may be a simpler way but that seems like the obvious process.

First clear this, you want to Bulk insert or Bulk update . Since you already have a column 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 . 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

update dbo.USR_02_ALL_RAW 
   set GLTGB=cast(CONVERT(datetime,RIGHT(@Date,4)+LEFT(@Date,2)+SUBSTRING(@Date,4,2)) as datetime)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM