How do I merge two columns into one new one? - sql

I'm trying to merge the first name and surname columns of this table into a new column but I'm not getting it to work. I've tried a few variations but I get very similar errors. What am I doing wrong?
SELECT *,
CONCAT(full_dataset.First_name, " ", full_dataset.Surname) AS Full_name
FROM full_dataset
gives me
ERROR: column full_dataset.first_name does not exist
LINE 2: CONCAT(full_dataset.First_name, " ", full_dataset.Surname) A...
^
HINT: Perhaps you meant to reference the column "full_dataset.First_name".
SQL state: 42703
Character: 18
whilst
SELECT *,
CONCAT("full_dataset.First_name", " ", "full_dataset.Surname") AS Full_name
FROM full_dataset
gives me
ERROR: column "full_dataset.First_name" does not exist
LINE 2: CONCAT("full_dataset.First_name", " ", "full_dataset.Surname...
^
SQL state: 42703
Character: 18
I've also tried ("First_name", " ", "Surname") amongst other variants
This is a section of my table (edited to remove private info)
Thank you very much for any help, I appreciate it.

Don't Use "" Double Quote inside Concat Function Used ' Single Quote
Try Following
SELECT *,
CONCAT(full_dataset.First_name, ' ' , full_dataset.First_name) AS Full_name
FROM full_dataset

Related

mismatched input 'GROUP' expecting <EOF> SQL

I am running a process on Spark which uses SQL for the most part. In one of the workflows I am getting the following error:
mismatched input 'GROUP' expecting
spark.sql("SELECT state, AVG(gestation_weeks) "
"FROM natality "
"WHERE state is not null "
"HAVING AVG(gestation_weeks) > (SELECT AVG(gestation_weeks) FROM natality) "
"GROUP BY state").show()
I cannot figure out what the error is for the life of me
I've tried checking for comma errors or unexpected brackets but that doesn't seem to be the issue
The SQL constructs should appear in the following order:
SELECT
FROM
WHERE
GROUP BY **
HAVING **
ORDER BY

How do I use a comma delimited string as query criteria on a numeric field

I've built a function that creates a comma delimited string from multiple selections in my list box. When I try to use this as the Where clause in my sql (in VBA) I get a 'type mismatch' error - because the field I'm using with the criteria is numeric. How do I resolve this?
example:
sql = "INSERT INTO tblItemsLibrary ( Constr, Description) ...
"WHERE (((tblItemsLibrary1.ItemsLibID) In (" & strSelectedRecords & ")));"
In the above example strSelectedRecords is coming in as "17,11,28" where the field is a long.
You can use LIKE:
INSERT INTO tblItemsLibrary ( Constr, Description)
...
WHERE "," & tblItemsLibrary1.ItemsLibID) & "," LIKE "*," & strSelectedRecords & ",*"
Actually you might want to adjust strSelectedRecords to start and end with *, and ,*.
If you are using SQL Server 2016 or above, the function string_split can be used:
SELECT *
FROM tblItemsLibrary
WHERE tblItemsLibrary1.ItemsLibID IN (select * from STRING_SPLIT(#numlist, ','))
Replace #numlist with your string 17,11,28
Another method is to construct dynamic sql and pass that on top of base sql statement:
EXECUTE('SELECT * FROM tblItemsLibrary1 WHERE ItemsLibID IN
('+#numlist+')')
Your SQL expression will result in:
INSERT INTO tblItemsLibrary ( Constr, Description)
...
WHERE (((tblItemsLibrary1.ItemsLibID) In (17,11,28)));
which is correct if ItemsLibID is numeric.
Thus, your error is probably caused by the values you try to insert.

Append R variable inside SQLDF's SQL code

Tried to search for an answer using "SQLDF append in code", but I couldnt find any answer.
Basically, I'd like to be able to write something like that :
divisor<-4
result<-sqldf("SELECT id, some_number/ " . divisor . " FROM my_table")
but I can't find a way to have divisor included in the SQL statement : Error: unexpected symbol in "result<-sqldf("SELECT id, some_number/ " ."
I tried having &, + and nothing instead of the dots ., but I can't find anything that works.
I also tried "SELECT id, some_number/divisor FROM my_table", without any " " around divisor, but I get error in statement: no such column: divisor
Thanks.
You can use
result<-sqldf(sprintf("SELECT id, some_number/%d FROM my_table", divisor))
Hope this helps

Syntax Error in Join Operation in MS-Access when splitting and comparing records

Above error message occurs with this statement:
SELECT f.fullname INTO SummaryJudgment_FinalForgottenWithMiddle
FROM (
(SELECT Left([aname],InStr(1,[aname],",")-1)) As lastname FROM
SummaryJudgment_FinalForgotten) & " " & (SELECT
RIGHT([aname],InStr(1,[aname],",")+1)) As firstname FROM
SummaryJudgment_FinalForgotten) & " " & (SELECT
summary_judgment.middle_initial AS middlename FROM summary_judgment)
) AS fullname
FROM SummaryJudgment_FinalForgotten AS f INNER JOIN summary_judgment
AS s ON f.lastname = s.last_name && f.firstname = s.first_name;
Basically this is what two tables look like (note they will have more fields than 1 where last or first name of different fields can be similar):
SummaryJudgment_FinalForgotten (table)
aname (field)
Leventhal,Raymond (data)
summary_judgment (table)
first_name(field)
Raymond (data)
last_name (field)
Leventhal (data)
middle_initial (field)
P (data)
Ultimately, I'm trying to create a new table that is like
SummaryJudgment_FinalForgotten but with the middle initial from
summary_judgment appended:
Leventhal,Raymond P
You do not need to write 3 select statements to concatenate the values into one field.
select left(...) & right(...) & initial AS fullname INTO SummaryJudgment_FinalForgottenWithMiddle
FROM SummaryJudgment_FinalForgotten
Are you trying to use two ampersands to represent a logical AND?
FROM SummaryJudgment_FinalForgotten AS f INNER JOIN summary_judgment
AS s ON f.lastname = s.last_name && f.firstname = s.first_name;
I don't think that's legal for Access' Jet/ACE database engines. Try it with the AND keyword in place of &&.
OTOH, I wonder if you can do something simpler.
SELECT last_name & "," & first_name & " " & middle_initial AS fullname
INTO SummaryJudgment_FinalForgottenWithMiddle
FROM summary_judgment;
This works:
SELECT left([aname],InStr(1,[aname],",")-1) & " "
& right([aname],Len(aname)-InStr(1,[aname],",")) & " "
& summary_judgment.middle_initial AS fullname
INTO SummaryJudgment_FinalForgottenWithMiddle
FROM SummaryJudgment_FinalForgotten, summary_judgment;
Though you might want this instead:
SELECT left([aname],InStr(1,[aname],",")-1) & ", "
& right([aname],Len(aname)-InStr(1,[aname],",")) & " "
& summary_judgment.middle_initial AS fullname
INTO SummaryJudgment_FinalForgottenWithMiddle
FROM SummaryJudgment_FinalForgotten, summary_judgment;
The second version gives you the comma after the last name. Note that Right counts from the right, which is why you have to subtract the InStr value from the length.
EDIT:
The code I gave above works with your sample data--one row in each table. With more rows, it gives a cross product of (LastName, FirstName) x MiddleInitial. It occurred to me that that might be the case, so I went back to my test & added a second row--it is true. So then I tried to write the join expression....
Access doesn't like this:
... ON left([aname],InStr(1,[aname],",")-1) = last_name ...
It throws the error "Join expression not supported." Changing it to this:
... ON (trim((left(SummaryJudgment_FinalForgotten.aname,InStr(1,[aname],",")-1))=trim(summary_judgment.last_name))) ...
results in a query that runs & creates the table, but doesn't create any rows (the same was true before I added the "trim" calls in an attempt to fix it).
So I tried specifying the table for all occurences of aname. No joy--until I realized that I was making the wrong comparison (derived-last to last and derived-first to last--oops).
Using the following FROM clause with either above SELECT ... INTO does work correctly:
FROM
SummaryJudgment_FinalForgotten INNER JOIN
summary_judgment ON
((left(SummaryJudgment_FinalForgotten.aname,InStr(1,SummaryJudgment_FinalForgotten.[aname],",")-1))=summary_judgment.last_name) AND
((right(SummaryJudgment_FinalForgotten.aname,Len(SummaryJudgment_FinalForgotten.aname)-InStr(1,SummaryJudgment_FinalForgotten.[aname],","))=summary_judgment.first_name));
It might even work correctly without the full qualification of each field now that I'm joining first to first & last to last (since there is no duplication across the tables), but having proven that it does work, I'm done.

"Null" value in Queries

I'm trying to run code that will copy fields into a new table, moving them from a _New table to the original table. The VBA code that does this works as such:
SQLStatement = CStr("INSERT INTO " & TName & " SELECT * FROM " & TName & "_New")
Log.WriteLine "Running query with string """ & SQLStatement & """ "
QueryTimer = Timer
DoCmd.RunSQL SQLStatement
Log.WriteLine "Query time: " & (Timer - QueryTimer)
The log is just a handful of procedures in a class module I threw together. Its output on the error is
#142921: Running query with string "INSERT INTO Records SELECT * FROM Records_New"
#142941: Error Date/Time: 7/21/2009 2:29:40 PM
#142941: Error # & Description: 3162, You tried to assign the Null value to a variable that is not a Variant data type.
I can confirm that TName and SQLStatement are both valid strings at the time the SQL operation is run, and that the sources (Records and Records_New) are both valid. Option Explicit is set elsewhere in the file to avoid any confusion from typos. The error is thrown on the DoCmd line.
Why would this have a Null value, even though DoCmd.RunSQL doesn't return a value?
Can you post the table descriptions for Records and Records_New tables?
I would wager that you are trying to insert a NULL value into one of the columns of the "Records" table (and the column description is NOT NULL).
Hope this helps.
I think it will help if you also change the insert statement to be more specific about which columns it is inserting/selecting. You are asking for bugs by being so non-specific.
This may seem like it is non-responsive to your answer, but I suspect that the columns in the select table and destination table are either not lined up, or there is a field in the destination table that disallows null.
Try this:
In a new Query (in SQL view) paste your query "INSERT INTO Records SELECT * FROM Records_New" in and try to run it manually. I bet you get a more specific error and can troubleshoot the query there before running it with the added complexity of the code around it.
INSERT INTO Statement (Microsoft Access SQL)
Your SQL INSERT statement is incorrect - it should be:
INSERT INTO Records SELECT * FROM [Records_New];
Here's what you need to use:
CStr("INSERT INTO " & TName & " SELECT * FROM [" & TName & "_New)"];")
Maybe Timer needs parens?
QueryTimer = Timer()