Import data with leading zeros - SQL Server

前端 未结 1 1271
隐瞒了意图╮
隐瞒了意图╮ 2021-01-25 22:09

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 w

相关标签:
1条回答
  • 2021-01-25 22:44

    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
    
    0 讨论(0)
提交回复
热议问题