String concatenation not working as expected in SELECT statement - sql

I'm using MS Access to work with an SQL Server database through an ODBC connection on Windows 7.
The ultimate goal is to append a string literal to one of the fields for a subset of rows. Initially, though, I'm just trying to do a SELECT so I can make sure I have everything correct. I'm having trouble trying to append a string literal to the field.
The below simple SQL works well.
SELECT Name FROM Customers WHERE CustomerType = 1;
Next step was to try and modify the displayed name slightly.
SELECT Name, 'PREFIX' & Name FROM Customers WHERE CustomerType = 1;
The above also worked. Then I tried the following.
SELECT Name, Name & 'SUFFIX' FROM Customers WHERE CustomerType = 1;
This does not work. The output shows just the Name field with nothing appended. I looked around and found SQL Server seems to support CONCATENATE('a' + 'b'). I tried using that in the query but it failed with an error from Access about there not being a CONCATENATE function.
I also tried double quotes instead and + instead of &.
Seems odd that the prefix case worked and the suffix case did not.
The eventual goal, again, would be to construct something like the below.
UPDATE Customers SET Name = Name & 'SUFFIX' WHERE CustomerType = 1;
This would append a suffx to a text field for a subset of rows in the table.
Any ideas?

In SQL Server, & is for binary masks. You want the + operator
UPDATE Customers
SET Name = Name + 'SUFFIX'
WHERE CustomerType = 1;
I don't know where you got CONCATENATE from - There is a CONCAT function in SQL 2012, but nothing like that in any other version

My impression is you have an Access query with Customers as an ODBC link to a SQL Server table. If that is correct, either of these 2 query versions should work.
SELECT
[Name],
[Name] & 'SUFFIX'
FROM Customers
WHERE CustomerType = 1;
SELECT
c.Name,
c.Name & 'SUFFIX'
FROM Customers AS c
WHERE c.CustomerType = 1;
The reason for this suggestion is that Name is a reserved word. One thing that makes reserved words frustrating is that you don't know when they will bite you. A reserved word might not cause trouble in one context but cause the same SQL statement to fail in another context. Enclosing the name in square brackets or qualifying the name with an alias (or the table name) avoids confusing Access' db engine.
Try this for your UPDATE.
UPDATE Customers
SET [Name] = [Name] & 'SUFFIX'
WHERE CustomerType = 1;

Related

cannot insert data into a new column

Hi I'm trying to write code for an email to auto update in an interactive grid in Oracle APEX. It keeps saying that my column identifier needs to be defined however it is defined by each table.
select
t.email into O.email
from Master_Tech_Email t, MASTER_COURSE_OWNER O
where O.Technology = t.Technology;
I think a misunderstanding into clause.
If you want to update email column in MASTER_COURSE_OWNER table based on Technology column try this
(Because of tags include sql and oracle I am sharing both of it)
For Oracle:
UPDATE MASTER_COURSE_OWNER SET EMail = (SELECT Email FROM Master_Tech_Email WHERE Technology = MASTER_COURSE_OWNER.Technology AND ROWNUM = 1)
I am putting a live sample for Oracle here
For SQL:
UPDATE MASTER_COURSE_OWNER SET EMail = (SELECT TOP(1) Email FROM Master_Tech_Email WHERE Technology = MASTER_COURSE_OWNER.Technology)
I am putting a live sample for SQL here

Mass criteria query

I have an Access query. I need a mass 'like' criteria.
Normally it's "Like "Apple" or "Orange"". However, I have over 400 words.
How do make this happen without manually typing in a Like formula by hand with the 400 words?
Is there a way to do this by referencing a table or form?
As for "referencing a table", you can actually do a join using LIKE instead of =.
SELECT
t.*
FROM
target t
INNER JOIN
SearchList s
ON t.name LIKE '%' + s.term '%'
In this case, SearchList is a table you create just for this task with just 1 column containing the terms you want to search for.
You can use in clause , sub query to get result, the query in Access can have 64000 characters so your 400 words X 5 chars in each approximate , 2000 chars + 50 chars SQL statement, should be OK,
Select * from tblVitalInfo where ObjectName in ('Apple','Banana','Pitch')
But I will recommend following better way of doing same,
Assume the table tblVitalInfo(ID, ObjectName) , and searching ObjectName
I would create a table tblWordList(ID autonumber primary key, SearchString Text(200)) . Write Insert statement to add all the words to search e.g. Insert into tblWordList(SearchString) values('Apple') ; execute these queries in Access. OR you can open the table to add data , easier than insert statement.
Run the following query
Select * from tblVitalInfo where ObjectName in (Select SearchString tblWordList)

