Which SQL command should I use to replace specific values? - mysql

I have numeric values like below:
0
2323
1003
I have to replace only single zeros to 100. I tried the following code but it affects the other values which include 0.
UPDATE table_name SET field_name= replace(field_name,"0","100");
The values should be like below after the transaction:
100
2323
1003
Which SQL command should I use?
Thanks.

You should use a where clause instead of replace.
UPDATE table_name SET field_name = 100
where field_name = 0

Related

How to alter values in a column for a large number of data in MySQL? Can it only be done manually for each value or is there another way?

Suppose I am having a column value as:
code value
#125 abc
#137 xyz
#197 pqr
And I want to change (Remove first character? Remove all # Signs?) values as:
code value
125 abc
137 xyz
197 pqr
You mean to remove the first character?
UPDATE t SET c = SUBSTRING(c, 2)
Or remove leading hashes?
UPDATE t SET c = TRIM(LEADING '#' FROM c)

Delete a specific value from a string (string contains comma separated values)

For example, if I have a table structure like this:
Table 1
ID Name Value
001 Rajesh 90,100,210,400
002 Suresh 100,400,300,66
003 Mahesh 200,500
004 Virat 400,578,57
How can I delete 400 from Suresh?
DELETE Value ="400" FROM table1
WHERE Name = 'Suresh'
This doesn't work.
I would recommend splitting the values into a second table which is related via the person's ID. However, you can use the following query for your current situation:
UPDATE table1
SET Value = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', Value, ','), ',400,', ','))
WHERE Name = 'Suresh'
Here's a SQL Fiddle. For reference, see MySQL's string functions

MySQL - replacing specific character inside of column string

Got a column that keeps strings like such:
{"plan-36";"id-36";}
I want to check each string of this column, and if matches my criteria, it would change a specific character of this string (for example would change number 36 to 99). Here's what i got so far:
SELECT string_column
FROM example_table
WHERE
INSTR(string_column,'plan-36') > 0
OR
INSTR(string_column,'id-36') > 0
This only returns rows that has 'plan-36' or 'id-36' string in them. What i need is - if this row contain such strings, i need to change the 36 value, to let's say 99.
Which sql functions do i need for this?
UPDATE example_table
SET string_column = REPLACE(string_column, "36", "99")
WHERE
INSTR(string_column,'plan-36') > 0
OR
INSTR(string_column,'id-36') > 0
would be the syntax, this will update all instances of 36 to 99, Be careful and test on backup data first.
It may be better to use this
SET string_column = REPLACE(string_column, "-36", "-99")
which includes the dash

How to return all matches in mysql where value occurs in string at position n?

Im looking to run a query in phpmyadmin (mysql) that wil check a table for a specific value at a specific postion in a string, i'm new to mysql and this is what i've tried but there's a syntax issue. I'm looking to find the value "1" at position 5 and display all those users that possess this.
SELECT*
FROM`user`
WHERE`options`LOCATE(`options`,1,5)
LIMIT 0 , 30
regards,
Silo
Locate is for finding a value ANYWHERE in a string. You want a specific location only, so use substr() instead:
SELECT *
FROM user
WHERE substr(options, 5, 1) = '1'
You could try
SELECT*
FROM`user`
WHERE options LIKE '____1%'
LIMIT 0 , 30
Another ;)
SELECT *
FROM user
WHERE instr(options, '1') = 5
Duh .. Well won't work since it only returns the first occurnace :$ But CHECK the reference,
SQLFIDDLE using Locate()
Sample data:
COL1 COL2
G11 112
G11-1 0
G11-2 2
G12-2 111
Query1:
-- to check on varchar columns
SELECT *
FROM tablex
where locate('1',col1,5)
;
Results on varchar:
COL1 COL2
G11-1 0
Query 2:
-- to check on int columns
SELECT *
FROM tablex
where locate(1,col2,2)
;
Results on int:
COL1 COL2
G11 112
G12-2 111

MySQL select bit field, select value with 0 in front

I have an row in my table from my database that contains bits, it is type bit with 4 chars long, bit(4). In this field all my value are stored like this:
0001
0010
0100
1111
I have tried to select and obtain the exact representation, with the 0 in front of 1 but without luck. I have tried to select the values from the bit field like this:
SELECT BIN(field+0) FROM `table` WHERE value=0001;
the result is: 1, how can i obtain 0001;
I need this value like so because i want to do bitwise operations in PHP. Thank you in advance.
Use LPAD() in mysql
SELECT LPAD(field,4,'0')
FROM `table` WHERE value=0001;
try this:
select right(concat(field,'0'),4) from yourtable where bcol='0001'