Use of in clause value to another sql query - mysql

I have one variable:
FarmerServiceID+= "'"+tabledata1.get(k)+"',";
My problem is: the values which are coming tabledata1.get(k) ends with ','
and what happens is I use this FarmerServiceID in the following query.
select farmerid from tblfarmer where farmermapid in("+ FarmerServiceID+");
error: syntax error near ')'
I know error is coming because of ','
How do I remove this error?

the error occurs because of your FarmerServiceID has a , (comma) at the end of the string.
I will suggest you to remove the comma at the end of the string before you do the SQL query.
FarmerServiceID += "'"+tabledata1.get(k)+"',";
FarmerServiceID = FarmerServiceID.substring(0, FarmerServiceId.lastIndexOf(',')); -- Remove the comma at the end of the string
Then do your SQL Query
select farmerid from tblfarmer where farmermapid in("+ FarmerServiceID+");

Related

Inserting a substring value into an already existing entry

Currently I have a database with the following parameters and entry:
Now I want to fill the column status_from in this particular entry with the id 1 with a substring of the action column. Which is basically I want to fill my status_from column with a word that comes after from and before to of the action column. To achieve this I've tried using the following statement:
INSERT INTO crm_logs2(status_from) SELECT status_from WHERE id='1' VALUES (substring_index(substring_index(action, 'from', -1),'to', 1))
But doing this gave me the following error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'VALUES (substring_index(substring_index(action, 'from', -1), ...' at line 1
You need to use the UPDATE statement
UPDATE crm_logs2
SET status_from = (SUBSTRING_INDEX(SUBSTRING_INDEX(action, "from", -1), "to", 1))
WHERE id = 1
update crm_logs2
set action=(select SUBSTRING_INDEX(SUBSTRING_INDEX(action, ' ', 6), ' ', -1) from
crm_logs2 where id=1)where id=1
Try this and let us know if it works

Why is this code not returning any values?

I'm using MySQL and I'm trying to return values of CustomerLastName that starts with M, and I know there are some in the database, but they are not being returned. I see CustomerLastName and CustomerFirstName but no names.
SELECT 'CustomerFirstName', 'CustomerLastName'
FROM customer
WHERE LEFT(CustomerLastName, 1 = 'M ');
Remove the Single Quotes from select statement
SELECT CustomerFirstName, CustomerLastName
FROM customer
WHERE LEFT(CustomerLastName, 1 = 'M ');
As well as the incorrect quotes in the select WHERE LEFT(CustomerLastName, 1 = 'M '); is not syntactically correct - and you should be seeing a raft of warnings.
Change to
WHERE LEFT(CustomerLastName, 1) = 'M';
And review When to use single quotes, double quotes, and backticks in MySQL

Setting a JSON array of strings as a column in a PostgreSQL table

I'm a beginner to SQL (as you'll soon be able to tell...) and I cannot for the life of me figure out how to best insert a simple JSON array of strings into a PostgreSQL table. I suspect the solution is quite easy but I've spent just a bit too long trying to puzzle it out on my own.
First I create the table:
CREATE TABLE test (
id serial PRIMARY KEY
my_array jsonb
);
Where the array is of type JSON. Insert some initial data:
INSERT INTO test (id, my_array) VALUES(1, '[]');
And now I want to update the myarray column with a JSON array using Node.js node-postgres. The array might look something like
const myArray = ['foo', 'bar', 'foobar\'s escaped character emporium'];
await db.none(
'UPDATE test ' +
`SET my_array = ${myArray} ` +
'WHERE id = 1'
);
This results in
error: syntax error at or near ","
Ok, so what if I do
await db.none(
'UPDATE test ' +
`SET my_array = "${myArray}" ` +
'WHERE id = 1'
);
I get
error: column "foo,bar,foobar's escaped character emporium" does not exist
and if I do
await db.none(
'UPDATE test ' +
`SET my_array = ${JSON.stringify(myArray)} ` +
'WHERE id = 1'
);
I get
ERROR error: syntax error at or near "["
Finally, if I do
await db.none(
'UPDATE test ' +
`SET my_array = '${JSON.stringify(myArray)}' ` +
'WHERE id = 1'
);
I end up with
stack=error: syntax error at or near "s"
I've also tried storing the data data as a native PostgreSQL array but I encounter similar problems:
CREATE TABLE test (
id serial PRIMARY KEY
my_array text ARRAY
);
INSERT INTO test (id, my_array) VALUES(1, '{}');
Then
const myArray = ['foo', 'bar', 'foobar\'s escaped character emporium'];
await db.none(
'UPDATE test ' +
`SET my_array = ${myArray} ` +
'WHERE id = 1'
);
gives
stack=error: syntax error at or near ","
Similar variations using JSON.stringify() and combinations of different quotes have proved fruitless as well. I kind of expected this approach to be less likely to work as PostgreSQL arrays are just a different format, but I was hoping there might be some kind of attempt at coercion. Reading through the documentation I can't spot any obvious way to convert a JSON array into the expected format for a PostgreSQL array.
Consider using a Parameterized query or Prepared statements.
That will help with you with qoutes and get protection against SQL injection as a bonus.

Using ltrim and rtrim in combination of substring

Hey there so I'm in the process of another project conversion there is a line written in Oracle SQL and I'm trying to convert it to MS SQL:
Oracle PL/SQL:
IF LTRIM(sCmtStr) IS NOT NULL THEN
sTrimStr := ' '||SUBSTR(RTRIM(LTRIM(sCmtStr),'; '),1,999);
ELSE
sTrimStr := NULL;
MS T-SQL:
IF ltrim(#sCmtStr) IS NOT NULL
SET #sTrimStr = ' ' + ISNULL(substring(rtrim(ltrim(#sCmtStr), '; '), 1, 999), '')
ELSE
SET #sTrimStr = NULL
I get the following error:
Msg 174, Level 15, State 1, Procedure TRIMCOMMENT, Line 12
The rtrim function requires 1 argument(s).
Any idea? Thank you.
RTRIM (https://learn.microsoft.com/en-us/sql/t-sql/functions/rtrim-transact-sql) Does not take 2 arguments; it can only trim spaces from the end of a string.
TRIM (https://learn.microsoft.com/en-us/sql/t-sql/functions/trim-transact-sql), on the other hand, can be given other characters to remove. It will remove the characters from both ends, however, so you might have to fiddle a bit if you only want it to remove from the end of the string.

Replace characters in row Mysql

I have table in db.
Structure:
street_id | street_atrribute | street_name1 | street_name2
In this database I have row with ""
For example:
546 | ul. | "Związku Młodzieży Wiejskiej ""Wici"""|
2836 | ul. |"Okulickiego ""Niedźwiadka""" |gen. Leopolda
I want delete characters:
""
I try replace this characters on space.
I try to use query:
SELECT street_id, street_attribute REPLACE(street_name1, '""', ' '), street_name2 FROM `street`;
but I have error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'REPLACE(street_name1, '""', ' '), street_name2 FROM `street` LIMIT 0, 25' at line 1
I have no idea what I'm doing wrong.
Can you ask for help?
You have a problem with your sql syntax, you're missing a comma :
SELECT street_id, street_attribute, REPLACE(street_name1, '""', ' '), street_name2 FROM `street`;
I assume you have double quotes " surrounding this query. So when you use them again in your REPLACE method it is closing the first double quote. You will need to escape them.
REPLACE(street_name1, '\"\"', ' ')