With as in Oracle SQL

I would like to know if is it possible to use the clause "with as" with a variable and/or in a block begin/end.
My code is
WITH EDGE_TMP
AS
(select edge.node_beg_id,edge.node_end_id,prg_massif.longueur,prg_massif.lgvideoupartage,prg_massif.lgsanscable from prg_massif
INNER JOIN edge on prg_massif.asset_id=edge.asset_id
where prg_massif.lgvideoupartage LIKE '1' OR prg_massif.lgsanscable LIKE '1')
,
journey (TO_TOWN, STEPS,DISTANCE,WAY)
AS
(SELECT DISTINCT node_beg_id, 0, 0, CAST(&&node_begin AS VARCHAR2(2000))
FROM EDGE_TMP
WHERE node_beg_id = &&node_begin
UNION ALL
SELECT node_end_id, journey.STEPS + 1
, journey.DISTANCE + EDGE_TMP.longueur,
CONCAT(CONCAT(journey.WAY,';'), EDGE_TMP.node_end_id
)
It create a string as output separated by a ; but i need to get it back as variable or table do you know how? I used a concat to retrieve data in a big string. Can i use a table to insert data
,
A need to use the result to proceed more treatment.
Thank you,
mat
No, WITH is a part of an SQL statement only. But if you describe why you need it in pl/sql, we'll can advice you something.
Edit: if you have SQL statement which produces result you need, you can assign it's value to pl/sql variable. There are several methods to do this, simpliest is to use SELECT INTO statement (add INTO variable clause into your select).
You can use WITH clause as a part of SELECT INTO statement (at least in not-too-very-old Oracle versions).

If you set a field name as an Alias, can you utilize that name within SQL code?

I'm a beginner in using SQL and I'm just messing around to learn. If I was trying to combine a First Name field and a Last Name field into a Name field, can I use that alias in my SQL code?
For example, (using the Northwind Access sample DB) I was trying to pull the Company and Address of all the Names that start with "J" using an alias for the name field
Example code below
SELECT Company, Address, [First Name] + " " + [Last Name] AS Name
FROM Customers
WHERE Name LIKE "J%";
In Access this is popping up a parameter box and not returning results even though I know there are names that start with J.
With few exceptions, you cannot use an alias defined in the SELECT elsewhere in the query at the same level. So, you cannot use the alias again in the SELECT or WHERE, for instance.
But you can use a subquery:
SELECT c.*
FROM (SELECT Company, Address, [First Name] & " " & [Last Name] AS Name
FROM Customers
) as c
WHERE Name LIKE "J*";
Notice that I changed the string concatenation operator from + to & and the wildcard in the LIKE pattern from '%' to '*'. These changes are to make the query compatible with MS Access.
According to this you can only output alias names in Access. Can't use them as part of your query. Didn't catch who posted this link earlier for credit.

SQL statement for a join in dB2

The following is my sql statement for a join in dB2.
select name, address, bloodgroup
from user_tb, health_tb
where user_tb.id = health_tb.id;
I am getting the following error:
"health_tb.id" is not valid in the context where it is used..
SQLCODE=-206, SQLSTATE=42703, DRIVER=4.12.79
I understand that one reason why I could be getting this error is because id may not exist in health_tb, but that is not the case. I hope someone can advise. Thank you.
First, you should learn to use modern join syntax, although this has nothing to do with your problem:
select name, address, bloodgroup
from user_tb join
health_tb
on user_tb.id = health_tb.id;
A simple search on Google pointed me to the documentation for this error. One of the first things it mentions is:
Possible reasons for this error include:
The specified column is not a column of any of the source or target
tables or views of the statement.
In a SELECT or DELETE statement, the specified column is not a column of any of the tables or views that are identified in a FROM
clause in the statement.
A column list of an SQL data change statement specified the name of a column of the target table or view of the statement.
I suspect that the id column is really called something like user_id. The working query might look like:
select name, address, bloodgroup
from user_tb join
health_tb
on user_tb.id = health_tb.user_id;
1) check if the id column in both tables have the same data type
2) check if there is any trailing space in the column name
select '<' || column_name || '>' from user_tab_columns
where tbname = 'health_tb'
If the id columns are defined as different types, that could be a problem.