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

R4F6 picture R4F6 · Aug 14, 2014 · Viewed 11.5k times · Source

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.

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.

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.

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

Answer

Gordon Linoff picture Gordon Linoff · Aug 14, 2014

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:

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

Otherwise, you have to list each column separately.