sql中character varying转换integer
时间: 2023-09-02 11:10:05 浏览: 37
如果你想将一个 `character varying` 类型的列(假设名为 `col1`)转换为 `integer` 类型,可以使用 `CAST` 函数来实现。具体的 SQL 语句如下:
```sql
SELECT CAST(col1 AS INTEGER) FROM 表名;
```
注意,在执行上述 SQL 语句之前,需要确保 `col1` 列中的所有值都是可以转换为整数的,否则会导致转换失败并抛出错误。
相关问题
ERROR: operator does not exist: character varying = integer
This error occurs when you try to compare a string (character varying) with an integer using the equality operator (=) in a SQL query. The equality operator only works between two values of the same data type, so you cannot compare a string with an integer directly.
To resolve this error, you need to make sure that you are comparing values of the same data type. You can use the CAST or CONVERT function to convert one data type to another before comparing them. For example, if you have a string column named "mycolumn" and an integer value 123, you can convert the integer value to a string and compare it with the string column as follows:
SELECT * FROM mytable WHERE mycolumn = CAST(123 AS VARCHAR);
Alternatively, you can convert the string column to an integer and compare it with the integer value:
SELECT * FROM mytable WHERE CAST(mycolumn AS INTEGER) = 123;
Make sure to choose the appropriate data type conversion function based on the data types of the values you are comparing.
Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: character varying = integer
这个错误通常是因为在 PostgreSQL 数据库中,你尝试使用 "varchar" 类型的字段和 "integer" 类型的字段进行比较,而这两种类型的数据是不能直接比较的。
你可以尝试将其中一个字段的类型转换为与另一个字段相同的类型,或者使用一个转换函数来将它们转换为相同的类型,例如:
```sql
SELECT * FROM your_table WHERE varchar_field = CAST(integer_field AS varchar);
```
或者:
```sql
SELECT * FROM your_table WHERE varchar_field = to_char(integer_field, '999');
```
这两种方法都将 "integer" 类型的字段转换为 "varchar" 类型的字段,以便进行比较。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)