MySQL query to prepend character to each entry - mysql

I have a table of users which has a username column consisting of a six digit number e.g 675381, I need to prepend a zero to each of these usernames e.g. 0675381 would be the final output of the previous example, is there a query that could handle this?

UPDATE Tablename SET Username = Concat('0', Username);

what type is the column of?
if it's string type, try something like this:
UPDATE your_table SET column_name=concat('0',column_name);

You mean "prepend" ? i.e. add it on the front?
Is the column numeric? Do you always want 7 characters output?
Assuming that, something like this would work for a query:
select LPAD(CONVERT(username, CHAR), 7, '0')
If the column is characters, the CONVERT() part is unnecessary, just LPAD the username.
If you want to permanently modify the value in the table, you'll need to ensure the column is a character type and UPDATE using the above.

You might want to use CONCAT_WS('', '0', Username) because if there is a null value, then you'll end up with NULL instead of '0'. This probably isn't a problem, but something I've learnt the hard way.

Related

mysql - search pattern and replace

I have MySQL table wp422_posts and I need to manipulate one type of values in post_name column. Actually, I need to manipulate long value beginning with 4 digits (unique ID) and than text, I need to get the 4 digits and left them there. Could anybody help me, please? I'm new to MySQL. Other values - which do not start with 4 digits and dash has to stay same.
one example of value of post_name
old:
"2147-sprava-uzivatelskych-uctu-databaze-oracle-v-prostredi-autocad-map-3d"
desired
new one: "2147"
How to select correct values in column:
SELECT * FROM `wp422_posts` WHERE `post_name` LIKE "[0-9][0-9][0-9][0-9]%"
it returns empty result, but why?
How to get rid of not needed part?
Thanks relly much for your help.
If you are sure that starting part is always 4 digit number and that is only needed, then you can use this:-
SELECT LEFT(COL_NAME, 4)
FROM `wp422_posts`
Then you can use update statement to update the table. Somthing like:-
UPDATE `wp422_posts`
SET COL_NAME = LEFT(COL_NAME, 4)
Hope this helps!!

How do I make extra white space affect my MySQL query result?

