Using cast in insert statement

岁酱吖の 提交于 2021-02-08 14:14:21

问题


I am inserting some raw data into a table in MS SQL 2005 from excel. Some of these data are not formatted correctly ie the amount colum is formatteT as a number 12345 whereas i need to be like 123.45 so i use this CAST(TRANSACTION_HISTORY.AMOUNT AS decimal) / 100 to convert it correctly. However is there a way to use the cast in an insert statement??

thanks


回答1:


You can use CAST in any kind of statement(Insert, update, delete, select) where you use data.

Insert into table1 values( CAST(col1 as nvarchar(50)) )



回答2:


I assume you're using a linked server or openquery to get the data from excel. You can cast in the select statement.

So

INSERT INTO YourTable
SELECT Cast(Transaction_History.Amount AS Decimal)/100
FROM EXCELLINK...[$Sheet1]

You could also just update all values in the table after you do the import

UPDATE YourTable
SET YourColumn = YourColumn/100


来源:https://stackoverflow.com/questions/5003164/using-cast-in-insert-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!