2

In Sql, if I want to fetch all columns from a table then I would write select * from <table name>. However let say there is a column called XYZ and type is numeric. However I want to fetch all columns but change that XYZ column to sting, then how should one proceed?

Any help will be highly appreciated.

Bogaso
  • 2,838
  • 3
  • 24
  • 54
  • 2
    List out all columns. Do the conversion explicitly. – Gordon Linoff Feb 12 '20 at 22:10
  • 1
    `SELECT *` should almost never be used in production code; it is fine for "developer" queries and troubleshooting, but for day to day operations SELECTs should always have the fields retrieved explicitly listed, and preferably unused fields omitted. See [this](https://stackoverflow.com/a/3639964/4104224) for details – Uueerdo Feb 12 '20 at 22:29

3 Answers3

2

Select the other columns individually, and CAST(xyz AS NVARCHAR)

SELECT foo, bar, CAST(xyz AS NVARCHAR)
FROM myTable
Tanner
  • 2,232
  • 13
  • 22
0

I don't think there is a way to do it without either specifying all the columns or duplicating the one you want to convert.

SELECT *, CAST(XYZ as varchar) as XYZ_String
FROM <table>
Jason Wadsworth
  • 8,059
  • 19
  • 32
  • With this I am getting below error '(DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=*;select ;NULL, DRIVER=4.19.26)'. However if I remove '*' then result is fine – Bogaso Feb 12 '20 at 22:34
  • I don't know much about DB2. The question was tagged as SQL Server, and I'm pretty confident this would work there. – Jason Wadsworth Feb 12 '20 at 22:35
0
SELECT CAST(column AS NVARCHAR(25)) AS xyz_string 
FROM Table
Zanyar Jalal
  • 1,407
  • 12
  • 29