I'm using MySQL 5.5.37. If I run the following query on my database
select count(*) FROM user where user_name = 'admin';
I get back the result "3". However, when I execute the query
select count(*) FROM user where user_name = 'admin ';
, (notice the extra spaces after the word "admin", I also get back the result "3". In fact, when i look at teh records, they are the same records as the first query. However, there are no records in my databaes with the user_name field equal to "admin ". Given that I cannot upgrade my MySQL database at this time, what can I do to ensure that the second query (wiht the extra white space) returns the correct result (i.e. "0")?
Edit: The user_name field is of type varchar(50).
I'm not 100% sure that it's the case, but it looks like your user_name field is a char(n) field and not varchar(n).
Such fields are filled with whitespaces to fully match the length of the field, while varchars are (I believe) null terminated strings.
Both queries return the same answer, as they also fill the missing characters with whitespaces.
If you want the query to work properly, consider changing the type of the column to varchar.
You could try using a regular expression as described in this post? Query for exact match of an string in SQL
select count(*) FROM [sb_user] where user_name = 'admin ';
you may try this one

update specific column of mysql table

I have a quite big table in mysql and I need to change all the records related to this column.
records are like this :
/name/nm0000209/?ref_=ttfc_fc_cl_t1,
/name/nm0000151/?ref_=ttfc_fc_cl_t2,
...,
/name/nm0104594/?ref_=ttfc_fc_cl_t10
what I want is to keep only the string in the middle which is nm0000209, nm0000151,.... I know how to delete specific characters from the right or left of the words by REPLACE or Trim , .., but my problem is that in this case the number of characters in the third part of string are not equal (as you see when it reaches to 10, I have to delete 21 characters from the end instead of 20 characters and since this table contains lots of records I dont know how to do it.
I reaaly appreciate if someone could helop me,
thanks
I want is to keep only the string in the middle which is nm0000209, nm0000151...
You can use 'SUBSTRING_INDEX' on the column to crop part of the column value.
Following example assumes that the said column will have 'name/' as starting pattern.
Example:
update table_name
set column_name = substring_index(
substring_index( column_name, 'name/', -1 )
, '/', 1 );
The same can be used for updating with the same value.
Demo # MySQL Fiddle
One approach would be to use MYSQL's SUBSTRING_INDEX function. It would let you get whatever's after the last slash. Or after the second to last.
For your particular case
select
SUBSTRING_INDEX(SUBSTRING_INDEX(thefield,'/',-2 ),'/', 1)
from supertext
would yield the desired result
EDIT: for update purposes
UPDATE thetable
SET thefield=SUBSTRING_INDEX(SUBSTRING_INDEX(thefield,'/',-2 ),'/', 1)

MySQL Select when specific digit is x

I have a column that contains values like 5A898, 89KAS, 89ASD.
I'm trying to write a query that will only return rows where the third digit of the column is 'A'. For instance '89ASD' would be returned but '89KAS' would not. I'm not sure what the right way to go about this is. Regular expressions?
So...
SELECT column
FROM table
WHERE column = ?
WHERE column LIKE '__A%'
^^-- 2 underscores
should do the trick. two "whatever" characters, followed by an A, then followed by anything in any amount.
Maybe you can use MySQL's SUBSTRING
SELECT column
FROM table
WHERE SUBSTRING(column,3,1) = 'A'
where right(left(col,3),1) = 'A'
That can help... MarcB's answer is cleaner
You can do this with a regex, but I think it might be easier just coping with a string operation here:
SELECT column
FROM table
WHERE SUBSTRING(column,3,1) = 'A';

MySQL column names and aliases

I have read that after select we use column-names but I have found a statement that was like this:
SELECT 'A' FROM T WHERE A = NULL;
would you lease help me? thanks (A is a column- name here?)
my DBMS is MySQL
EDITED : the exact question is this that:
Will the above statement produce a row (select all that
apply)? Notice that ANSI_NULLS is OFF.
I want to know that the above statement will work? because some of you said that we should write IS NULL instead of =null
Based on that query, you would get a result set containing the character 'A' for each row where the column named A was equal to null.
If you actually want to see the value of the column A instead of the character 'A', you have to remove the single quotes:
SELECT A FROM T WHERE A IS NULL
Either way, you should not use = NULL. Certain RDMSs don't handle that the way you would think. The standard is to use IS NULL instead.
You should use
SELECT 'A' FROM T WHERE A IS NULL;
There are three types of quotes in SQL.
The single quote ' means that something is a string literal. 'A' in this instance means that it returns the character A for all rows where the column A is NULL.
The double quote " means that something is an identifier. This is useful if the identifier has the same name as a reserved word, like select. Example: SELECT "select" FROM T selects the column select from the table T.
The backtick quote ` works only in MySQL, and is the same as the double quote. The double quote can sometimes used for string literals in MySQL, although this is very much against the standard. MySQL has an option to conform to the standard, using SET SQL_MODE='ANSI'; where the backtick becomes invalid, and you need to use the single and double quotes instead.
An identifier without quotes is the same as an identifier with double quotes, unless it's a reserved word.
Hope this helps understand a bit more.
In answer to your question:
A = NULL is always false, so you will get no rows returned. To compare with NULL you must use A is NULL instead.
NULL is special in SQL, in that it is not equal to anything, even itself. Yep, (NULL = NULL) evaluates to false.
If you change it to IS NULL, then you will get a set of rows with one column, containing the character 'A' in each row. You will get one 'A' for each row in the table T where the A column is null.
You will get the letter A and not the value of the column because you have quotes around the 'A'. If you remove them, you'll get the value of A in each row (which will be null, because those are the rows you're selecting with your where clause).
If you wanted to see which rows in T had a null value for A, then you should change it to select * from T where A is null
Your SELECT statement has the following meaning:
"For every row of the table called T, return the string 'A' if the column A of the table T is NULL"
So, if you have 3 records where A is NULL, the output will be:
A
A
A
3 row(s) selected
The correct syntax is WHERE A IS NULL, and not WHERE A = NULL.
Have you tried running it on your test database to see what it does? Or was this just in reading?
Breaking down that statement, what is says is:
In the table T (FROM T), find the rows where the value of A is null (WHERE A = NULL).
For each of those rows, return an 'A'.
The result I would expect is
+--+
|T |
+--+
|A |
|A |
...
|A |
+--+
If the statement was instead:
SELECT A FROM T WHERE A = NULL;
Where the single quotes are removed, it would return a bunch of nulls, the value of the column A.
A is a column name, but you probably don't want single-quotes around it. I'd try...
SELECT A FROM T WHERE A IS NULL;