Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi my table has separate columns for house no,city,state.But i want adress as houseno,city,state problem is that is i have space entered in these fields i get ,,,
list so i want to test for space.
Posted
Comments
Sandeep Mewara 29-Jun-12 6:56am    
Not clear. Are you saying your query result has spaces missing in output? Or you want to ignore the spaces?.

Please provide sample example and the query you are using.
Pratika05 29-Jun-12 7:05am    
select
(IsNull(House_Name_No,'') +','+IsNull(City,'')+','+IsNull(State,'')
from emptable

this is my query if a record has no values for these i got ,,, but i want a space not ,,,

Hello dude may be helpful this code.


SQL
declare @house  nvarchar(200)
declare @city  nvarchar(200)
declare @state nvarchar(200)


set @house='50';
set @city='Himaat nagar';
set @state='Delhi';

select case when @house !='' then  @house else '' end  +  (case when @city !='' then case when @house!='' then ' , '+@city else @city end else ''  end)  +

 (case when @state !='' then case when @city!='' then ' , '+@state else @state end else ''end)



Use this code Where @house replace your table column house ,@city replace your table column City and @state replace your table column state and add and of query as (from tablename )


It is confirm proper work as i know your problem

Thanks
 
Share this answer
 
v4
Comments
Pratika05 29-Jun-12 7:42am    
pls expalin it more
Pratika05 29-Jun-12 8:12am    
thks it workss
AshishChaudha 30-Jun-12 5:52am    
my 5!
[updated the fields in else part of case..if forgot to replace in else part]try this..

I am not good in sql but I tried this and according to your requirement its perfectly working..

SQL
select case when isnull(ltrim(rtrim(houseno)),'') + ',' + isnull(ltrim(rtrim(city)),'') + ',' +  isnull(ltrim(rtrim(state)),'') =',,' then ' ' else isnull(ltrim(rtrim(houseno)),'') + ',' + isnull(ltrim(rtrim(city)),'') + ',' +  isnull(ltrim(rtrim(state)),'') end as address from [tablename] 


make it answer if you get your solution.

Thanks
 
Share this answer
 
v2
Comments
Pratika05 29-Jun-12 7:00am    
thanks yupiii i got it
AshishChaudha 29-Jun-12 7:01am    
no ratings??
[Improved my solution........]May this query helps you..

SQL
select isnull(ltrim(rtrim(houseno)),'')+','+ isnull(ltrim(rtrim(city)),'') +','+ isnull(ltrim(rtrim(state)),'') as address from tablename



make it answer if you get your solution.


Thanks
 
Share this answer
 
v2
Comments
Pratika05 29-Jun-12 6:37am    
no this does not solve my problem i am getting ,,, if fields are blank
AshishChaudha 29-Jun-12 6:41am    
What you want if all fields are blank??
Pratika05 29-Jun-12 6:52am    
if all fields are blank i need a space or nothng but not this ,,,
You can use
select isnull(houseno,'')+' '+isnull(city,'')+' '+isnull(state,'') as address
 
Share this answer
 
Comments
Pratika05 29-Jun-12 6:36am    
no i want comma in between each part house no,city,state
Try this

SQL
SELECT (CASE WHEN LTRIM(RTRIM(HOUSENO)) IS NOT NULL THAN HOUSENO + ',' END
        CASE WHEN LTRIM(RTRIM(CITY)) IS NOT NULL THAN CITY + ',' END
        CASE WHEN LTRIM(RTRIM(STATE)) IS NOT NULL THAN STATE END ) AS ADDRESS
        FROM TABLENAME
 
Share this answer
 
v2
Comments
Pratika05 29-Jun-12 7:38am    
incorrect syntax
bhagirathimfs 30-Jun-12 8:24am    
ignore this answer sry for wrong answer :(

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