如何从表中选择*,但将其中一列转换为不同的数据类型?

时间:2021-11-18 01:38:09

I have a table with several columns. I want to do essentially:

我有一张几列的桌子。我想做的本质是:

select * from myTable and CAST(COLUMN1 as INT) where STUFF

and get all the columns as well as my first column casted as an int. Obviously this does not work.

得到所有的列以及第一列作为int类型,显然这行不通。

I know I can do the following, but its not portable whatsoever incase I need to add more columns or what have you. This is so heavy handed, and it makes my sql statements extremely long.

我知道我可以做以下的事情,但它不是可移植的,如果我需要添加更多的列或者其他什么。这是非常复杂的操作,它使我的sql语句非常长。

select (CAST COLUMN1 as INT), COLUMN2, COLUMN3, COLUMN# from TABLE where STUFF

So my question is simply, can I query all rows and columns and just cast one of them?

我的问题很简单,我能查询所有的行和列并只强制转换其中的一个吗?

If it matters, I am using DB2.

如果重要的话,我正在使用DB2。

EDIT: I just tried to run the command listing out all the columns and SQuirreL wont even execute it.

编辑:我只是尝试运行命令列出所有列,而SQuirreL甚至不会执行它。

1 个解决方案

#1


6  

If you can handle all the columns from the table, I would guess you could handle all the columns plus 1. So, do the conversion and rename the value:

如果你能处理表格中的所有列,我猜你能处理所有列加上1。因此,进行转换并重命名值:

select t.*, CAST(COLUMN1 as INT) as column1_int
from myTable t
where STUFF;

Otherwise, you have to list each column separately.

否则,必须分别列出每一列。

#1


6  

If you can handle all the columns from the table, I would guess you could handle all the columns plus 1. So, do the conversion and rename the value:

如果你能处理表格中的所有列,我猜你能处理所有列加上1。因此,进行转换并重命名值:

select t.*, CAST(COLUMN1 as INT) as column1_int
from myTable t
where STUFF;

Otherwise, you have to list each column separately.

否则,必须分别列出每一列。