I am trying to remove a carriage return in a MySQL Text type data field using the Update command. I've tried the following and when I export a record and view in a text editor (image attached) I still see this character? What should I use to remove this?
update fort_property_res SET property_information = TRIM(TRAILING '\n' FROM property_information
update fort_property_res SET property_information = TRIM(TRAILING '\r' FROM property_information
update fort_property_res SET property_information = TRIM(TRAILING '\t' FROM property_information
Harder than it seems as though it should be, yes? Here is a way that works. Perhaps not the best, but it can get you started.
I tried something using rlike and it did not work with my first try. It seems that it should have though. O well.
mysql> create table foo (pk int primary key, name varchar(8));
Query OK, 0 rows affected (0.62 sec)
mysql> insert into foo values (1, 'aaa\t');
Query OK, 1 row affected (0.18 sec)
mysql> select * from foo;
+----+------+
| pk | name |
+----+------+
| 1 | aaa |
+----+------+
1 row in set (0.00 sec)
mysql> update foo set name = substr(name,1,length(name)-1) where hex(name) like '%09';
Query OK, 1 row affected (0.23 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from foo;
+----+------+
| pk | name |
+----+------+
| 1 | aaa |
+----+------+
1 row in set (0.00 sec)
Related
I'm using MySQL version '8.0.28' and I'm trying to assign a default value to JSON column to one table in MySQL workbench.
Have tried this Mysql set default value to a json type column but it didn't worked out.
Any pointers or help is welcomed.
If you want a NULL to be the default, you don't need to declare that. It's the "default default" so to speak.
Here are a few different ways, tested on MySQL 8.0.29.
mysql> create table mytable (id serial primary key, j json);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into mytable () values ();
Query OK, 1 row affected (0.00 sec)
mysql> insert into mytable set j = null;
Query OK, 1 row affected (0.01 sec)
mysql> insert into mytable (id) values (default);
Query OK, 1 row affected (0.00 sec)
mysql> select * from mytable;
+----+------+
| id | j |
+----+------+
| 1 | NULL |
| 2 | NULL |
| 3 | NULL |
+----+------+
You can't set a
i have inserted a column in mysql table with backslash(\)like user\12345.But when i see in the table values i am not seeing the forward slash(\). I am just seeing user12345.
When i query the just inserted row with backslash(\) with the below possible ways, i am always getting the zero records.
select * from users where user='user12345'
select * from users where user='user\12345'
select * from users where user='user\\12345'
when i copy paste the column value into notepad+ i could see there is a carriage retun in the end of every column values. user12345CRLF, as of now i can't update/remove the carriage return. but i want to fetch the records with carriage return. how can i do this?
I don't want to use like query, and i want to query for the exact username. how can i do this?
Backslash character \ is used for escape sequence. when you insert value like 'user\12345' then by default mysql server takes it as '\1' as escape character but it is not any special character so it discard the \ from the string.
Hitesh> update test set fname='user\344';
Query OK, 2 rows affected (0.06 sec)
Rows matched: 2 Changed: 2 Warnings: 0
Hitesh> select fname from test;
+---------+
| fname |
+---------+
| user344 |
| user344 |
+---------+
2 rows in set (0.00 sec)
If you want to store \ in string then you should use \
Hitesh> update test set fname='user\\344';
Query OK, 2 rows affected (0.03 sec)
Rows matched: 2 Changed: 2 Warnings: 0
Hitesh> select fname from test;
+----------+
| fname |
+----------+
| user\344 |
| user\344 |
+----------+
2 rows in set (0.00 sec)
if you don't want to set \ as escape character then you can enable the sql mode NO_BACKSLASH_ESCAPES.
Hitesh> SET sql_mode = 'NO_BACKSLASH_ESCAPES';
Query OK, 0 rows affected (0.00 sec)
Hitesh> update test set fname='user\344';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 2 Changed: 0 Warnings: 0
Hitesh> select fname from test;
+----------+
| fname |
+----------+
| user\344 |
| user\344 |
+----------+
2 rows in set (0.00 sec)
for case of carriage return to end of column, in query you can find the string which ends with \r in where clause and select column you can skip the last character which is \r
Hitesh> set ##sql_mode='STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION';
Query OK, 0 rows affected (0.00 sec)
Hitesh> update test set fname='user344\r';
Query OK, 2 rows affected (0.17 sec)
Rows matched: 2 Changed: 2 Warnings: 0
Hitesh> select left( fname, CHAR_LENGTH(fname)-1) as fname from test where fname like '%\r';
+---------+
| fname |
+---------+
| user344 |
| user344 |
+---------+
2 rows in set (0.00 sec)
Thanks a ton #Hitesh,#Barmar again. I figured it out the query for exact match search. select * from users where user='user12345\r'
In MySQL I have some tables I need to randomize the phone numbers and Email addresses to be randomly generated for development purposes.
In MySQL how could I generate 7 digit unique random numbers for the phone numbers?
How can I generate random email address like 545165498#mailinator.com.
How can I generate this random data with MySQL Queries?
MySQL rand() Returns a random floating-point value in the range 0 <= value < 1.0.
Multiply that by another number: UPPER_BOUND and get the floor of that, and you will get a random integer between 0 and (UPPER_BOUND-1) like this:
SELECT floor(rand() * 10) as randNum;
That will give you only one random number between 0 and 10.
Change the 10 to the number one higher than you want to generate.
Something like this :
UPDATE user
SET email = CONCAT(FLOOR(rand() * 10000000),'#mailinator.com'),
PhoneNo = FLOOR(rand() * 10000000)
This should give you a random number of 7 digits length
SELECT FLOOR(1000000 + RAND() * 8999999)
And something like this should update your phone numbers and e-mail addresses according to your requirement
UPDATE Customers
SET phone = CAST(FLOOR(1000000 + RAND(8999999) AS VARCHAR),
email = CONCAT(CAST(FLOOR(1000000 + RAND(8999999) AS VARCHAR), '#mailinator.com')
MySQL Generate random data walkthrough:
Random number between 0 (inclusive) and 1 exclusive:
mysql> select rand();
+--------------------+
| rand() |
+--------------------+
| 0.5485130739850114 |
+--------------------+
1 row in set (0.00 sec)
Random int between 0 (inclusive) and 10 exclusive:
mysql> select floor(rand()*10);
+------------------+
| floor(rand()*10) |
+------------------+
| 6 |
+------------------+
1 row in set (0.00 sec)
Random letter or number:
mysql> select concat(substring('ABCDEF012345', rand()*36+1, 1));
+---------------------------------------------------------------------------+
| concat(substring('ABCDEF012345', rand()*36+1, 1)) |
+---------------------------------------------------------------------------+
| F |
+---------------------------------------------------------------------------+
1 row in set (0.00 sec)
Random letter a to z:
mysql> select char(round(rand()*25)+97);
+---------------------------+
| char(round(rand()*25)+97) |
+---------------------------+
| s |
+---------------------------+
1 row in set (0.00 sec)
Random 8 character alphanumeric string:
mysql> SELECT LEFT(UUID(), 8);
+-----------------+
| LEFT(UUID(), 8) |
+-----------------+
| c26117af |
+-----------------+
1 row in set (0.00 sec)
Random capital letter in MySQL:
mysql> select CHAR( FLOOR(65 + (RAND() * 25)));
+----------------------------------+
| CHAR( FLOOR(65 + (RAND() * 25))) |
+----------------------------------+
| B |
+----------------------------------+
1 row in set (0.00 sec)
Load a random row into a table:
mysql> create table penguin (id INT primary key auto_increment, msg TEXT);
Query OK, 0 rows affected (0.02 sec)
mysql> insert into penguin values (0, LEFT(UUID(), 8));
Query OK, 1 row affected (0.00 sec)
mysql> select * from penguin;
+------+----------+
| id | msg |
+------+----------+
| 0 | abab341b |
+------+----------+
1 row in set (0.00 sec)
Load random rows:
Make a procedure called dennis that loads 1000 random rows into penguin.
mysql> delimiter ;;
mysql> drop procedure if exists dennis;;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> create procedure dennis()
-> begin
-> DECLARE int_val INT DEFAULT 0;
-> myloop : LOOP
-> if (int_val = 1000) THEN
-> LEAVE myloop;
-> end if;
-> insert into penguin values (0, LEFT(UUID(), 8));
-> set int_val = int_val +1;
-> end loop;
-> end;;
Query OK, 0 rows affected (0.00 sec)
mysql> call dennis();;
mysql> select * from penguin;;
+------+----------+
| id | msg |
+------+----------+
| 0 | abab341b |
| 1 | c5dc08ee |
| 2 | c5dca476 |
...
+------+----------+
Update all rows in a table to have random data:
mysql> create table foo (id INT primary key auto_increment, msg TEXT);
Query OK, 0 rows affected (0.02 sec)
mysql> insert into foo values (0,'hi');
Query OK, 1 row affected (0.00 sec)
mysql> insert into foo values (0,'hi2');
Query OK, 1 row affected (0.00 sec)
mysql> insert into foo values (0,'hi3');
Query OK, 1 row affected (0.00 sec)
mysql> select * from foo;
+----+------+
| id | msg |
+----+------+
| 1 | hi |
| 2 | hi2 |
| 3 | hi3 |
+----+------+
3 rows in set (0.00 sec)
mysql> update foo set msg = rand();
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> select * from foo;
+----+---------------------+
| id | msg |
+----+---------------------+
| 1 | 0.42576668451145916 |
| 2 | 0.6385560879842901 |
| 3 | 0.9154804171207178 |
+----+---------------------+
3 rows in set (0.00 sec)
Here is an online tool to generate random data with many options. http://www.generatedata.com/
Just enter the parameters to define what kind of random data you want, and export it to the appropriate format, then you can load it.
In the following example, why does the min() query return results, but the max() query does not?
mysql> create table t(id int, a int);
Query OK, 0 rows affected (0.10 sec)
mysql> insert into t(id, a) values(1, 1);
Query OK, 1 row affected (0.03 sec)
mysql> insert into t(id, a) values(1, 2);
Query OK, 1 row affected (0.02 sec)
mysql> select * from t
-> ;
+------+------+
| id | a |
+------+------+
| 1 | 1 |
| 1 | 2 |
+------+------+
2 rows in set (0.00 sec)
mysql> select * from t where a < 4;
+------+------+
| id | a |
+------+------+
| 1 | 1 |
| 1 | 2 |
+------+------+
2 rows in set (0.00 sec)
mysql> select * from t where a < 4 having a = max(a);
Empty set (0.00 sec)
mysql> select * from t where a < 4 having a = min(a);
+------+------+
| id | a |
+------+------+
| 1 | 1 |
+------+------+
1 row in set (0.00 sec)
The HAVING clause is used to filter groups of rows. You reference min(a) and max(a) which (in the absence of any GROUP BY clause) aggregate over all a values in the table but then use a comparison against a single a value.
So which a value is MySQL supposed to use? All other RDBMSs that I know of would throw an error at this point however MySQL does allow this. From the docs
Standard SQL does not permit the HAVING clause to name any column
not found in the GROUP BY clause unless it is enclosed in an aggregate
function. MySQL permits the use of such columns to simplify
calculations. This extension assumes that the nongrouped columns will
have the same group-wise values. Otherwise, the result is
indeterminate.
So in your case from the results you are getting it appears that it ended up using 1 as the scalar value for a but this behaviour is not guaranteed and it could equally well have used 2 or any other existing a value.
I have a column in a table that contains the path for a file, along the lines of:
/here/here2/something.jpg
I need to change every row to map it to:
/here3/something.jpg
Is there an easy way to do this? Thanks!
You don't need regex for this. Simple string functions can do the trick. You could use a query like this:
update test set path=concat('/here3/',
substring(path, length('/here/here2/') + 1))
where path like '/here/here2/%';
Here is a little test case to prove it works:
mysql> create table test (path varchar(64));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into test (path) values
-> ('/here/here2/something.jpg'),
-> ('/here/here2/something-else.jpg'),
-> ('/here/here2/yet-another-something.jpg');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from test;
+---------------------------------------+
| path |
+---------------------------------------+
| /here/here2/something.jpg |
| /here/here2/something-else.jpg |
| /here/here2/yet-another-something.jpg |
+---------------------------------------+
3 rows in set (0.00 sec)
mysql> update test set path=concat('/here3/',
-> substring(path, length('/here/here2/') + 1))
-> where path like '/here/here2/%';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> select * from test;
+----------------------------------+
| path |
+----------------------------------+
| /here3/something.jpg |
| /here3/something-else.jpg |
| /here3/yet-another-something.jpg |
+----------------------------------+
3 rows in set (0.00 sec)
First of all, are you familiar with using PHP? You could write a script to get all of the entries from the database, and then change them using preg_replace(). Then you could use mysql to update the database.