我在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"
}
请注意,字符串的长度看起来比实际的长多少?我认为这可能是由于以下屏幕截图(而不是完整的屏幕截图)
:所以我的问题是如何防止这种情况发生?我试图找到Alive
中的strpos
,但由于找不到文本,我总是得到false
。
答案 0 :(得分:0)
我最终为每个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