Sqlite sql查询数据数据类型转换

一、SQL数据类型转换代码

SELECT CAST('3.02' as decimal) 
-- they aren't real decimals in sqlite, though. beware floats. 

二、sqlite数据类型转换参考

Affinity of type-name

Conversion Processing   转换处理

NONE

没有

Casting a value to a type-name with no affinity causes the value to be converted into a BLOB. Casting to a BLOB consists of first casting the value to TEXT in the encoding of the database connection, then interpreting the resulting byte sequence as a BLOB instead of as TEXT.

将一个值转换为一个 类型名称 没有关联将导致值转换为BLOB。强制转换为BLOB包括首先将值强制转换为 编码 然后将结果字节序列解释为BLOB而不是文本。

TEXT

文本

To cast a BLOB value to TEXT, the sequence of bytes that make up the BLOB is interpreted as text encoded using the database encoding.

Casting an INTEGER or REAL value into TEXT renders the value as if via sqlite3_snprintf() except that the resulting TEXT uses the encoding of the database connection.

若要将BLOB值强制转换为文本,组成BLOB的字节序列将被解释为使用数据库编码的文本编码。将整数值或实数值强制转换为文本将使该值如同通过 sqlite3\u snprintf() 除了结果文本使用 编码 数据库连接的名称。

REAL

When casting a BLOB value to a REAL, the value is first converted to TEXT.

When casting a TEXT value to REAL, the longest possible prefix of the value that can be interpreted as a real number is extracted from the TEXT value and the remainder ignored. Any leading spaces in the TEXT value are ignored when converging from TEXT to REAL. If there is no prefix that can be interpreted as a real number, the result of the conversion is 0.0.

将BLOB值强制转换为实数时,首先将该值转换为文本。

将文本值强制转换为实数时,将从文本值中提取可解释为实数的值的最长前缀,并忽略其余部分。从文本聚合到实数时,将忽略文本值中的任何前导空格。如果没有可以解释为实数的前缀,则转换的结果为0.0。

INTEGER

整数

When casting a BLOB value to INTEGER, the value is first converted to TEXT.

When casting a TEXT value to INTEGER, the longest possible prefix of the value that can be interpreted as an integer number is extracted from the TEXT value and the remainder ignored. Any leading spaces in the TEXT value when converting from TEXT to INTEGER are ignored. If there is no prefix that can be interpreted as an integer number, the result of the conversion is 0. If the prefix integer is greater than +9223372036854775807 then the result of the cast is exactly +9223372036854775807. Similarly, if the prefix integer is less than -9223372036854775808 then the result of the cast is exactly -9223372036854775808.

When casting to INTEGER, if the text looks like a floating point value with an exponent, the exponent will be ignored because it is no part of the integer prefix. For example, "(CAST '123e+5' AS INTEGER)" results in 123, not in 12300000.

The CAST operator understands decimal integers only — conversion of hexadecimal integers stops at the "x" in the "0x" prefix of the hexadecimal integer string and thus result of the CAST is always zero.

A cast of a REAL value into an INTEGER results in the integer between the REAL value and zero that is closest to the REAL value. If a REAL is greater than the greatest possible signed integer (+9223372036854775807) then the result is the greatest possible signed integer and if the REAL is less than the least possible signed integer (-9223372036854775808) then the result is the least possible signed integer.

Prior to SQLite version 3.8.2 (2013-12-06), casting a REAL value greater than +9223372036854775807.0 into an integer resulted in the most negative integer, -9223372036854775808. This behavior was meant to emulate the behavior of x86/x64 hardware when doing the equivalent cast.

将BLOB值强制转换为整数时,首先将该值转换为文本。

将文本值强制转换为整数时,将从文本值中提取可解释为整数的值的最长前缀,并忽略余数。从文本转换为整数时,文本值中的任何前导空格都将被忽略。如果没有可解释为整数的前缀,则转换结果为0。如果前缀整数大于+9223372036854775807,则强制转换的结果正好是+9223372036854775807。类似地,如果前缀整数小于-9223372036854775808,则强制转换的结果正好是-9223372036854775808。

强制转换为整数时,如果文本看起来像带指数的浮点值,则指数将被忽略,因为它不是整数前缀的一部分。例如,“(将'123e+5'转换为整数)”结果是123,而不是12300000。

CAST运算符只理解十进制整数-的转换 十六进制整数 在十六进制整数字符串的“0x”前缀中的“x”处停止,因此强制转换的结果始终为零。

将实值转换为整数将导致实值和零之间的整数最接近实值。如果实数大于最大可能有符号整数(+9223372036854775807),则结果为最大可能有符号整数,如果实数小于最小可能有符号整数(-9223372036854775808),则结果为最小可能有符号整数。

在SQLite版本3.8.2(2013-12-06)之前,将大于+9223372036854775807.0的实值强制转换为整数会导致最负的整数-9223372036854775808。此行为旨在模拟执行等效转换时x86/x64硬件的行为。

NUMERIC

数字

Casting a TEXT or BLOB value into NUMERIC yields either an INTEGER or a REAL result. If the input text looks like an integer (there is no decimal point nor exponent) and the value is small enough to fit in a 64-bit signed integer, then the result will be INTEGER. Input text that looks like floating point (there is a decimal point and/or an exponent) and the text describes a value that can be losslessly converted back and forth between IEEE 754 64-bit float and a 51-bit signed integer, then the result is INTEGER. (In the previous sentence, a 51-bit integer is specified since that is one bit less than the length of the mantissa of an IEEE 754 64-bit float and thus provides a 1-bit of margin for the text-to-float conversion operation.) Any text input that describes a value outside the range of a 64-bit signed integer yields a REAL result.

Casting a REAL or INTEGER value to NUMERIC is a no-op, even if a real value could be losslessly converted to an integer.

将文本或BLOB值转换为数字将产生整数或实数结果。如果输入文本看起来像一个整数(没有小数点或指数),并且该值足够小,可以容纳64位有符号整数,那么结果将是整数。输入看起来像浮点的文本(有一个小数点和/或一个指数),该文本描述了一个值,该值可以在IEEE 754 64位浮点和51位有符号整数之间无损地来回转换,然后结果是整数(在前面的句子中,指定了一个51位整数,因为它比ieee754 64位浮点的尾数长度少一位,因此为文本到浮点的转换操作提供了1位的余量。)任何描述64位有符号整数范围之外的值的文本输入都会产生一个实际结果。

将实数或整数值转换为数值是不可操作的,即使实数可以无损地转换为整数。

decimal

十进制

 

 

Sqlite sql查询数据数据类型转换_第1张图片

你可能感兴趣的:(数据库,sqlite)