order by charindex >0 and SQL concat [closed] - sql

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I want to order Recordset by setting priority when ID is in an array:
This is the comma separated myString which holds a series of ID: ,1,2,4,6,8,12,34,
I use concat to add comma before and after ID so I can compare it against the string:
sql="select * from customers order by case when charindex( concat(','," & id & ",',') , '" & myString & "' )>0 then 1 else 0 end desc"
I get syntax error message. I am not sure if there is error in SQL structure as I used concat in deep structure or there is error in using quote and double quote?

I found the problem when trying to answer #Diado Comment. The parameter ID should be used in SQL context not as a variable in VbScript context. So I have changed the query to this and it works:
sql="select * from customers order by case when charindex( concat(',',id,',') , '" & myString & "' )>0 then 1 else 0 end desc"

Related

Delete a row from subreport in MS-Access [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am trying to Delete a row from a subreport (my subreport used different columns values from different tables). But I've got a Data type mismatch error!
Below is the code I used:
CurrentDb.Execute "DELETE FROM StateBudget " & " WHERE S_ID = " & _
DLookup("ID", "States", "State='" & _
Me.subformStateBudget.Form.Recordset.Fields("State") & "'")
I think S_ID is a text value, so how can I change DLookup value to string or text?
You don't need to change the DLookUp value to a string, you need to pass it to SQL as a string (in quotes):
CurrentDb.Execute "DELETE FROM StateBudget " & " WHERE S_ID = """ & _
DLookup("ID", "States", "State='" & _
Me.subformStateBudget.Form.Recordset.Fields("State") & "'") & """"

VBA - SQL query with dates as variable on Oracle DB [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am performing in VBA an SQL query selecting per date. Everything is working until I specify the dates, as the code below:
conn.Open dbConnectStr
StartDate = "01/01/2015"
SQL_String = "SELECT * FROM database " & _
"WHERE date BETWEEN to_date('01/01/2015','dd/mm/yyyy') and to_date('31/12/2016','dd/mm/yyyy')"
recset.Close
conn.Close
But, when I put in a variable instead of dates I obtain an error multiple-step operation completed with one or more errors. Check each status values.
conn.Open dbConnectStr
StartDate = "01/01/2015"
EndDate = "31/12/2016"
SQL_String = "SELECT * FROM database " & _
"WHERE date BETWEEN to_date('" & StartDate & "','dd/mm/yyyy') and to_date('" & EndDate & "','dd/mm/yyyy')"
recset.Close
conn.Close
Anybody could suggest how to proceed?

Is this valid SQL? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'd like to update a table row where:
Either statement1 is true OR statement2 is true
AND independent of which one of those are true, statement3 is also true.
Would this be a valid way of writing this SQL statement?
$upadte = "UPDATE table SET
header = 'value'
WHERE (statment1 = 'true' OR statment2 = 'true') AND statement3 = 'true'";
Yes the SQL is correct in the where part, but I think the 'true' must be true without "'" because if it's wrapped in '' will be interpreted as a string expresion rather than a boolean. The (A OR B) will return true if A or B are true, and if they are true. This condition is checked first because of the parenthesis. Then the thir statetment will be checked and if is true, the complete expression will return true.
$upadte = "UPDATE table SET
header = 'value'
WHERE (statment1 = true OR statment2 = true) AND statement3 = true";

SQL inner join Syntax error (missing operator) in query expression [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm making a library system which allows student assistants to log in to the system. I have a table for student information to pull out information of the student assistants from instead of putting that same information in the student assistants accounts table because that would be a bit too redundant.
sqlSearch = "select * from tblSALogin where SA_ID = '" & txtUserName.Text & _
"' inner join tblStudentInfo on tblSALogin.StudentID = tblStudentInfo.StudentID"
I'm using the above SQL query expression to do that but it gives me a missing operator error and i don't know why. The syntax of the expression is correct and all of the tables required for the expression have already been related and have the required records.
You need to put the where after the join:
sqlSearch = _
"select * from tblSALogin " & _
"inner join tblStudentInfo on tblSALogin.StudentID=tblStudentInfo.StudentID " & _
"where SA_ID = '" & txtUserName.Text & "'"
Also note that you are vulnerable to SQL injection with this query, and you should look into using parameterized queries.
the join should be in the FROM clause, not in the WHERE clause
Try Like this
sqlSearch = "select tblSALogin.*,tblStudentInfo.* from tblSALogin inner join tblStudentInfo on tblSALogin.StudentID = tblStudentInfo.StudentID where SA_ID = '" & txtUserName.Text &"' "
Where clause should be come after Join.

How to concatenate result strings in a MS Access Query obtained by GROUP BY [duplicate]

This question already has an answer here:
Combine values from related rows into a single concatenated string value
(1 answer)
Closed 8 years ago.
I have seen that strings can be concatenated in Access with &.
However, I cannot write the following query:
SELECT age, &names ==> this line does not work, how can I concatenate the strings?
FROM table GROUP BY age;
So that I get for example: "30; JohnWilliamPeter", all of them 30 years old.
Any way to do that?
Do you mean:
SELECT age & "; " & names
FROM table
GROUP BY age & "; " & names
However, this seems like you are aiming at a solution that should be presentation, not SQL.