문자열의 추출 부분을위한 SQL
문제
텍스트 필드 (문자열)에 저장된 zipcode가 있고 select 문에서 값의 마지막 3 자리 만 선택하고 싶습니다.이것이 가능한가?SQL이 동시에 Accross 데이터베이스를 교환 할 수 있도록 표준 방법이 있습니까?나는 오라클에서 생산할 때 그것을 사용할 것입니다. 그러나 나는 Interbase (예, 예, 알아, 두 개의 완전히 diff dbs, 내가하고있는 것)
를 제공 할 수있는 도움을 주셔서 감사합니다.
해결책
Assuming the zipcodes all have the same length, you can use substr
.
If they don't have the same length, you have to do similar things with the strlen
function.
Interbase does not have a built-in substring function, but it does have a UDF
(user defined function) called SUBSTR
in lib_udf.dll
that works like this:
select substr(clients.lastname, 1, 10)
from clients
You declare the UDF like this:
DECLARE EXTERNAL FUNCTION SUBSTR
CSTRING(80),
SMALLINT,
SMALLINT
RETURNS CSTRING(80) FREE_IT
ENTRY_POINT 'IB_UDF_substr' MODULE_NAME 'ib_udf';
Oracle does have a built-in substr function that you use like this:
select substr(clients.lastname, 1, 10)
from clients
--jeroen
다른 팁
This depends on how your storing the zip code. If you are using 5 digits only then this should work for Oracle and may work for Interbase.
select * from table where substr(zip,3,3) = '014'
IF you store Zip + 4 and you want the last 3 digits and some are 5 digits and some are 9 digits you would have to do the following.
select * from table where substr(zip,length(zip) -2,3) = '014'
and one option that may work better in both databases is
select * from table where zip like '%014'