0

基本上,我想跳过随机数的零。我认为,解决方案是在格式文件中创建一个以第一个非零值结尾的虚拟列。但是,在淘完网之后,我不知道该怎么做。

编辑:澄清每一行前面都有随机数0。

例如

000004412900000009982101201021042010
000000935000000009902005199322071993
4

5 回答 5

0
ALTER Function [dbo].[fn_CSVToTable] (@CSVList Varchar(5000))
Returns @Table Table (ColumnData Varchar(50))
As
Begin
If right(@CSVList, 1) <> ','
Select @CSVList = @CSVList + ','

Declare @Pos    Smallint,
@OldPos Smallint
Select  @Pos    = 1,
@OldPos = 1

While   @Pos < Len(@CSVList)
Begin
Select  @Pos = CharIndex(',', @CSVList, @OldPos)
Insert into @Table
Select  LTrim(RTrim(SubString(@CSVList, @OldPos, @Pos - @OldPos))) Col001
Select  @OldPos = @Pos + 1
End
Return
End
于 2013-01-08T06:15:16.357 回答
0

I had to do something similar on a MLS import. Here is essentially what I did.

BULK INSERT TmpTable FROM [CSVPATH] 
WITH (FIELDTERMINATOR = ',' , ROWTERMINATOR = '\n')

Then fix the data you need to fix (the leading zeros)

There a few ways you can do this, but I will list two.

  1. UPDATE TmpTable Set TmpCol = CASE WHEN LEFT(TmpCol,1) = "0" THEN LTRIM(TmpCol, LEN(TmpCol) -1) ELSE TmpCol END

    2. UPDATE TmpTable Set TmpCol = SUBSTRING(TmpCol, PATINDEX('%[^0]%', TmpCol + '.'), LEN(TmpCol))

于 2012-07-24T16:03:16.270 回答
0

尝试这个。

DECLARE @str varchar(100)='000004412900000009982101201021042010'
DECLARE @str1 varchar(100)='000000935000000009902005199322071993'

    SELECT @str,RIGHT(@str,LEN(@str)-(PATINDEX('%'+'[1-9]'+'%',@str)-1)),@str1,RIGHT(@str1,LEN(@str1)-(PATINDEX('%'+'[1-9]'+'%',@str1)-1))
于 2012-07-25T04:09:01.273 回答
0

首先,我将它按原样导入到临时创建的列中的行中,然后创建一个用户定义的函数或基于这个集合

循环并删除了第一个 char 为“0”的字符

TempCol = CASE WHEN LEFT(TempCol,1) = "0" THEN LTRIM(TempCol, LEN(TempCol) -1)
          ESLE TempCol
END

LOOP WHILE @LEN changes

@LEN = SUM(LEN(TempCol))
SELECT @LEN
于 2012-07-24T14:41:48.473 回答
0

我假设 .csv 文件导入过程的第一步是将文件导入数据库。然后第二步就是清理数据了。

要在导入后删除零,您可以创建一个函数:

 create function [dbo].[RemoveLeadingZero](@strSrc as varchar(8000))
 returns varchar(8000)
 as
 begin
      declare  @strResult varchar(8000)
      set @strResult = ''
      begin
          set @strResult = Replace(Ltrim(Replace(@strSrc, '0', ' ')), ' ', '0')
      end
      return @strResult
 end
select dbo.RemoveLeadingZero('000004412900000009982101201021042010')

update myTable
set columnA = dbo.RemoveLeadingZero(columnA)
于 2012-07-24T17:45:04.260 回答