1

I'd like to concatenate three columns - street, street number and city to one column "adress". The strange thing is that I cannot do it for some reason.

This is what I have tried so far:

SELECT      street,
            street_num,
            city,
            isnull(street,'') + '' + isnull(street_num,'') + '' + isnull(city,'') AS tst1, --doesnt work
            concat(isnull(street,''),' ',isnull(street_num,''), ' ', isnull(city,'')) AS tst2, --doesnt work
            (street_num + ' ' + street) AS tst3, --does work
            (street_num + ' ' + city) AS tst4, --does work
            (city + ' ' + street) AS tst5 --doesnt work
FROM        [DB].[dbo].[adresses]

Note that + or concat doesnt work, it only shows the first column, street in these cases. However, if I start with street number and add street or city, it does work. But if I try to add third column, it is not shown.

If it helps, the table was pulled from Oracle by OPENQUERY and the table structure is as follows:

street VARCHAR(100), null
street_num VARCHAR(50), null
city VARCHAR(100), null

I am on MSSQL 2014.

EDIT

As asked in the comments, i cant show the data as I am dealing with addresses of our customers. Below are two dummy records plus expected result (adress) as example:

street            | street_num | city   | adress
--------------------------------------------------------------------
avenida pino alto | 45         | avila  | avenida pino alto 45 avila  
rue de abaixo     | 86         | madrid | rue de abaixo 86 madrid        

Furthermore, If i copy the records and do something like this, it works of course.

SELECT 'avenida pino alto' + ' ' + '45' + ' ' + 'avila'
10
  • Can you post some sample data? Commented Apr 4, 2017 at 8:39
  • Its addresses of our clients, so I cant :(. Commented Apr 4, 2017 at 8:40
  • 1
    you could extract some of them (two rows) and change manually some values... Apparently your query is correct (as far as I know) Commented Apr 4, 2017 at 8:42
  • 1
    @HonzaB:please paste some dummy sample data and expected rsult Commented Apr 4, 2017 at 8:44
  • try isnull(CAST(street as varchar) ,'')+' '+isnull(CAST(street_num as varchar) ,'')+' '+isnull(CAST(cityas varchar) ,'') Commented Apr 4, 2017 at 8:48

3 Answers 3

1

Based on comments, it seems that your street column contains some char/data that causes problems.

I have no idea what it could be, but you can try to find out like this:

select top 10
    street,
    len(street) as streetCharLen,
    cast(street as varbinary(500)) as streetBytes
from [DB].[dbo].[adresses]

Then compare what the different columns tell you.

Here's a quick sample:

declare @t table (
    id int,
    thestring varchar(50)
)

insert into @t values (1, 'test')

select thestring,
    len(thestring) as slen,
    cast(thestring as varbinary(100)) as sbytes
from @t

If in this sample, the slen is not 4, or the sbytes contains something that does not map back to one of the characters that I see when selecting, then something is wrong with the string.

Sign up to request clarification or add additional context in comments.

1 Comment

All right, so there were some funny addresses. But that actually led me back to Oracle, where were some strange things happening with these columns. Once I cleaned it, it works like a charm. Thank you!
0

Use convert(varchar,[exp]):

SELECT      street,
            street_num,
            city,
            isnull(convert(varchar,street),'') + '' + isnull(convert(varchar,street_num),'') + '' + isnull(convert(varchar,city),'') AS tst1

FROM        [DB].[dbo].[adresses]

Comments

0

Try as follows.

    select isnull((convert varchar(250),street),'')+isnull((convert varchar(250),[street number]),'')+
    isnull((convert varchar(250),[city]),'') as 'Adress'
    from .......(your query)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.