SQL 如何在sqlite3中输入类型转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32953639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:05:22  来源:igfitidea点击:

How to type cast in sqlite3

sqlcastingsqlite

提问by Abundance

I ran sqlite3 on my command prompt and ran some basic SQL commands.

我在命令提示符下运行了 sqlite3 并运行了一些基本的 SQL 命令。

user@comp:~$ sqlite3  
SQLite version 3.8.2 2013-12-06 14:53:30  
Enter ".help" for instructions  
Enter SQL statements terminated with a ";"  
sqlite> CREATE TABLE A (a int, b text, c float);  
sqlite> INSERT INTO A(a,b,c) VALUES (1, '2', 3);  
sqlite> SELECT b::int+2 FROM A;  

All of the lines work except for the last one, which gives the error: `

除了最后一行之外,所有行都有效,这给出了错误:`

Error: unrecognized token: ":"`

错误:无法识别的令牌:“:”`

I was reading from this question (Double colon (::) notation in SQL) that the double colon notation is a type-cast for SQL. Am I doing the operation wrong?

我从这个问题(SQL中的双冒号(::) 表示法)中读到双冒号表示法是 SQL 的类型转换。我操作错了吗?

回答by Lukasz Szozda

The ::syntax is PostgreSQL specific. You could use ANSI standard instead:

::语法是PostgreSQL的具体。您可以改用 ANSI 标准:

SELECT CAST(b AS INT) + 2 AS alias
FROM A

SqlFiddleDemo

SqlFiddleDemo