Deleting all rows which contain a unique value - mysql

I have a database and my dataset is really messed up. The column of importance is the a "uniqueidentity" number where some records have "&&" or "%%" in contained at the end of the value. If it does, I would like to delete the entire row from the table. uniqueidentity = VARCHAR
Does anybody have any ideas on how to do this using a SQL Query?
Thanks in advance

you could use
DELETE FROM table WHERE RIGHT(uniqueidentity, 2) = "&&" OR RIGHT(uniqueidentity, 2) = "%%"

know thy sql commands young one and all troubles will begone
sql delete result from query
http://www.cs.utexas.edu/~mitra/csFall2013/cs329/lectures/sql.html
SQL - how to select words with certain values at the end of word

Try like this:
DELETE FROM tableName WHERE
uniqueidentity REGEXP '%%$' OR
uniqueidentity REGEXP '&&$';
I have created supporting SQL FIDDLE with select query which you can change it to delete as above.

Related

How to replace the first character of a column in sql

I have a table and in that I want to sort a column in that table. But I’m unable to get the sorted order because there are different kinds of characters in the first place of the string. Some of them have horizontal tab, some of them have spaces, and some of them are empty. I have tried trim method but it doesn’t work for me. I have nearly 200000 records in that table. I cant update the table. I need a select query which should give the result neglecting all the unnecessary things and should sort the columns.
I think this might work
SELECT *
FROM table_name
ORDER BY IF(coloum_name LIKE "_%", substr(coloum_name, 1), coloum_name);
Explanation:
We use the IF function to strip the first character from the beginning of the string before returning the string to the ORDER BY clause. For more complex rules we could create a user-defined function and place that in the ORDER BY clause instead. Then you would have ...ORDER BY MyFunction(coloum_name)
For SQL Server, do this:
SELECT *
FROM table_name
ORDER BY CASE WHEN coloum_name LIKE "_%" THEN substr(coloum_name, 1) ELSE coloum_name END

How to replace value of column with MySQL Query

I try to make some changes to my MySQL database, and I need to change values of this col name "id_lang" which values now is "6" to change to value "2".
This col is found in many tables, it will be great to have a single query which will to this for all DB at once.
by now i found this query,
SELECT REPLACE(yourcolumn,'ValueInTheColumnTobeReplaced', 'NewValue') as replacedColumnName FROM yourtable
PS: I use PHPMYADMIN
but i cant make it work... PLease help me!!!
You have to use an Update query.
But you can not do this in one query for all tables.
Update yourTable set id_lang=2 where id_lang=6
Use this query
UPDATE yourTable SET id_lang=2 WHERE id_lang=6;

How to do MySQL + Substring? + replace?

I am not very good with SQL and would like to be able to become better.
I am having some trouble trying to preform a certain table manipulation.
I would like to be able to select the substring out of the ProgUID column below
something like...
SUBSTRING(table.ProgUID,3,12);
which would give me CAMVE-9701 for the ProgUID P-CAMVE-9701-1 (removing the P- from the beginning and the -1 from the end), and then insert the substring into that rows UID.
I assume this should be fairly easy, and I have been trying to figure it out but havent had much luck.
If there is a better approach please let me know!
Thanks in advance for your thoughts / help!
use UPDATE statement
UPDATE tableName
SET UID = SUBSTRING(ProgUID,3,12)
See SQLFiddle Demo
If the portion you want is always 12 characters, then
UPDATE table
SET UID = SUBSTRING(ProgUID, 3, 12)
otherwise
UPDATE table
SET UID = SUBSTRING(ProgUID, 3, LENGTH(ProgUID)-2)

mysql query based on length of string in a column

I am trying to query a table in mysql based on the length of a string in a specific column. I know mysql has a function called LENGTH(), but that returns the length of the string. I want to be able to pull data based on the result of the LENGTH() function.
Example:
SELECT * table WHERE LENGTH(word) = 6
of course that does not work. I read through http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function%5Flength but could not find anything to help me.
yes I could make something in PhP to accomplish this, but I would like to do it at the query level.
Any help?
Try:
SELECT *
FROM table
WHERE LENGTH(RTRIM(word)) = 6
I believe you wanted to use query SELECT * FROM tableName WHERE LENGTH(word) = 6; (assuming that the word is name of column in tableName).
This is very unfortunate solution on large tables, you should create new column and use UPDATE tableName SET wordLength = LENGTH( word).

mysql datetime update to one column

I have made a really silly mistake of having a seperate date and time field in my DB. I really need to put these together and just have one field (datetime) Luckily these are individually in the corect format. Is there a way I can di an SQL statement to take the date, time (with a space inbetween) and input these in to the new datetime field? I have around 3000 records in this table so really dont want to do it by hand.
Thanks
Richard
run:
UPDATE dates SET datettime_field = CONCAT(date_field,' ', time_field);
You can use CONCAT_WS to do a string concatenation:
UPDATE table
SET newfield = CONCAT_WS(' ', oldfield1, oldfield2);
Another solution is:
update table set datetime_field = addtime(date_field, time_field);