搜索使用' LIKE'在Oracle中

时间:2015-02-02 02:57:03

标签: sql oracle

我在搜索oracle中的字符串时有一个问题,我在使用像'M_'这样的字符串搜索,但它不起作用。有人会帮我整理一下这个:

    SELECT UNIQUE
    (SELECT SUBSTR (E.PERSON_NAME,
    1,
    INSTR (PERSON_NAME, ',', 1) - 1)
    FROM SEC_PERSON E
    WHERE PERSON_AID = LV_USERID)
    first_Name,
    (SELECT SUBSTR (E.PERSON_NAME,
    INSTR (E.PERSON_NAME,
    ',',
    -1,
    1)
    + 1)
    FROM SEC_PERSON E
    WHERE PERSON_AID = LV_USERID)
    last_Name,
    (SELECT FK_ROLE_ID
    FROM SEC_APPLICATION_PERSON_ASSOC
    WHERE FK_PERSON_AID = LV_USERID)
    user_Role,
    A.MARKET_SEGMENT_NAME market_Name,
    A.MARKET_SEGMENT_ID marker_Id,
    B.SECTOR_NAME sector_Name,
    B.SECTOR_NAME_UPPER sector_Display_Name,
    B.SECTOR_ID sector_Id,
    D.EEQ_SOL_NAME end_Equipment_Name,
    D.EEQ_SOL_NAME_UPPER end_Equipment_Display_Name,
    D.EEQ_SOL_ID end_Equipment_Id,
    NVL(D.APPROVED_FLAG,'N') APPROVED_FLAG,
    NVL(D.CCB_FLAG,'N') CCB_FLAG,
    C.WEB_CATEGORY_NAME web_Category_Name,
    C.WEB_CATEGORY_ID web_Category_Id,
    NVL(D.EXPORT_CONTROL_FLAG,'N') export_Flag,
    E.SALES_FLAG available_Sales_Flag,
    NVL(D.TICOM_FLAG,'N') available_TI_Flag,
    A.UPDATE_DATE modified_Date,
    E.PERSON_AID modified_Name,
    NVL(D.OBSOLETE_FLAG,'N') obsolete_Flag,
    NVL(D.TICOM_SUB_SYSTEM,'N')  sub_system_Flag
    FROM MSE_MARKET_SEGMENT_MASTER a,
    MSE_SECTOR_MASTER b,
    MSE_WEB_CATEGORY_MASTER c,
    MSE_EE_MASTER d,
    SEC_PERSON e,
    SEC_APPLICATION_PERSON_ASSOC f,
    SEC_APPLICATION g,
    SEC_ROLE h
    WHERE     a.MARKET_SEGMENT_ID = b.FK_MARKET_SEGMENT_ID(+)
    AND b.SECTOR_ID = c.FK_SECTOR_ID(+)
    AND d.FK_SECTOR_ID(+) = b.SECTOR_ID
    AND e.PERSON_AID = f.FK_PERSON_AID
    AND g.APPLICATION_ID = h.FK_APPLICATION_ID
    AND NVL(A.MARKET_SEGMENT_NAME_UPPER,'X')  LIKE
    NVL2 ('%'||LV_Market_Name||'%',
    '%'||LV_Market_Name||'%',
    NVL('%'||A.MARKET_SEGMENT_NAME_UPPER||'%','X'))
    AND NVL(b.SECTOR_NAME_UPPER,'X')  LIKE
    NVL2 ('%'||LV_Sector_Name||'%',
    '%'||LV_Sector_Name||'%',
    NVL('%'||b.SECTOR_NAME_UPPER||'%','X'))
    AND NVL(D.EEQ_SOL_NAME_UPPER,'X') LIKE
    NVL2 ('%'||LV_End_Equipment_Name||'%',
    '%'||LV_End_Equipment_Name||'%',
    NVL('%'||D.EEQ_SOL_NAME_UPPER||'%','X'))
    AND NVL(C.WEB_CATEGORY_NAME_UPPER,'X') LIKE
    NVL2 ('%'||LV_Web_Category_Name||'%',
    '%'||LV_Web_Category_Name||'%',
    NVL('%'||C.WEB_CATEGORY_NAME_UPPER||'%','X'))
    AND A.UPDATE_USER LIKE
    NVL2 ('%'||pv_modified_Name||'%',
    '%'||PV_modified_Name||'%',
    '%'||A.UPDATE_USER||'%')
    AND TRUNC (A.UPDATE_DATE) =
    NVL2 (pv_modified_Date,
    PV_modified_Date,
    TRUNC (A.UPDATE_DATE))
    AND E.PERSON_AID = LV_USERID
    ORDER BY a.MARKET_SEGMENT_NAME,
    a.MARKET_SEGMENT_ID,
    b.SECTOR_NAME,
    b.SECTOR_ID,
    c.WEB_CATEGORY_NAME,
    c.WEB_CATEGORY_ID,
    D.EEQ_SOL_NAME,
    D.EEQ_SOL_ID;

到这个字符串我从Java Front端传递一个动态值。 但是当我传递像'M_'这样的值时,搜索没有选择正确的值。

任何人都可以为此解决问题(搜索'_')

由于 IK

1 个答案:

答案 0 :(得分:4)

 select * from Market_data where MARKET_SEGMENT_NAME like '%M_%'; 
  

输出很像(Market_two Market_1 NewTestMarket M_testing2 Market_ONE)


这是因为您使用%开始查询,以便它与字符串中任意位置的M匹配。

同样_是一个通配符,意思是"任何单个字符" (恰好一个)。

您想要将其转义为仅匹配下划线。

 select * from Market_data where MARKET_SEGMENT_NAME like 'M$_%' ESCAPE '$';