作者:文巧语 | 来源:互联网 | 2023-02-05 14:23
CurrentlyworkingwithaFirebird1.5databaseandattemptingtopullthedatainthecorrectformat
Currently working with a Firebird 1.5 database and attempting to pull the data in the correct format natively with SQL.
目前正在使用Firebird 1.5数据库并尝试使用SQL本地提取正确格式的数据。
Consider the following database:
考虑以下数据库:
ID | Full Name
1 Jon Doe
2 Sarah Lee
What I am trying to achieve is a simple split on the full name field (space) within a query.
我想要实现的是对查询中的全名字段(空格)进行简单拆分。
ID | First Name | Last Name
1 Jon Doe
2 Sarah Lee
The issue faced is Firebird POSITION() was introduced in v2.0. Is there any known workaround to split on a space that anyone has come across?
面临的问题是Firebird POSITION()在v2.0中引入。是否有任何已知的解决方法可以拆分任何人遇到过的空间?
Much appreciate your assistance!
非常感谢您的帮助!
3 个解决方案
3
For Firebird 1.5 a solution is to find a UDF that either combines both functions, or provides the position (I don't use UDFs, so I am not sure if one already exists). If none is available you might have to write one.
对于Firebird 1.5,解决方案是找到一个UDF,它既可以组合两个函数,也可以提供位置(我不使用UDF,所以我不确定是否已存在)。如果没有,你可能需要写一个。
The other solution is to write a stored procedure for this functionality, see for example: Position of substring function in SP
另一种解决方案是为此功能编写存储过程,例如参见:SP中子字符串函数的位置
CREATE PROCEDURE Pos (SubStr VARCHAR(100), Str VARCHAR(100))
RETURNS (Pos INTEGER) AS
DECLARE VARIABLE SubStr2 VARCHAR(201); /* 1 + SubStr-lenght + Str-length */
DECLARE VARIABLE Tmp VARCHAR(100);
BEGIN
IF (SubStr IS NULL OR Str IS NULL)
THEN BEGIN Pos = NULL; EXIT; END
SubStr2 = SubStr || '%';
Tmp = '';
Pos = 1;
WHILE (Str NOT LIKE SubStr2 AND Str NOT LIKE Tmp) DO BEGIN
SubStr2 = '_' || SubStr2;
Tmp = Tmp || '_';
Pos = Pos + 1;
END
IF (Str LIKE Tmp) THEN Pos = 0;
END
This example (taken from the link) can be extended to then use SUBSTRING
to split on the space.
此示例(取自链接)可以扩展为然后使用SUBSTRING在空间上拆分。
For search on a single character like a space a simpler solution can probably be devised than above stored procedure. For your exact needs you might need to write a selectable stored procedure specifically for this purpose.
对于像空间这样的单个字符的搜索,可能设计出比上述存储过程更简单的解决方案。为满足您的确切需求,您可能需要专门为此目的编写可选择的存储过程。
However, upgrading your database to Firebird 2.5 will give you much more powerful internal functions that simplify this query (and your life)!
但是,将数据库升级到Firebird 2.5将为您提供更强大的内部功能,以简化此查询(以及您的生活)!