How to do MySQL + Substring? + replace? - mysql

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)

Related

Deleting all rows which contain a unique value

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.

how to "ident_current" in mysql?

I use this code in MSSQL:
SELECT IDENT_CURRENT('customers');
When I try it in MySQL it doesn't work. I looked for answers on the net, but I could not find anything that worked for me. What's the MySQL equivalent for the above TSQL?
I think you are looking for this:
SELECT LAST_INSERT_ID('customers');
But LAST_INSERT_ID() not in all cases true, so better use:
SELECT MAX('id') FROM customers;
in MySQL, you can use LAST_INSERT_ID(expr)
LAST_INSERT_ID(expr)
I donĀ“t know if you still need it but it may help someone. :)
I was facing the same problem of needing to get the next id (auto_increment number) from my table without having to add a new row and without using MAX() or LAST_INSERT_ID() because it would only return the last visible record and not the real next auto_increment.
The solution I found was to get the auto_increment from the table_schema, like this:
SELECT AUTO_INCREMENT
FROM information_schema.TABLES
WHERE (TABLE_NAME = 'your_table')
Hope it help someone, like it helped me.
*Sorry the bad english, I'm from Brasil.
SELECT LAST_INSERT_ID();
To learn more about this function: http://dev.mysql.com/doc/refman/5.6/en/information-functions.html#function_last-insert-id
In order for this to work as intended, the 'ID' column must be set to "UNIQUE".
If you are using some sort of Identity(incrementing IDs) column the following should work;
Select top 1 ID from TBLNAME
order by ID DESC

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);

mySql new rows can only be searched with LIKE, has anyone had this before?

A bit of background
I'm running mySQL on a mac
I have a few databases setup that have been working okay
I have recently created a new table from a sqlDump from another server.
I am having an issue with new rows that equal = a value that I know exists
e.g. Table setup
id=1 name='dave' - already exists in database
id=2 name='john' - I add a new row
Following are the sql I tried with results...
Select * from tablename where id=1 -- I get the correct Dave result.
Select * from tablename where `name` = 'dave' -- I get the correct Dave result.
Select * from tablename where id=2 -- I get the correct John result.
Select * from tablename where `name` like 'joh%' -- I get the correct John result.
Select * from tablename where `name` = 'john' -- (No result!) eek!
Anyone seen this before? it's in the database but a direct match on the name field is not yielding a result.
Thanks in advance
M
One possibility: there could be a trailing space after 'john' in the name column.
One way to check that:
select `name`,char_length(`name`), char_length('john')
from tablename
where id = 2
An easy way to not have to deal with that problem would be to trim your input (if you don't expect to ever have preceding or trailing white space.
In that case you could have a query like:
Select * from tablename where trim(`name`) = trim('john')
I agree with the comments on your question, that it is most likely a hidden space or something similar. If you include the column definitions so we can check the data that your using with the types we could help more. Remove the entry and and retry with a different name other than john and see if you can replicate the problem.