Postgresql中ELT()函数的替代方法是什么?

时间:2019-10-30 04:20:13

标签: mysql postgresql

在MySQL中,可以使用ELT()函数从列表中的指定位置返回项目。

如何在Postgresql中找到

---在MYSQL中

SELECT ELT(3, 'Cat', 'Dog', 'Horse') AS 'Result';

---结果----------

 Horse  

2 个答案:

答案 0 :(得分:2)

使用数组:

select (array['Cat', 'Dog', 'Horse'])[3];

如果出于兼容性原因需要功能:

create or replace function elt(int, variadic text[])
returns text language sql immutable as $$
    select $2[$1]
$$;

select elt(3, 'Cat', 'Dog', 'Horse') AS "Result";

db<>fiddle.中的实时演示

答案 1 :(得分:1)

您可能必须在Postgres中使用ToString表达式。替换:

var l1 = Int64.MaxValue;
Console.WriteLine($"Int64.MaxValue: {l1}");
double d = l1;

foreach(var x in new []{"C", "E", "F", "F0", "G", "G15", "G30", "N", "P", "R", "##########################"}) {
    var dStr = d.ToString(x);
    Console.WriteLine($"{x} {dStr}");

    try { Console.WriteLine($"\t long diff: {long.Parse(dStr, NumberStyles.Any) - long.MaxValue}");} 
    catch (Exception ex) { Console.WriteLine($"\t long.Parse: {ex.Message}");};

    try { Console.WriteLine($"\t ulong diff: {ulong.Parse(dStr, NumberStyles.Any) - long.MaxValue}");} 
    catch (Exception ex) { Console.WriteLine($"\t ulong.Parse: {ex.Message}");};

    Console.WriteLine();
}

具有:

// .NETCoreApp,Version=v3.0
Int64.MaxValue: 9223372036854775807
C $9,223,372,036,854,775,808.00
         long.Parse: Value was either too large or too small for an Int64.
         ulong diff: 1

E 9.223372E+018
         long diff: -36854775807
         ulong diff: 18446744036854775809

F 9223372036854775808.00
         long.Parse: Value was either too large or too small for an Int64.
         ulong diff: 1

F0 9223372036854775808
         long.Parse: Value was either too large or too small for an Int64.
         ulong diff: 1

G 9.223372036854776E+18
         long.Parse: Value was either too large or too small for an Int64.
         ulong diff: 193

G15 9.22337203685478E+18
         long.Parse: Value was either too large or too small for an Int64.
         ulong diff: 4193

G30 9223372036854775808
         long.Parse: Value was either too large or too small for an Int64.
         ulong diff: 1

N 9,223,372,036,854,775,808.00
         long.Parse: Value was either too large or too small for an Int64.
         ulong diff: 1

P 922,337,203,685,477,580,800.00%
         long.Parse: Input string was not in a correct format.
         ulong.Parse: Input string was not in a correct format.

R 9.223372036854776E+18
         long.Parse: Value was either too large or too small for an Int64.
         ulong diff: 193

########################## 9223372036854780000
         long.Parse: Value was either too large or too small for an Int64.
         ulong diff: 4193