我在Oracle中有一个NVARCHAR2
字段,我正在做一个LISTAGG
并附加一些文本。
LISTAGG(
CASE
WHEN a.is_dead = 'Y' AND a.death_cause IS NOT NULL AND a.death_date IS NOT NULL THEN a.composite_pednum || ' - ' || a.death_cause || ' (' || a.death_date || ')'
WHEN a.is_dead = 'Y' AND a.death_cause IS NOT NULL THEN a.composite_pednum || ' - ' || a.death_cause
WHEN a.is_dead = 'Y' THEN a.composite_pednum || ' - Dead'
WHEN a.is_dead = 'N' THEN a.composite_pednum || ' - Alive'
ELSE a.composite_pednum || ' - Unknown Status'
END,
';'
) WITHIN GROUP (ORDER BY a.pedigree_number ASC) AS cage_animals
在PHP中,我将explode
这个文本转换成一个数组,以便以后处理。
$cage_animals = array();
if(!empty($data['CAGE_ANIMALS'][$i])) {
$cage_animals = explode(';', $data['CAGE_ANIMALS'][$i]);
}
这给了我类似的东西
["cage_animals"]=>
array(2) {
[0]=>
string(38) "R15-57713-B - Alive"
[1]=>
string(40) "R15-57714-RR - Alive"
}
请注意字符串的长度看起来比实际更多?我想这可能是因为下面的截图(不是完整的截图):
所以我的问题是如何防止这种情况?我试图找到strpos
的Alive
,我总是得到false
,因为找不到文字。
我最后使用CAST
或TO_CHAR
为我的每个THEN
条款。
LISTAGG(
CASE
WHEN a.is_dead = 'Y' AND a.death_cause IS NOT NULL AND a.death_date IS NOT NULL THEN TO_CHAR(a.composite_pednum || ' - ' || a.death_cause || ' (' || TO_CHAR(a.death_date, 'Mon DD, YYYY') || ')')
WHEN a.is_dead = 'Y' AND a.death_cause IS NOT NULL THEN TO_CHAR(a.composite_pednum || ' - ' || a.death_cause)
WHEN a.is_dead = 'Y' THEN TO_CHAR(a.composite_pednum || ' - Dead')
WHEN a.is_dead = 'N' THEN TO_CHAR(a.composite_pednum || ' - Alive')
ELSE TO_CHAR(a.composite_pednum || ' - Unknown Status')
END,
';'
) WITHIN GROUP (ORDER BY a.pedigree_number ASC) AS cage_animals