I need to join on a (new for me) where condition. In one table it is all in one field, in the second table it is split and I need to combine it. This syntax just gives me the generic, 'incorrect syntax near the keyword 'on'' which is not very
helpful to me. What do I need to do to get this as a functioning query?
Select pi.fullname, si.address, si.phone
From saleinfo si
Inner join personelinfo pi
ON si.[FirstName]+' '+si.[LastName] = pi.[fullname]'
Assuming this is correct, do you have a setting on that allows for schema information hiding?
It is best practice in SQL to list the master database beforehand in what is called the BUS principle. Namely, in case you get hit by a bus, another developer should be able to come along and know what they are looking at. Therefore, in your scripts you
should script out a USE <database> at the top of your query.
Part of the reason also is that are you sure that the master database you are calling the tables from knows that concatenating the columns together will match the same result for the right table column you are joining? So you should be doing a little data-mining
to ensure the integrity of the data now and in the future to match.
Without really knowing what the data looks like, I can't be very specific, but definitely as RyanAB says get rid of that hyphen ( ' ) at the end.
USE <databasename>
GO
Select pi.fullname, si.address, si.phone
From dbo.saleinfo si
Inner join dbo.personelinfo pi ON si.[personID] + '' + [LastName] = pi.[fullname]
Note, the reason for verifying your data and the datasets you are using is because what if there was a discrepancy in the saleinfo data? Worse, what if you are referencing a table from another database or different schema than the master (dbo is the default
un-named SCHEMA) Then the entire script will either not run as you currently have or it will return an empty set!
Hope this steers you in the right direction.
-
Edited by
Conquistador0
2 hours 45 minutes ago