Appending a "0" to the front of a string - mysql

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

Related

MYSQL Replace Statement using _ (UnderScore) or %

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

MySQL - IF Query with STR_TO_DATE

Greetings Stackian Overflowers,
I'm trying to overwrite data in an existing column and do a STR_TO_DATE in the case it is already set.
Here's the code :
SELECT IF(columnA = '../../..' , '0000/00/00' , STR_TO_DATE(columnA, '%m/%d/%Y)
FROM tablename
WHERE extract_date = '2018-12-31';
In all reality, I must use this logic to make an update on the table. I tried this :
UPDATE tablename
IF columnA = '../../..' THEN
SET columnA = '0000-00-00' ELSE
SET columnA = STR_TO_DATE(columnA,'%m/%d/%Y);
Could use any help on the matter.
MySQL's REGEXP operator comes in handy here:
UPDATE tablename
SET date_col = CASE WHEN columnA REGEXP '[0-9]{2}/[0-9]{2}/[0-9]{4}'
THEN STR_TO_DATE(columnA, '%m/%d/%Y)
ELSE NULL END;
It doesn't make sense to update columnA from a string to a date, because the types don't match. A better strategy is to create a new column to store the date. Also, I recommend using NULL, not 0000-00-00, to represent a date string that could not be parsed.
Note that the regex I used is not foolproof and does not completely validate your date strings. But it should at least weed out string data which is really off.

MySQL: can anyone explain about using = to select not null rows

I'm a beginner MySQL user.
My teacher gave me a question to explain how this function works.
SELECT *
FROM TableName
WHERE ColumnName=ColumnName
Then, the result shows the rows that contain values in that column. (The null value is not appear)
I have no idea about it. I do searching for answer but most of it is talking about using IS NOT NULL.
You need to use ' = ' to select rows by checking an empty column right ?
If that's the case
You can simply use the below code
SELECT *
FROM TableName
WHERE ColumnName = ' '
The expression:
WHERE ColumnName = ColumnName
is comparing two values from the same column. This should be true in all cases, except when ColumnName contains a NULL value. So, you can equivalently write this as:
WHERE ColumnName IS NOT NULL
This version is more understandable and the preferred way to write the logic.

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

update a column by subtracting a value

I'm trying to come up with a MySQL query that will update points... Can I do something like this?
UPDATE `a75ting`.`username` SET `points` = '`points` - 5'
UPDATE a75ting.username
SET points = points - 5
by putting the single quotes around the "points -5", you converted that expression into a plaintext string. Leaving it without the quotes lets MySQL see you're referring to a field (points) and subtracting 5 from its current value.
Run this query to find out the difference:
SELECT '`points` - 5' AS string, `points` - 5 AS expression
FROM a75ting.username