SQL 如何在oracle查询中获取字符串的前两个字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6225392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:50:19  来源:igfitidea点击:

How to get first two characters of a string in oracle query?

sqloracle

提问by Vivek

Suppose I have a column name OrderNowith value AO025631in a table shipment.

假设我在表中有一个OrderNo带有值的列名。AO025631shipment

I am trying to query the table so that I can get only first two character of column value i.e. AO.

我正在尝试查询表,以便我只能获得列值的前两个字符,即AO.

Can I do this in the SQL query itself?

我可以在 SQL 查询本身中执行此操作吗?

回答by manji

SUBSTR(documentation):

SUBSTR(文档)

SELECT SUBSTR(OrderNo, 1, 2) As NewColumnName from shipment

When selected, it's like any other column. You should give it a name (with Askeyword), and you can selected other columns in the same statement:

选中后,它就像任何其他列一样。您应该为其命名(带As关键字),并且您可以在同一语句中选择其他列:

SELECT SUBSTR(OrderNo, 1, 2) As NewColumnName, column2, ... from shipment

回答by Tony Andrews

select substr(orderno,1,2) from shipment;

You may want to have a look at the documentationtoo.

您可能也想查看文档

回答by Datajam

Easy:

简单:

SELECT SUBSTR(OrderNo, 1, 2) FROM shipment;

回答by thomas

take a look here

看看这里

SELECT SUBSTR('Take the first four characters', 1, 4) FIRST_FOUR FROM DUAL;

回答by Farshid Zaker

Just use SUBSTRfunction. It takes 3 parameters: String column name, starting index and length of substring:

只需使用SUBSTR函数。它需要 3 个参数:字符串列名、子字符串的起始索引和长度:

select SUBSTR(OrderNo, 1, 2) FROM shipment;

回答by Shepherdess

Try select substr(orderno, 1,2) from shipment;

尝试从发货中选择 substr(orderno, 1,2);