我将GUID存储在oracle 10g表的一列中。该列的数据类型为Varchar(2)。
现在,我要检查此列中的值是否为有效的GUID。

如何在Oracle中查询以检查值是否为有效的GUID?

最佳答案

我从here中获取了测试样本,并从here中获取了正则表达式:

create table guids ( guid varchar(36) );
insert into guids values ( 'BC1EAAAF-1B5A-4695-9797-3ED6C99B7FC5' );
insert into guids values ( 'BCXXAAAF-1B5A-4695-9797-3EDXXXXXXFC5' );
insert into guids values ( '-jk1029347 lka llur 193241 lk;qed' );

select guid,
    case when regexp_like(guid,
'^({|\()?[A-Fa-f0-9]{8}-([A-Fa-f0-9]{4}-){3}[A-Fa-f0-9]{12}(}|\))?$'
    )
  then 'Y'
  else 'N'
  end AS is_valid
from guids;

GUID                                 IS_VALID
------------------------------------ --------
BC1EAAAF-1B5A-4695-9797-3ED6C99B7FC5 Y
BCXXAAAF-1B5A-4695-9797-3EDXXXXXXFC5 N
-jk1029347 lka llur 193241 lk;qed    N

09-12 13:07