Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add additional column with default value in SQL query which actually doesn't exist in the table

Tags:

sql-server

I have two database tables which I use to feed the same jquery DataTable. The problem is that the data is very similar (I show information for individual persons and companies) but it differs in that that the individual person has Personal Identification Number(PIN) and the Company has (IBAN). So I need two columns in my jquery DataTable one named PIN and one named IBAN and each row will have only one of the both field filled and the other will be empty.

So my problem is, due to the fact how I construct my JSON the most easiest way would be to add some fake column to each query like, for the Individual Perosn query :

SELECT Name, City, PIN, IBAN(this is the fake column which should be null) FROM Persons

and for the Company query :

SELECT Name, City, PIN(this time this is a fake column which should be null), IBAN FROM Companies

The problem is that I don't know if this is possible, and if it is, how to do it. I have the option to do this later after I fetch the records but like this it would be very easy.

like image 965
Leron_says_get_back_Monica Avatar asked Nov 25 '25 03:11

Leron_says_get_back_Monica


2 Answers

You can select a fake column quite easily. Just create the value and name it.

SELECT Column1, Column2, '' AS FakeColumn, Column3 FROM MyTable

Or with NULL:

SELECT Column1, Column2, NULL AS FakeColumn, Column3 FROM MyTable
like image 124
DavidG Avatar answered Nov 28 '25 06:11

DavidG


I think you can do it like this

Declare @IBAN nvarchar(10)=NULL
SELECT Name, City, PIN, @IBAN FROM Persons
like image 36
Nitin Varpe Avatar answered Nov 28 '25 05:11

Nitin Varpe