简体   繁体   中英

Comparing values from part of a column in SQL Server

I have 2 columns in DB2 Table: PARID and LOC

PARID VARCHAR2 (10) and values are like 1005620001 LOC CHAR(1) .

Now I have to compare the first digit of PARID and LOC values. How can I get the first digit of the PARID in SQL query?

Thank You.

If your first digit is always the first character in PARID,

LEFT(PARID, 1) 

will do the job.

If it cannot be guarantee that the first digit is the first character on PARID,

REGEXP_SUBSTR(PARID, '\d', 1, 1)

will do the job, as in the following example:

SELECT REGEXP_SUBSTR('  ABC.7.XY', '\d', 1, 1) as FIRST_DIGIT FROM SYSIBM.SYSDUMMY1

with result:

 FIRST_DIGIT
 -----------
 7

LEFT(PARID, 1)将返回列的第一位

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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