替换SQL中字符串的某些部分

时间:2018-08-02 08:05:42

标签: sql sql-server

我有一个带有数据的平面文件,并且在每一行中都有自己想要的位置:

  1. 替换为货币值(这是一个小数)
  2. 将此小数点后两位设为11个字符。

我的数据示例如下:

ALD2018041620180416FRAP   25CEUR  0017537400        TUYAU DE FREIN BRAKE HOSE     P70   0000000203300000000000                          00001000000000000520J
ALD2018041620180416FRAP   25CEUR  0017689400        CYLINDRE ROUE  WHEEL CYLINDER W70   0000000145200000000000                          00001000000000000455K

在此文本中,我想将长度为11个字符的字符100处的文本替换为我计算的十进制值(该值是货币值),我需要将其转换为美分,然后将其转换为总共11个字符。

我要更改的部分在此之后(只是为了您可以轻松找到位置):

00000002033
00000001452

所以在上面的2行中,我想要以下结果:

ALD2018041620180416FRAP   25CEUR  0017537400        TUYAU DE FREIN BRAKE HOSE     P70   0000000203300000091972                          00001000000000000520J
ALD2018041620180416FRAP   25CEUR  0017689400        CYLINDRE ROUE  WHEEL CYLINDER W70   0000000145200000065688                          00001000000000000455K

这是我的结果:

ALD2018041620180416FRAP   25CEUR  0017537400        TUYAU DE FREIN BRAKE HOSE     P70   0000000203391972.920000                          0000191972.9200000520J
ALD2018041620180416FRAP   25CEUR  0017689400        CYLINDRE ROUE  WHEEL CYLINDER W70   0000000145265688.480000                          0000165688.4800000455K

我正在使用SUBSTRING来尝试在所需的特定位置进行替换,该方法可以工作....但是它也在文本中进一步替换,我不希望这样做(明显)。另外,我也不知道如何从小数点到末尾删除(这似乎总是带有小数点的7个字符),然后在前面添加正确数量的0以使整个长度为11个字符。

编辑

我已经想出了如何通过替换来替换第一次出现的事件:

 SET @TextToChange = REPLACE(@TextToChange,SUBSTRING(@TextToChange,100,11),(((CAST(@UnitPurchasePriceexVAT as decimal(11,2))/100) * @ExchangeRate * 100) * @RetailePriceValue)*100)

与此:

SET @TextToChange = STUFF(@TextToChange, CHARINDEX(SUBSTRING(@TextToChange,100,11), @TextToChange), LEN(SUBSTRING(@TextToChange,100,11)), (((CAST(@UnitPurchasePriceexVAT as decimal(11,2))/100) * @ExchangeRate * 100) * @RetailePriceValue)*100)

这是我正在尝试的代码:

DECLARE @FilePath as Varchar(1000)
DECLARE @TotalRecords as INT
DECLARE @RecordCount AS INT

SET @FilePath = 'C:\Client Files\Client\AP\2018-07-17\AP-XP0006-Test.txt'
DECLARE @UnitPurchasePriceexVAT As Varchar(11)
DECLARE @TextToChange As Varchar(MAX)
DECLARE @ExchangeRate as Decimal(18,4)
DECLARE @RetailePriceValue as Decimal(18,4)
SET @ExchangeRate = (SELECT CalcValue FROM CalculationValues WHERE CalcName = 'Exchange Rate')
SET @RetailePriceValue = (SELECT CalcValue FROM CalculationValues WHERE CalcName = 'Retail Price Value')

CREATE TABLE #SparePartsPriceList_Temp
(Text Varchar(MAX) NULL)

DECLARE @SQL as Varchar(MAX)

SET @SQL = 'bulk insert #SparePartsPriceList_Temp
from ''' + @FilePath + ''' 
with (fieldterminator = ''\t'', rowterminator = ''0x0a'')'

EXEC (@SQL)

CREATE TABLE #SparePartsPriceList_Temp_staging
(ID int IDENTITY(1,1) PRIMARY KEY,
 Text Varchar(MAX) NULL)

 CREATE TABLE #SparePartsPriceList_Temp_Export
(Text Varchar(MAX) NULL)

INSERT INTO #SparePartsPriceList_Temp_staging(Text)
SELECT Text FROM #SparePartsPriceList_Temp

