问题
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