[英]Edit DB2 data on specific column
Thank you in advance for any help...I have to edit some data on my DB2 database - working with version 11. Basically I have a column with multiple entries that look like this: <GUID=b92c4c2340953g98hn298743n5634234> and I need them to look like this: b92c4c23-4095-3g98-hn29-8743n5634234提前感谢您的帮助...我必须在我的 DB2 数据库上编辑一些数据 - 使用版本 11。基本上我有一个包含多个条目的列,如下所示:<GUID=b92c4c2340953g98hn298743n5634234> 我需要它们查看像这样:b92c4c23-4095-3g98-hn29-8743n5634234
Note** I am trying to create a function that takes in value A (<GUID=b92c4c2340953g98hn298743n5634234>) and returns value B (b92c4c23-4095-3g98-hn29-8743n5634234)注意** 我正在尝试创建一个 function 接受值 A (<GUID=b92c4c2340953g98hn298743n5634234>) 并返回值 B (b92c4c23-4095-3g98-hn29-8743n5634234)
Use substring function SUBSTR
as follows:使用 substring function
SUBSTR
如下:
SUBSTR(SUBSTR(your_column, 1, LENGTH(your_column) - 1), 7)
Example:例子:
with t(s) as (
values '<GUID=b92c4c2340953g98hn298743n5634234>'
), strip(s) as (
select substr(s,7,length(s)-7) from t
)
select substr(s,1,8) || '-' || substr(s,9,4) || '-' || substr(s,13,4) || '-'
|| substr(s,17,4) || '-' || substr(s,21)
from strip;
1
---------------------------------------------------------------
b92c4c23-4095-3g98-hn29-8743n5634234
If you do this a lot you may want to create a function:如果你经常这样做,你可能想要创建一个 function:
create function trim_guid(s varchar(39))
returns varchar(36)
return with strip(s) as (
values substr(s,7,length(s)-7)
) select substr(s,1,8) || '-' || substr(s,9,4) || '-'
|| substr(s,13,4) || '-' || substr(s,17,4) || '-'
|| substr(s,21) from strip
with t(s) as ( values '<GUID=b92c4c2340953g98hn298743n5634234>' )
select trim_guid(s) from t
1
------------------------------------
b92c4c23-4095-3g98-hn29-8743n5634234
Db2 has some built in facilities for working with GUID values. Db2 有一些用于处理 GUID 值的内置工具。 There are functions to convert them to/from binary and character representations.
有一些函数可以将它们转换为二进制和字符表示。
If your data are valid GUIDs, you can use the following for example to return a character GUID representation.如果您的数据是有效的 GUID,您可以使用以下示例返回字符 GUID 表示。
VALUES
VARCHAR_FORMAT_BIT(
CAST(HEXTORAW(SUBSTR('<GUID=d83d6360181811db9804b622a1ef5492>',7,32)) AS CHAR(16) FOR BIT DATA)
, 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
)
will return将返回
d83d6360-1818-11db-9804-b622a1ef5492
Needless to say, storing the data as BINARY(16)
(aka CHAR(16) FOR BIT DATA
) is typically more efficient than using eg VARCHAR(32)
.不用说,将数据存储为
BINARY(16)
(又名CHAR(16) FOR BIT DATA
)通常比使用VARCHAR(32)
更有效。 Still if GUIDs themselves are less efficient (from a database point of view) than eg integer sequence identifers.如果 GUID 本身的效率比 integer 序列标识符低(从数据库的角度来看)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.