Postgresql DB数据类型问题 [英] Postgresql DB datatype question

查看:178
本文介绍了Postgresql DB数据类型问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该为Source列使用什么数据类型?

我想将数据导入postgreSQL DB



源列中的数据是

0x00

0x00

0x01

0xBA

0x90



谢谢。



我的尝试:



我尝试使用bytea数据类型。当我将数据导入数据库

我将数据下载到源列中

< binary data>

<二进制数据>

<二进制数据>

<二进制数据>

<二进制数据>

What data type should I use for Source column?
I want to import data into postgreSQL DB

data in source column is
0x00
0x00
0x01
0xBA
0x90

Thanks.

What I have tried:

I tried using bytea datatype. when I import data into DB
I am getting folloowing data into source column
<binary data>
<binary data>
<binary data>
<binary data>
<binary data>

推荐答案

0x 前缀表示它是十六进制数字,你可以将它存储为整数。

要获得十六进制表示法,你可以使用PostgreSQL函数 to_hex()

有关十六进制的更多信息,请访问:十六进制 - 维基百科 [ ^ ]

在C#中,您可以将十六进制转换为整数,如下所示:
The 0x prefix means that it is a hexadecimal number, you can store it as an integer.
To get the hexadecimal notation back you can use the PostgreSQL function to_hex().
More info about hexadecimal here: Hexadecimal - Wikipedia[^]
In C# you can convert hex to integer as follows:
Convert.ToInt32("BA", 16)

在PostgreSQL中,您可以使用:

In PostgreSQL you can use:

select ('x'||lpad(the_hex_value,16,'0'))::bit(64)::bigint;

或者此函数:

CREATE OR REPLACE FUNCTION hex_to_int(hexval varchar) RETURNS integer AS


DECLARE
结果 int ;
BEGIN
EXECUTE ' SELECT x''' || hexval || ' '':: int' INTO 结果;
RETURN 结果;
END ;
DECLARE result int; BEGIN EXECUTE 'SELECT x''' || hexval || '''::int' INTO result; RETURN result; END;


LANGUAGE plpgsql IMMUTABLE STRICT;
LANGUAGE plpgsql IMMUTABLE STRICT;

请参阅: sql - 在postgres中将十六进制字符串转换为bigint - Stack Overflow [ ^ ]

postgresql - 将文本表示中的十六进制转换为十进制数 - Stack Overflow [ ^ ]

see: sql - Convert hex string to bigint in postgres - Stack Overflow[^]
postgresql - Convert hex in text representation to decimal number - Stack Overflow[^]


这篇关于Postgresql DB数据类型问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