Click here to Skip to main content
15,909,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, Good Day!

I would like to ask some help regarding my problem.

This is my query:

SQL
SELECT name [Name],
       allowance [Allowance]
FROM Table1

UNION ALL

SELECT name [Name],
       '' [Allowance] <--
FROM Table2


There's no allowance field in the Table2. I want it to have a BLANK data.
But an error occurred,
C#
"Error converting data type varchar to numeric."


I know that it is because, allowance field in Table1 has a numeric datatype and I union it with a ''. How will I do it? I want it to have a Blank Data or Space. NOT 0, or NULL.

I don't want to use

SQL
SELECT name [Name],
       allowance [Allowance]
FROM Table1

UNION ALL

SELECT name [Name],
       0 [Allowance] <-- 0
FROM Table2


OR NULL Value

SQL
SELECT name [Name],
       allowance [Allowance]
FROM Table1

UNION ALL

SELECT name [Name],
       NULL [Allowance] <-- NULL
FROM Table2




Please Help.
Thank you so much.
Posted
Comments
Maciej Los 25-Jul-13 1:58am    
Why?
berrymaria 25-Jul-13 2:13am    
Above query shown is just an example. And I ask since I don't know how will I do it. Is my question not clear enough?
Maciej Los 25-Jul-13 2:21am    
No, it's not clear enough. I'm asking you because i do really not understand why do you want to get empty string (not NULL, not 0) as a Allowance. If Allowance field is a numeric field, why do you want to convert it as a varchar type? What kind of practical application it have?

Hi,

you can try it by converting data type in your select statement..

create table table1
(
name varchar(10),
allowance int
)


create table table2
(
name varchar(10),
)

insert into table1 values('amit',1000),('arun',2000),('nitin',1000)
insert into table1 values('ajay'),('kamal'),('kunal')


SELECT Name,Convert(varchar,allowance) as Allowance
FROM Table1
UNION ALL
SELECT Name ,'' as Allowance
FROM Table2


Output:

Name	Allowance
amit	1000
arun	2000
nitin	1000
ajay	
kamal	
kunal	


And in case of above table structure.. your query is also not giving any error..

SQL
SELECT Name [Name],
       allowance [Allowance]
FROM Table1

UNION ALL

SELECT name [Name],
       '' [Allowance]
FROM Table2


OutPut:

Name	Allowance
amit	1000
arun	2000
nitin	1000
ajay	0
kamal	0
kunal	0
 
Share this answer
 
Hi,

You need to convert the field in Allowance in Table1 to varchar type.

SQL
SELECT name [Name],
       Convert(varchar(100),allowance) as [Allowance]
FROM Table1
UNION ALL
SELECT name [Name],
      '' as Allowance
FROM Table2


Since allowance field is now a varchar, joining it with a table that has no varchar allowance will actually show a space.
 
Share this answer
 
Comments
Raja Sekhar S 25-Jul-13 2:15am    
Agree.. +5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900