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, '\"\"', ' ')
Related
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
I have a very specific question.
I have to build a SQL statement that builds a table where some columns are merged together. These columns shall be formatted with delimiters like '\n' or ' ' or ' - '. These delimiters shall be added only if the column before is not empty or null. This should prevent empty lines or unneeded delimiters.
Here is how I started:
SELECT
any_table.table_id,
CONCAT(any_table.text1, '\n', any_table.text2) AS text1_2,
FROM
any_table
WHERE any_table.use = 'true'
This code concats text1 and text2 as a new column text1_2 and uses a line feed as delimiter. The missing part is that line feed shall just be added if any_table.text1 is not null or empty.
Is there an elegant way in doing this with SQL?
thx
Some databases support a very handy function called concat_ws() which does exactly what you want:
CONCAT_WS('\n', NULLIF(any_table.text1, ''), NULLIF(any_table.text2, '')) AS text1_2,
In standard SQL, you can do:
TRIM(LEADING '\n' FROM CONCAT( '\n', || NULLIF(any_table.text1, ''),
'\n' || NULLIF(any_table.text2, '')
)
)
It is possible that your database supports neither of these constructs.
if you'r under SQL SERVER you can use,
SELECT id, CONCAT(colonne1 + ' - ', colonne2) FROM "table"
if you'r under Oracle : you shoul use || for concaténation like
SELECT id, CONCAT(colonne1 || ' - ', colonne2) FROM "table"
There are a lot of rows with multiple spaces in column title and I want to replace them with a single space.
update abc set title = REPLACE(title, " ", " ");
Nothing is replaced.
I'm using phpMyAdmin.
I noticed (clicking on the button Simulate query that my query is transformed into:
update abc set title = REPLACE(title, " ", " ");
so replace single space with single space.
Any help?
While checking this page it came to my attention that to replacing all the double spaces from the database you might have triple space or more on the single record.
The thing that the some solution didn't take in consideration.So you need to make sure that your statement replace them all. Doing one time or two time replacement of double space with single space might not cover all the corrupted data.
For example having a record value as 'A B C'; what you can do is:
first replace all the single space with open/closed characters
like <> , or [] or {}...
Then replace the back to back reversed order characters (closed/open)
with empty value, so all >< or ][ or }{ will be removed.
Final step is to restore the single spaces by replacing the remaining open/close
characters with single space, for example <> will be changed back to ' '
I always use something like following to fix my data:
UPDATE Table1 SET Column1 = REPLACE(REPLACE(REPLACE(Column1, ' ', '<>'), '><', ''),'<>',' ');
Number of consecutive space characters can either be odd or even. You can replace two space characters with one space character, and do a similar replace again on the modified string to cover all the odd/even cases.
UPDATE abc SET title = REPLACE(REPLACE(title, ' ', ' '), ' ', ' ');
Explanation:
2 spaces: First replace will convert to 1 space. Second replace will not modify further.
3 spaces: First replace will convert (2+1) spaces to (1+1). Second will convert (1+1 = 2) spaces to 1 space.
4 spaces: First replace will convert (2+2) spaces to (1+1). Second will convert (1+1 = 2) spaces to 1 space.
and so on...
DEMO:
mysql> select
-> dt.test_str,
-> REPLACE(REPLACE(dt.test_str, ' ', ' '), ' ', ' ') AS modified
-> FROM
-> (SELECT 'thi s is a weird string' AS test_str) AS dt ;
+--------------------------------+--------------------------+
| test_str | modified |
+--------------------------------+--------------------------+
| thi s is a weird string | thi s is a weird string |
+--------------------------------+--------------------------+
1 row in set (0.00 sec)
You can try the following SELECT example, with REGEXP_REPLACE is used. For example:
SELECT 'ab asd asd a qeqw q qwe qweqw qw' AS `text 1`, REGEXP_REPLACE('ab asd asd a qeqw q qwe qweqw qw', ' \+', ' ') AS `text 2`;
REGEXP_REPLACE documentation
You can use REGEXP_REPLACE in an UPDATE statement:
UPDATE abc SET title = REGEXP_REPLACE(title, ' \+', ' ');
Here is my approach
SELECT ARRAY_TO_STRING(ARRAY_AGG(VALUE::varchar),' ') FROM TABLE(flatten(split(REGEXP_REPLACE('','\n'),' '))) WHERE VALUE <> '' ORDER BY INDEX;
might be a long route but does the job.
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.
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+");