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
Related
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
I am getting a syntax error 3075 in the following code. I am trying to open a recordset where the field [subject] contains the [ID] from an open form (Edit_Shipment_frm!Text105). The subject field will be laid out like: Information Request - 1715, where 1715 is the [ID]. I know I need to use a wildcard before [ID] in the criteria. I am struggling to find a way to do this that access will accept. Does anyone have any ideas? I don't see any missing operators below so it must just not like my syntax. Thank you in advance!
Set rst1 = db.OpenRecordset("SELECT Subject, Contents FROM LinkedTable WHERE Subject = *" & [Forms]![Edit_Shipment_frm]![Text105])
You can't use the asterisk like this. Correct SQL would be
... WHERE Textfield LIKE '*" & [string that contains a part of Textfield] & "*'"
but you have it the other way around (your ID is part of the search string).
You need to extract the ID from the form field. If [Text105] (please give this field a meaningful name!) always looks like "some string - ID", you can use the Split() function:
S = "SELECT Subject, Contents FROM LinkedTable WHERE Subject = " & _
Split([Forms]![Edit_Shipment_frm]![Text105], " - ")(1)
Set rst1 = db.OpenRecordset(S)
Split() returns a 0-based array, so Split(..., " - ")(1) gives the part after " - ".
But to get some error checking (e.g. if " - " can be part of the string before the ID), you should put the extracting into a separate function, where you check for NULLs, and use UBound() of the array.
I am trying to update a field in a table using an SQL update query where there is a like statement referencing a value in another table. They syntax unfortunately is not working. Below is my code. In short, I am trying to put a '1' in the field 'Query07ParolaChiave' in the table 'tblSearchEngine01' when the value located in table 'tblsearchengine07' is present in the field 'tblMasterListOfEventsNotes' located in the table 'tblSearchEngine01'. I think my code is almost complete but there is a syntax issue which i cant find.
st_sql = "UPDATE tblSearchEngine01, tblSearchEngine07 SET tblSearchEngine01.Query07ParolaChiaveSelect = '1' WHERE ((([tblSearchEngine01].[tblMasterListOfEventsNotes]) Like " * " & [tblsearchengine07].[ParolaChiave] & " * "))"
Application.DoCmd.RunSQL (st_sql)
I suggest you 2 solutions :
This one is using EXISTS functions, and will check for each row in tblSearchEngine01 if there is a matching value in tblsearchengine07
UPDATE
tblSearchEngine01
SET
tblSearchEngine01.Query07ParolaChiaveSelect = '1'
WHERE
EXISTS (SELECT 1
FROM tblsearchengine07
WHERE [tblSearchEngine01].[tblMasterListOfEventsNotes] Like '*' & [tblsearchengine07].[ParolaChiave] & '*')
This one is more performant because it uses JOIN
UPDATE
tblSearchEngine01
INNER JOIN tblsearchengine07
ON [tblSearchEngine01].[tblMasterListOfEventsNotes] Like '*' & [tblsearchengine07].[ParolaChiave] & '*'
SET
tblSearchEngine01.Query07ParolaChiaveSelect = '1'
I read something like in ADO/VBA, you have to use % instead of * as the wildcard.
You can have more information on wildcard and LIKE comparator here
UPDATE
Why the '1' after select in your first solution?
EXISTS (SELECT 1 ... is better for performance because it return only the number 1 instead of fields, anyway EXISTS just stop the excecution after 1 element found.
'Performant' means more consuming in regards to space and memory?
JOIN is more performant in term of time of execution, RDBMS are far better at joining tables than using subquery, in some rare case, it's more interesting to use the 1st solution.
Also, any initial thoughts as to why my original solution (coming straight from an Access Query which works) does not function?
I cannot really know but perhaps it's because of " * ", because you are saying SPACE + * + SPACE + VALUE + SPACE + * + SPACE. For ex : 'John' LIKE ' John '
May be with "*" instead of " * " could solve it...
I have no other track, I'm not Access sql developper, I usually play around Sql server/Oracle/mySql, hope it helped. ;)
Try to change your like this way:
... Like '*" & tblsearchengine07.parolachiave & "*'))"
The like statement go into the WHERE clause.
If you do want to use LIKE without you care about caps letters, then you can use it like this:
LIKE COLUMN_NAME = '%WhatYouLike%'
My suggestion is:
Use a table variable (#Table) with a unique/primary key coming from the table to be updated.
SELECT all the data to be updated (you can add the like statement here) and then INSERT that in the created table variable.
Construct the UPDATE statement with an INNER JOIN to the table variable matching with the unique/primary key.
I know this may take a lot of steps but believe me these are more efficient than using a black list approach.
I'm trying to count the number of records in a table that meet a certain criteria. My preference is to use SQL, not Dcount, as I want to get better at SQL. Here's my current code below:
Dim countString As String
Dim count
countString = "SELECT COUNT(*) FROM `Engagement Letters` WHERE 'Client ID' = " & Me.cboSelectClient
count = CurrentDb.OpenRecordset(countString).Fields(0).Value
Yeah I know, I've used spaces in my tables and field names - I will change that. Though I think I should still be able to run this query as is, so I will leave it as is for now.
When I run the above, I get runtime error 3464 - data type mismatch in criteria expression. I've had the below dcount function work fine:
count = DCount("[Engagement Letter ID]", "Engagement Letters", "[Client ID] = " & Me.cboSelectClient)
And also the below COUNT query without the WHERE works fine:
"SELECT COUNT(*) FROM `Engagement Letters`"
My knowledge of SQL is very minimal, and my knowledge of more advanced VBA is also quite minimal, so I'm not sure where I'm going wrong. Can anyone help me with this?
Try building your string like this.
countString = "SELECT COUNT(*) FROM [Engagement Letters]" & vbCrLf & _
"WHERE [Client ID] = " & Me.cboSelectClient
Debug.Print countString
Use square brackets around object (table and field) names which include spaces or any characters other than letters, digits, and the underscore character.
For the table name, you used `Engagement Letters`, and the backticks appear to work the same as square brackets. Perhaps they always work equally well, but I don't know for sure because I use the brackets exclusively. And brackets instead of backticks might help you avoid this mistake ...
WHERE 'Client ID' = " & Me.cboSelectClient
... that was asking the db engine to compare the literal string, "Client ID", to the numerical value (?) you pulled from cboSelectClient.
I used vbCrLf between the 2 parts of the SELECT statement because I find that convenient when examining the completed string (via Debug.Print).
I have the following statement and it returns my desired result in Access however in Visual Studio, I receive an error saying "; expected", what could be the problem?
var query = "SELECT Count(*) FROM usersTable WHERE (((usersTable.[uDateCreated]) Between DateAdd("d",-2,Now()) And Now()))";
You need to escape your quotes inside your string:
" .. Between DateAdd(\"d\",-2 .. "
^ ^ escape the quotes
You're using a quotation mark in your query, which is ending the string. Use apostrophes around d instead:
var query = "SELECT Count(*) FROM usersTable WHERE (((usersTable.[uDateCreated]) " & _
"Between DateAdd('d',-2,Now()) And Now()))"
Specifically:
DateAdd('d',-2,Now())
I think your problem is that you have " (quotes) in your string without escaping them. I donut know which language you are using, but for many you escape with \ (backslash), then your string would read DateAdd(\"d\",