SET @TotalRecords = (SELECT COUNT(TEXT) FROM #SparePartsPriceList_Temp_staging)
SET @RecordCount = 1

WHILE (@RecordCount <= @TotalRecords)
BEGIN
    SET @TextToChange = (SELECT Text FROM #SparePartsPriceList_Temp_staging WHERE ID = @RecordCount)

    SET @UnitPurchasePriceexVAT = (SELECT SUBSTRING(@TextToChange,89,11))

    SET @TextToChange = STUFF(@TextToChange, CHARINDEX(SUBSTRING(@TextToChange,100,11), @TextToChange), LEN(SUBSTRING(@TextToChange,100,11)), (((CAST(@UnitPurchasePriceexVAT as decimal(11,2))/100) * @ExchangeRate * 100) * @RetailePriceValue)*100)

    INSERT INTO #SparePartsPriceList_Temp_Export
    SELECT @TextToChange

    SET @RecordCount = @RecordCount + 1
END

SELECT * FROM #SparePartsPriceList_Temp_Export
SELECT * FROM #SparePartsPriceList_Temp
DROP TABLE #SparePartsPriceList_Temp
DROP TABLE #SparePartsPriceList_Temp_staging
DROP TABLE #SparePartsPriceList_Temp_Export

这有点冗长,但是我不知道该怎么解释。我希望我想要的东西有意义,并且我不会受到太多惩罚...

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

所以我终于得到了答案!我不确定这是否会对任何人有帮助,但我只是在发布,以防万一:

DECLARE @DealerType as VarChar(20)
DECLARE @FilePath as Varchar(1000)
DECLARE @TotalRecords as INT
DECLARE @RecordCount AS INT
DECLARE @DecimalIndex as INT


SET @DealerType = 'ClientName'
SET @FilePath = 'C:\Client Files\Client\AP\2018-07-17\AP-XP0006_MULTI.txt'
DECLARE @UnitPurchasePriceexVAT As Varchar(11)
DECLARE @NewUnitSellingPriceExVAT as Varchar(11)
DECLARE @TextToChange As Varchar(MAX)
DECLARE @ExchangeRate as Decimal(18,4)
DECLARE @RetailePriceValue as Decimal(18,4)
SET @ExchangeRate = (SELECT CalcValue FROM CalculationValues WHERE CalcName = 'Exchange Rate')
SET @RetailePriceValue = (SELECT CalcValue FROM CalculationValues WHERE CalcName = 'Retail Price Value')

CREATE TABLE #SparePartsPriceList_Temp
(Text Varchar(MAX) NULL)

DECLARE @SQL as Varchar(MAX)

SET @SQL = 'bulk insert #SparePartsPriceList_Temp
from ''' + @FilePath + ''' 
with (fieldterminator = ''\t'', rowterminator = ''0x0a'')'

EXEC (@SQL)

CREATE TABLE #SparePartsPriceList_Temp_staging
(ID int IDENTITY(1,1) PRIMARY KEY,
 Text Varchar(MAX) NULL)

 CREATE TABLE SparePartsPriceList_Temp_Export
(Text Varchar(MAX) NULL)

INSERT INTO #SparePartsPriceList_Temp_staging(Text)
SELECT Text FROM #SparePartsPriceList_Temp

SET @TotalRecords = (SELECT COUNT(TEXT) FROM #SparePartsPriceList_Temp_staging)
SET @RecordCount = 1

WHILE (@RecordCount <= @TotalRecords)
BEGIN
    SET @TextToChange = (SELECT Text FROM #SparePartsPriceList_Temp_staging WHERE ID = @RecordCount)

    SET @UnitPurchasePriceexVAT = (SELECT SUBSTRING(@TextToChange,89,11))
    SET @NewUnitSellingPriceExVAT = CAST((((CAST(@UnitPurchasePriceexVAT as decimal(11,2))/100) * @ExchangeRate * 100) * @RetailePriceValue)*100 as varchar)
    SET @DecimalIndex = CHARINDEX('.',@NewUnitSellingPriceExVAT,1)
    SET @NewUnitSellingPriceExVAT = STUFF(@NewUnitSellingPriceExVAT, CHARINDEX(SUBSTRING(CAST(@NewUnitSellingPriceExVAT as Varchar),@DecimalIndex,7), @NewUnitSellingPriceExVAT), 
                        LEN(SUBSTRING(CAST(@NewUnitSellingPriceExVAT as Varchar),@DecimalIndex,7)), 
                        '')

    SET @NewUnitSellingPriceExVAT = REPLICATE('0',11-LEN(@NewUnitSellingPriceExVAT)) + @NewUnitSellingPriceExVAT
    SET @TextToChange = STUFF(@TextToChange, CHARINDEX(SUBSTRING(@TextToChange,100,11), @TextToChange), LEN(SUBSTRING(@TextToChange,100,11)), @NewUnitSellingPriceExVAT)

    INSERT INTO SparePartsPriceList_Temp_Export
    SELECT @TextToChange

    SET @RecordCount = @RecordCount + 1
END

SELECT * FROM SparePartsPriceList_Temp_Export
SELECT * FROM #SparePartsPriceList_Temp
DROP TABLE #SparePartsPriceList_Temp
DROP TABLE #SparePartsPriceList_Temp_staging
DROP TABLE SparePartsPriceList_Temp_Export