根据值的长度删除某些单词

时间:2014-10-21 16:22:43

标签: sql vba ms-access access-vba ms-access-2013

我在查询中有一个串联,它从不同的字段中获取值并创建产品名称。我的问题是我需要将名称保持在80个字符以下,所以如果它在那里传递80个字符 我应该能够删除“名称”中的某些关键词。例如

Oracle Lights 79-80 American Motors AMX 4x6" LED Amber Halo 2 Sealed Headlights 

很好,它只有不到80个字符。

但是:

Oracle Lights 79-83 American Motors Concord 4x6" LED ColorSHIFT Halo 2 Sealed Headlights

这是88个字符,所以我必须找出一种方法,如果这个连接太长,可以删除单词Lights。如果它仍然超过80个字符,则应删除世界Oracle

对于任何想要了解字段[year_single] = 79-80, [make] = American Motors, [model] = AMX, and [color] = Amber的人。其他所有内容都是串联中添加的字符串,因为它是静态的。

任何人都知道我可以做到这一点吗?

3 个答案:

答案 0 :(得分:4)

您可以在vba中尝试以下操作。

if len(yourfield) > 80 then Replace(yourfield, "Lights ", "") end if
if len(yourfield) > 80 then Replace(yourfield, "Oracle ", "") end if

答案 1 :(得分:2)

对于SQL“纯粹主义者”,这里有一种方法可以在没有VBA的情况下完成。给定名为[tblData]的实际数据表

ID  year_single  make             model    color     
--  -----------  ---------------  -------  ----------
 1  79-80        American Motors  AMX      Amber     
 2  79-83        American Motors  Concord  ColorSHIFT

以及包含填充文本可能变体的表格:

[tblFiller1]

Filler1      
-------------

Oracle       
Oracle Lights

[tblFiller2]

Filler2 
--------
4x6"    
4x6" LED

和[tblFiller3]

Filler3                 
------------------------
Halo 2 Sealed Headlights

我们可以创建一个名为[qryDescripOptions]

的已保存的Access查询
SELECT
    ID,
    Len(Descrip) AS DescripLen,
    Descrip
FROM
    (
        SELECT 
            ID,
            Filler1 & IIf(Len(Nz(Filler1,""))>0," ","") & 
                year_single & " " & make & " " & model & 
                IIf(Len(Nz(Filler2,""))>0," ","") & Filler2 & 
                " " & color & 
                IIf(Len(Nz(Filler3,""))>0," ","") & Filler3
                AS Descrip
        FROM
            tblData, tblFiller1, tblFiller2, tblFiller3
    )

使用表上的交叉JOIN来为我们提供所有排列:

ID  DescripLen  Descrip                                                                                 
--  ----------  ----------------------------------------------------------------------------------------
 1          61  79-80 American Motors AMX 4x6" Amber Halo 2 Sealed Headlights                           
 2          70  79-83 American Motors Concord 4x6" ColorSHIFT Halo 2 Sealed Headlights                  
 1          65  79-80 American Motors AMX 4x6" LED Amber Halo 2 Sealed Headlights                       
 2          74  79-83 American Motors Concord 4x6" LED ColorSHIFT Halo 2 Sealed Headlights              
 1          68  Oracle 79-80 American Motors AMX 4x6" Amber Halo 2 Sealed Headlights                    
 2          77  Oracle 79-83 American Motors Concord 4x6" ColorSHIFT Halo 2 Sealed Headlights           
 1          72  Oracle 79-80 American Motors AMX 4x6" LED Amber Halo 2 Sealed Headlights                
 2          81  Oracle 79-83 American Motors Concord 4x6" LED ColorSHIFT Halo 2 Sealed Headlights       
 1          75  Oracle Lights 79-80 American Motors AMX 4x6" Amber Halo 2 Sealed Headlights             
 2          84  Oracle Lights 79-83 American Motors Concord 4x6" ColorSHIFT Halo 2 Sealed Headlights    
 1          79  Oracle Lights 79-80 American Motors AMX 4x6" LED Amber Halo 2 Sealed Headlights         
 2          88  Oracle Lights 79-83 American Motors Concord 4x6" LED ColorSHIFT Halo 2 Sealed Headlights

所以查询

SELECT
    ID,
    MAX(DescripLen) AS MaxOfDescripLen
FROM qryDescripOptions
WHERE Descriplen<=80
GROUP BY ID

为我们提供了仍然适合80个字符的每个ID的最长描述的长度

ID  MaxOfDescripLen
--  ---------------
 1               79
 2               77

现在我们可以将它连接回我们保存的查询以检索实际描述

SELECT
    q.ID,
    q.Descrip
FROM
    qryDescripOptions q
    INNER JOIN
    (
        SELECT
            ID,
            MAX(DescripLen) AS MaxOfDescripLen
        FROM qryDescripOptions
        WHERE Descriplen<=80
        GROUP BY ID
    ) maxlen
        ON q.ID = maxlen.ID AND q.DescripLen = maxlen.MaxOfDescripLen
制造

ID  Descrip                                                                        
--  -------------------------------------------------------------------------------
 1  Oracle Lights 79-80 American Motors AMX 4x6" LED Amber Halo 2 Sealed Headlights
 2  Oracle 79-83 American Motors Concord 4x6" ColorSHIFT Halo 2 Sealed Headlights  

在这种特殊情况下,对于给定ID的MaxOfDescripLen值没有“绑定”引起的重复,但是为了防止这种可能性,我们将整个事物包装在聚合查询中

SELECT 
    ID, 
    MIN(Descrip) AS Description
FROM
    (
        SELECT
            q.ID,
            q.Descrip
        FROM
            qryDescripOptions q
            INNER JOIN
            (
                SELECT
                    ID,
                    MAX(DescripLen) AS MaxOfDescripLen
                FROM qryDescripOptions
                WHERE Descriplen<=80
                GROUP BY ID
            ) maxlen
                ON q.ID = maxlen.ID AND q.DescripLen = maxlen.MaxOfDescripLen
    )
GROUP BY ID

答案 2 :(得分:1)

好的,感谢Phicon推动我朝着正确的方向发展。我创建了一个函数,它接受一个字符串并检查它的长度,然后取出&#34; Lights&#34;如果字符串超过80个字符,并且&#34; Oracle&#34;之后,如果它仍然超过80个字符,则在更新查询中调用它。

Public Function shortname(value As String) As String
    Dim temp As String
    temp = value

    If Len(temp) > 80 Then temp = Replace(temp, "Lights ", "", 1, 1)
    If Len(temp) > 80 Then temp = Replace(temp, "Oracle ", "", 1, 1)

    shortname = temp
End Function

更新查询:

UPDATE tbldata SET name = shortname(name);