简体   繁体   English

如何在ORACLE中选择SUBSTRING

[英]How Select SUBSTRING in ORACLE

I need select a subtring to get the consecutive number from a table field. 我需要选择一个subtring才能从表字段中获取连续的数字。 My table is: 我的桌子是:

ORDER_NUM       ORDER_DATE   ORDER_TYPE  LOCATION   SALE_TYPE
10501702315618  08/01/17       43223       1050        18    
105017023186230 21/01/17       43221       1050        230

The field ORDER_NUM is generated as follows 字段ORDER_NUM的生成如下

[LOCATION] + [YY] + [CONSECUTIVE_NUMBER] + [SALE_TYPE]

The length of the fields LOCATION and SALE_TYPE Can be different.So, my query is: LOCATION和SALE_TYPE字段的长度可以不同。因此,我的查询是:

SELECT 
SUBSTR(ORDER_NUM,LENGTH (ORDER_TYPE)  + 3,LENGTH (ORDER_NUM)   - LENGTH ( SALE_TYPE ) ),
ORDER_DATE
FROM 
CAT_ORDERS
WHERE 
LOCATION = '1050'
AND SALE_TYPE = '18'

The result is 结果是

    SELECT SUBSTR('10501702315618',7,12) from dual
    RESULT: 02315618

Where the index position is: 索引位置在哪里:

 12345678901234
 10501702315618

How i can delete the SALE_TYPE characters from the string? 我如何从字符串中删除SALE_TYPE字符? There is another function for this ? 还有另一个功能吗?

Thank's! 谢谢!

Your description refers to the location, so it's odd that your offset is based on order type. 您的描述是针对位置的,因此您的抵消额是基于订单类型的,这很奇怪。 The length of the substring needs to exclude the length of the location and the year as well: 子字符串的长度也需要排除位置的长度和年份:

SUBSTR(ORDER_NUM, LENGTH (LOCATION) + 3,
  LENGTH (ORDER_NUM) - LENGTH(LOCATION) - 2 - LENGTH (SALE_TYPE))

At the moment you're getting 12 characters from your offset, when you only want six, and there are only actually eight available. 目前,您将从偏移量中获取12个字符,而当您只需要6个字符时,实际上只有8个可用字符。

Demo with your data: 演示数据:

WITH CAT_ORDERS (ORDER_NUN, ORDER_DATE, ORDER_TYPE, LOCATION, SALE_TYPE) AS (
  SELECT 10501702315618, TO_DATE('08/01/17', 'DD/MM/RR'), 43223, 1050, 18 FROM DUAL
  UNION ALL SELECT 105017023186230, TO_DATE('21/01/17', 'DD/MM/RR'), 43221, 1050, 230 FROM DUAL
)
SELECT SUBSTR(ORDER_NUM, LENGTH (LOCATION) + 3,
  LENGTH (ORDER_NUM) - LENGTH(LOCATION) - 2 - LENGTH (SALE_TYPE))
FROM CAT_ORDERS;

023156
023186

Your order number duplicates data from other columns, which doesn't seem ideal. 您的订单号重复了其他列中的数据,这似乎并不理想。 It would be simpler to only store the 'consecutive number' part instead, and generate the full order number as a virtual column. 仅存储“连续编号”部分,并生成完整的订单编号作为虚拟列会更简单。

This should remove the sale_type characters from the end: 这应该从最后删除sale_type字符:

select 
  substr(substr(order_num, 0, length(order_num)-length(sale_type)), length(location) + 3)
from t;
select  regexp_substr(ORDER_NUM,'^' || LOCATION || '..(.*)' || SALE_TYPE || '$',1,1,'',1)
from    mytable;

Since we know now that the length of consecutive_number is fixed (namely, 6): 既然我们知道consecutive_number的长度是固定的(即6):

select  regexp_substr(ORDER_NUM,'^' || LOCATION || '..(.{6})',1,1,'',1)
from    mytable;

or 要么

select  regexp_substr(ORDER_NUM,'(.{6})' || SALE_TYPE || '$',1,1,'',1)
from    mytable;

Since we know now that the length of consecutive_number is fixed (namely, 6): 既然我们知道consecutive_number的长度是固定的(即6):

select  substr(ORDER_NUM,length(LOCATION)+3,6)
from    mytable;

or 要么

select  substr(ORDER_NUM,-length(SALE_TYPE)-6,6)
from    mytable;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM