MYSQL Replace Statement using _ (UnderScore) or % - mysql

I need to Update a Column using Replace Query but I won't know 10 characters and i need to fill that up with _ just like in LIKE statements
UPDATE USERS SET LASTONLINE = REPLACE(LASTONLINE, "User1:__________", "User1:1620647000") WHERE NUMBER = '9988776655'
I'm trying to store timestamp of when the user was last online. I cannot change the table structure for many reasons which makes the question bigger. I just want to replace the old Timestamp which i won't know with the new one. I'm sure this can be done but having trouble with the logic. Any help is appreciated
Structure
NUMBER | LASTONLINE
9988.. | User1:<timestamp>,User2:<timestamp>

If you want to set a timestamp value, set it to a timestamp value. For instance:
UPDATE USERS
SET LASTONLINE = NOW()
WHERE NUMBER = '9988776655';
I don't know what timestamp value you want to set it to. This just uses the current time.
I have no idea why you are trying to set a timestamp to a string, but that won't do anything useful.

Assuming your LASTONLINE field contains a string of number of users1,2,3,4...
and from your example query you would know which user to update as well as the number being updated to, you can use subtring get the substring of string before the User (User1 in your case in your example), concatenate with desired string, and get the substring from the position of User1 plus 16 spaces which means string after the "User1: plus 10 digits").
update USERS
set LASTONLINE = concat(substring_index(LASTONLINE , 'User1:', 1),
'User1:1620647000',
substr(LASTONLINE,LOCATE('User1:',LASTONLINE)+16))
where NUMBER = '9988776655';

Related

MySQL returns a date format different that it has been setted

i have a MySQL's db with 2 DATE columns and when i add a registry the date is saved like this 2018/07/30. The problem is that when i make a get call to the db the value comes like this"2018-07-30T03:00:00.000Z"
I do not need the time part, and i think that the DATE type i have set to the column should not return this kind of value.
I have tried this but it does not work:
"SELECT * FROM patrones WHERE DATE_FORMAT(bd_fRecepcion, '%Y-%m-%d') WHERE bd_SI = 'Si' "
How could i do to get retuned the date part only?
Thanks for your help.

Appending a "0" to the front of a string

Someone in my company downloaded some data, played with it in Excel and uploaded again.
Excel when trying to be helpful truncated a leading zero on a file called license_number.
As a result rather than having "037463524" the data now says "37463524"
I know that if the string is eight characters long, I need to add a "0" to the front of it to correct the mess.
Is there a SQL query that I can run in order to accomplish this?
You can use LENGTH()
UPDATE Tablename SET license_number = '0' + license_number WHERE LENGTH(license_number) = 8
or
UPDATE Tablename SET license_number = CONCAT('0', license_number) WHERE LENGTH(license_number) = 8
One more way by using LPAD
UPDATE `TABLE` SET `Lic_NO` = LPAD(`Lic_NO`, 9, '0')

MySQL Query where id = id_product error

i'm working with mysql in a nodejs web app. I don't understand why when I ask for some id (key) it gives me more than 1 result.
When I:
SELECT * FROM products WHERE id = 1;
This happens, I get 3 results, but I only want 1:
1, 001 and 0000001.
I just want the info of one product (id: 1 in this example)
How can I fix this?
ID type is varchar(20)
If I use LIKE instead of = my result changes:
SELECT * FROM products WHERE id LIKE 0000001;
I get the info of id = 1 instead 0000001. Don't know why.
Thanks
The WHERE clause of your query contains a comparison of a literal numeric value with a string (column id).
When it needs to compare values of different type, MySQL uses several rules to convert one or both of the values to a common type.
Some of the type conversion rules are not intuitive. The last rule is the only one that matches a comparison of an integer number with a string:
In all other cases, the arguments are compared as floating-point (real) numbers.
When they are converted to floating-point (real) numbers, 1 (integer), '1', '0001' and '0000001' are all equal.
In order to get an exact match the literal value you put in the query must have the same type as the column id (i.e string). The query should be:
SELECT * FROM products WHERE id = '1'
The problem is that you are looking by a varchar type using an integer cast.
Try to add quotes to the id parameter:
SELECT * FROM products WHERE id = '1';
If you want to add integer ids with with leading zeros, I recommend you to use the zerofill option:
https://dev.mysql.com/doc/refman/5.5/en/numeric-type-attributes.html
If you want to use use alphanumeric values then keeps the ID type as varchar, but remember to enclose the search param into quotes.
Numbers in MySQL (and the real world) don't have leading zeros. Strings do.
So, you just need to make the comparison using the right type:
SELECT *
FROM products
WHERE id = '1';
What happens with your original query is that the id is converted to a number. And '1', '001' and '0000001' are all converted to the same integer -- 1. Hence, all three pass the filter.

SQL - Strip first digit if it starts with the number '9'

Basically I have a table called 'Telephone' with over 100,000+ records.
In this database I have numbers such as:-
90123456789
91234567893
97126372319
Basically I want to Update the Telephone.DB and SET the numbers so it strips the first number only if it starts with the number '9', so the result would end up as follows:-
0123456789
1234567893
7126372319
Any idea how I can achieve this using MySQL?
Provided that the datatype is a string type, this should do it;
UPDATE numbers
SET number = SUBSTR(number, 2)
WHERE number LIKE '9%';
An SQLfiddle to test with.
As always, test yourself before running updates from random people on the Internet on your production database :)
Ok, this works for a VARCHAR column. You could always convert on the fly the value to varchar:
SELECT CONCAT(REPLACE(SUBSTR(PHONEFIELD,1,1),9,''), SUBSTR(PHONEFIELD,2)) AS PHONEWITHOUTNINE FROM TABLENAME
Check it out and tell me if it works.

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