Import data with leading zeros - SQL Server

拥有回忆 提交于 2020-04-07 08:38:29

问题


I'm trying to import data into a table. I'm doing a bulk insert. I've created the table using a CREATE statement where all fields are nvarchar(max). I cannot understand why when the import is done, the data with leading zeros has been changed to scientific notation. Why does it not stay as text and preserve the leading zeros?


回答1:


I suggest that you define the number of zeros that do you want and then make an update. Here is an example with 10 zeros.

create table #leadingZeros(uglynumber nvarchar(max),handsomenumber nvarchar(max),nicenumber nvarchar(max))
INSERT INTO #leadingZeros VALUES(1000000,0000123,0500000)

SELECT * FROM #leadingZeros
--OUTPUT:
--uglynumber    handsomenumber  nicenumber
--1000000   123     500000
UPDATE #leadingZeros SET 
uglynumber=RIGHT('0000000000'+uglynumber,10)
,handsomenumber=RIGHT('0000000000'+handsomenumber,10)
,nicenumber=RIGHT('0000000000'+nicenumber,10)

SELECT * FROM #leadingZeros
--OUTPUT
--uglynumber    handsomenumber  nicenumber
--0001000000    0000000123  0000500000


来源:https://stackoverflow.com/questions/60561038/import-data-with-leading-zeros-sql-server

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