I am trying to concatenate three columns from two tables Customer and Invoice, create a new column and find the new_id member whose name begins from ‘Astridgruber’ in SQl Server.
The below is causing an error.
select
concat(c.FirstName, c.LastName, i.InvoiceId) as New_Id
from
Invoice i, Customer c
where
i.[CustomerId] = c.CustomerId
where
New_Id = 'AstriGruber%'
Help will be much appreciated.
These are the tables from the Chinook Database:
Advertisement
Answer
Too long for a comment. You have a few issues:
- You should be using modern, explicit
JOINsyntax, not comma joins; - Too many
WHEREclauses;ANDthe conditions together; - You can’t use aliases in
WHEREclauses; replaceNew_Idwithconcat(c.FirstName,c.LastName,i.InvoiceId); - You should be using
LIKE, not=
All up your query should look like:
select concat(c.FirstName,c.LastName,i.InvoiceId) as New_Id from Invoice i join Customer c on i.CustomerId = c.CustomerId where concat(c.FirstName,c.LastName,i.InvoiceId) LIKE 'AstriGruber%'
