سؤال

لدي zipcode المخزنة في حقل نص (سلسلة) وترغب في تحديد آخر 3 أرقام من القيمة في عبارة SELECT الخاصة بي.هل هذا ممكن؟هل هناك طريقة قياسية للقيام بذلك بحيث تكون SQL قواعد بيانات قابلة للتبديل؟سوف أستخدمها في الإنتاج على Oracle، لكنني أقوم باختبار Interbase (نعم، نعم، أعرف، اثنين Diff DBB 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'
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top