July 28, 2004 at 10:25 am
I am having trouble concatenating an address into one column for my query. The address is split up over several columns in the “Address” table as so:
===============================================================================
98475 S MORTIMER DR
I would like for that address to look like this in a single column:
I keep getting the following error message:
Syntax error converting the nvarchar value '5560 EPONCE-DE-LEON ' to a column of data type int.
July 28, 2004 at 10:42 am
The problem is that Street Number and ZIP are probably INTEGERS (INT).
When you use the + with INTs, that's not CONCATINATE. It means ADD.
So you need to CONVERT or CAST the Street Number and ZIP to VARCHAR or
someother character datatype and then concatenate.
For example: SELECT (CAST(Street_Number AS VARCHAR(5))) + Street_Direction + Street_Name
Test it.
-SQLBill
July 28, 2004 at 10:45 am
This will happen if you try to concatenate a string which is all numeric (even if its defined as char or varchar) to character data. You will need to CAST it as char or varchar. Your Zip is a good example:
... + City + ', ' + State + ' ' + CAST(Zip as CHAR(5))
Steve
July 28, 2004 at 10:47 am
how do i account for columns that are "null"?
There is not always a "streetsuffix", for example.
July 28, 2004 at 10:54 am
Use the ISNULL command.
SELECT ISNULL(streetsuffix, '')
that will replace any NULL streetsuffix with no space.
You will have to include the ISNULL with all columns SELECTed. (Unless they can't/don't have a NULL).
-SQLBill
July 28, 2004 at 11:03 am
For some reason, i get the following error message:
The isnull function requires 2 arguments.
July 28, 2004 at 11:50 am
SELECT ISNULL(streetsuffix, '')
The first argument is the column name that might be null, (streetsuffix in SQLBill's example).
The second argument is what you want to replace the null with, ('' in SQLBill's example), can be a space, two adjacent single quotes (replaces the null with no space, as SQLBill explained, kind of like a null value in itself), or anything else you want to put in there.
Steve
July 28, 2004 at 12:11 pm
ok...here's another question...sorry.
How would i use the CAST with the ISNULL?
SELECT ISNULL(CAST (streetnumber AS VARCHAR(5), '') ?
July 28, 2004 at 12:20 pm
select isnull(cast(streetnumber as varchar(5)),'')
July 29, 2004 at 2:21 am
To save putting isnull around all your feld names you might want to look at SET CONCAT_NULL_YIELDS_NULL.
Regards,Iain
July 29, 2004 at 7:23 am
That setting also helps set up a way to avoid extra spaces in the address where null fields occur.
Instead of adding spaces between the address parts outside of the ISNULL(CAST(... concatenate the space in first argument of the ISNULL and you won't get the extra spaces. Like so:
isnull(cast(streetnumber as varchar(5))+' ','')
That's especially important for fields like direction that are usually optional.
Also, do yourself a favor and put all this into a user defined function for easy reuse. It can take all the fields as arguments or just an unique key to the record with the address and find the parts there.
AddressString( number, prefix, street, suffix ) or AddressString( streetId )
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply