Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

db2

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.

like image 229
R4F6 Avatar asked Aug 13 '14 22:08

R4F6


1 Answers

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.

like image 119
Gordon Linoff Avatar answered Sep 17 '22 17:09

Gordon Linoff