简体   繁体   English

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

[英]How can I select * from table, but cast one of the columns to a different datatype?

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. 并获取所有列以及我的第一列作为int。 Obviously this does not work. 显然这不起作用。

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甚至不执行它。

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. 否则,您必须单独列出每列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM