如何编写 DB2 select 查询以在“-”定界符之前包含字符串

How to write a DB2 select query to include the string before the '-' delimiter

我的Table“学院”中有一列“部门”。部门有类似

的数据
Commerce1-683877
Science2-678900

我需要编写一个 select 查询,其中 returns 只有 Commerce1 和 Science2。

我是 DB2 的新手,请帮我解决这个问题。

select substring(department,0,CHARINDEX('-',department) as DEPT from College 

编辑-1:谢谢@Charles

我试过你的解决方案:
select substring(department , 1, LOCATE('-',department) - 1) AS DEPT from College

但是它给我一个错误:

SQL Error [42815]: THE DATA TYPE, LENGTH, OR VALUE OF ARGUMENT 3 OF SUBSTRING IS INVALID. SQLCODE=-171, SQLSTATE=42815, DRIVER=4.9.78

编辑 2:你是对的@Charles,我所有的行都不包含“-” 我也尝试使用下面的查询,但得到了同样的错误:

select substring(department,1, LOCATE('-',department || '-') - 1) as DEPT from College

Db2 是什么平台和版本?

您是否在寻找合适的 Db2 SQL 参考手册?

注意 Db2 SQL 字符串和数组从 1 开始,而不是 0。

select substring(department,1, LOCATE('-',department) - 1) as DEPT from College

一种方法使用 regexp_substr():

regexp_substr(department, '^[^-]+')