Microsoft Access 2010 SQL拆分字符串在“X”和乘法

时间:2013-10-10 21:19:52

标签: sql ms-access-2010

我有一个包含一个包大小列的表,其中包含一个数据类型的文本,我需要将其转换为整数,这是出于数学原因。这一列中的值通常看起来像“100ML”,“20GM”,“UD 20”,“13OZ”这里它变得棘手,偶尔会出现像“6X12ML”,“UD 5X6ML”这样的值。其中带有“X”的那些我需要删除“ML”我正在用

执行此操作
Replace([TABLE_NAME].[COLUMN_NAME],"ML","")

在查询的表达式列中。我可以使用嵌套的Replace函数来删除“ML”,“GM”,“OZ”和“UD”。我尝试这样做的所有尝试都失败了,我认为最终的解决方案就像是

IIf([TABLE_NAME].[COLUMN_NAME] Like "X", (CInt(Left([TABLE_NAME].[COLUMN_NAME],InStr(1,[TABLE_NAME].[COLUMN_NAME],"X")-1))*CInt(Right([TABLE_NAME].[COLUMN_NAME],InStr(1,[TABLE_NAME].[COLUMN_NAME],"X")+1))),[TABLE_NAME].[COLUMN_NAME])

我尝试过使用上面代码的变体而无济于事。所有的建议都很受欢迎,我希望在一个查询中得到这个,但我确实知道我可以使用和表达,只是将“X”之前和之后的文本拆分成两个不同的表达式列。然后使用另一个查询来乘以值。

1 个答案:

答案 0 :(得分:1)

QTY_ORDERED: IIf(InStr(1,Replace(Replace(Replace(Replace([STANDARD_PRICING].[PACKAGE_AMOUNT],"GM",""),"ML",""),"UD","")," ",""),"X")>1,[CRX_HISTORIC_PO].[QUANTITY]/Left(Replace(Replace(Replace(Replace([STANDARD_PRICING].[PACKAGE_AMOUNT],"GM",""),"ML",""),"UD","")," ",""),InStr(1,Replace(Replace(Replace(Replace([STANDARD_PRICING].[PACKAGE_AMOUNT],"GM",""),"ML",""),"UD","")," ",""),"X")-1)*Right(Replace(Replace(Replace(Replace([STANDARD_PRICING].[PACKAGE_AMOUNT],"GM",""),"ML",""),"UD","")," ",""),Len(Replace(Replace(Replace(Replace([STANDARD_PRICING].[PACKAGE_AMOUNT],"GM",""),"ML",""),"UD","")," ",""))-InStr(1,Replace(Replace(Replace(Replace([STANDARD_PRICING].[PACKAGE_AMOUNT],"GM",""),"ML",""),"UD","")," ",""),"X"))*-1,[CRX_HISTORIC_PO].[QUANTITY]/Replace(Replace(Replace(Replace([STANDARD_PRICING].[PACKAGE_AMOUNT],"GM",""),"ML",""),"UD","")," ","")*-1)

上面的代码是我用来完成手头任务的代码。