Mysql query replace first empty string - mysql

I have many fields that contains extra space in start, like:
" RTX 3060"
-^
I want to remove first extra space from all fields of this table.
What I tried:
UPDATE table set field = concat( '', substring(field , 1)) where left(field ,1)=' ';
Return 0 result!

Try with this statement, it will remove only the first space char of field is it's a space:
UPDATE `table`
SET column = SUBSTRING(column,2)
WHERE SUBSTRING(column, 1, 1) = ' ';

Try using the LTRIM function. The syntax looks something like this:
UPDATE table SET column = LTRIM(column);

Related

Why CONCAT does not insert text for the first time into mySQL table?

I am using UPDATE to insert simple text into a table where the field is MEDIUMTEXT (nullable field).
It is strange that it does not work when the field is null initially. If I manually enter at least a one character/space, then it's working.
I want to append the new text into existing text in the field.
UPDATE pen SET
PEN_STATUS = #PenStat,
PEN_STATUS_CHANGE_REASON = CONCAT(PEN_STATUS_CHANGE_REASON,'\n',ChangeDate,':',EmployeeID,':',ChangeReason)
WHERE PEN_ID = PenID;
Why is this?
CONCAT does not handle NULL values. As explained in the MySQL manual:
CONCAT() returns NULL if any argument is NULL.
You want to use COALESCE to handle that use case, like :
UPDATE pen SET
PEN_STATUS = #PenStat,
PEN_STATUS_CHANGE_REASON = CONCAT(
COALESCE(PEN_STATUS_CHANGE_REASON, ''),
'\n',
ChangeDate,
':',
EmployeeID,
':',
ChangeReason
)
WHERE PEN_ID = PenID;
Presumably, because something is NULL. Try using CONCAT_WS() instead:
UPDATE pen
SET PEN_STATUS = #PenStat,
PEN_STATUS_CHANGE_REASON = CONCAT_WS('\n',
PEN_STATUS_CHANGE_REASON,
CONCAT_WS(':', ChangeDate, EmployeeID, ChangeReason
)
)
WHERE PEN_ID = PenID;
CONCAT_WS() ignores NULL arguments. Plus, the separator only needs to be listed once.

SET variable for use as field in query?

I feel like I've done this before but missing something. I'm going through a database I inherited and want to see a bunch of DISTINCT values. I was thinking I could do something like this instead of writing cf_840 (or whatever number) a bunch of times and just change the actual field name in one spot...
SET #var = 'cf_840';
SELECT DISTINCT #var, COUNT(#var) AS counter
FROM vtiger_leadscf
GROUP BY #var
ORDER BY #var;
But this isn't working right and I feel like I'm missing something simple but can't find the right thing to search on SO.
You cannot use variables to directly specify fields outside of dynamically constructing a query; at best you can choose a value from a field conditionally, like CASE #var WHEN 840 THEN cf_840 WHEN 1 THEN cf_1 .... etc END AS fieldVal
Otherwise, you need to dynamically construct a query string with the field name "baked" into the query that gets executed.
C# style: var query = String.Format("SELECT {0}, COUNT(DISTINCT {0}) AS counter FROM .... blah blah blah", fieldName);
SQL Proc Style: SET query := 'SELECT ' + fieldName + ', COUNT(DISTINCT ' + fieldName.....and so on, then PREPARE and EXECUTE.
Edit: I'm not sure why you're selecting #var if you're getting the count of its values. I am guessing you want the field name included in the result, so perhaps the examples would be better as SELECT '{0}' as theField, COUNT(DISTINCT {0}) AS counter ... and 'SELECT ''' + fieldName + ''' AS fieldName, COUNT(DISTINCT ' + fieldName ....`
Also, you should not need GROUP BY or ORDER BY clauses, these queries will have only one result row.
To use a variable in the current query, declare it in the body of the SELECT
SELECT DISTINCT #var := 'cf_840' AS fieldName, COUNT(#var) AS counter
FROM vtiger_leadscf
GROUP BY fieldName
ORDER BY counter;

Adding space to a string after 3 characters starting from right in mysql

I have a requirement where at first I need to remove all space from a string, then put a space after 3 characters started from right.
I have removed the spaces but putting space after certain characters is not happening.
IE:
AX1098
AX1 098
SELECT INSERT('AX1098', 4, 0, ' ');
http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_insert
To update all rows:
UPDATE YOURTABLE
SET YOURCOL = INSERT(YOURCOL, 4, 0, ' ');
If strings have different length then:
update t set F = INSERT(F,LENGTH(F)-2,0,' ');

How to replace only few characters from a string in mysql?

I have a field urn_sem.studentid that I'd like to replace a few characters in; for example:
ABC/2011/BCOMH_NC/I/12 → ABC/2011/BCOMH/I/12
ABC/2011/BCOMH_NC/I/24 → ABC/2011/BCOMH/I/24
I've tried this query:
SELECT REPLACE(studentid, 'KNC/2011/BCOMH_NC/', ' KNC/2011/BCOMH/')
FROM urn_sem
but it doesn't show the new value.
Do you want this:
update urn_sem
set studentid = REPLACE(studentid, 'KNC/2011/BCOMH_NC/', ' KNC/2011/BCOMH/')
where studentid like '%KNC/2011/BCOMH_NC/%'
The WHERE clause is optional. It ensures that the replace is only on rows that change.
And this sample query does not work?
SELECT REPLACE (studentid, '_', '') FROM urn_sem

MySql: updating a column with the column's content plus something else

I'm don't have a lot of knowledge of MySql (or SQL in general) so sorry for the noobness.
I'm trying to update a bunch of String entries this way:
Lets say we have this:
commands.firm.pm.Stuff
Well I want to convert that into:
commands.firm.pm.print.Stuff
Meaning, Add the .print after pm, before "Stuff" (where Stuff can be any Alphanumerical String).
How would I do this with a MySql Query? I'm sure REGEXP has to be used, but I'm not sure how to go about it.
Thanks
Try something like this. It finds the last period and inserts your string there:
select insert(s, length(s) - instr(reverse(s), '.') + 1, 0, '.print')
from (
select 'commands.firm.pm.Stuff' as s
) a
To update:
update MyTable
set MyColumn = insert(MyColumn, length(MyColumn) - instr(reverse(MyColumn), '.') + 1, 0, '.print')
where MyColumn like 'commands.firm.pm.%'
Perhaps use a str_replace to replace commands.firm.pm to commands.firm.pm.print
$original_str = "commands.firm.pm.15hhkl15k0fak1";
str_replace("commands.firm.pm", "commands.firm.pm.print", $original_str);
should output: commands.firm.pm.print.15hhkl15k0fak1
then update your table with the new value...How to do it all in one query (get column value and do the update), I do not know. All I can think of is you getting the column value in one query, doing the replacement above, and then updating the column with the new value in a second query.
To update rows that end in '.Stuff' only:
UPDATE TableX
SET Column = CONCAT( LEFT( CHAR_LENGTH(Column) - CHAR_LENGTH('.Stuff') )
, '.print'
, '.Stuff'
)
WHERE Column LIKE '%.Stuff'
To update all rows - by appending .print just before the last dot .:
UPDATE TableX
SET Column = CONCAT( LEFT( CHAR_LENGTH(Column)
- CHAR_LENGTH(SUBSTRING_INDEX(Column, '.', -1))
)
, 'print.'
, SUBSTRING_INDEX(Column, '.', -1)
)
WHERE Column LIKE '%.%'