MySQL Select Query - Get only first 10 characters of a value - mysql

Ok, so here is the issue.
I have a table with some columns and 'subject' is one of the columns.
I need to get the first 10 letters from the 'subject' field no matter the 'subject' field contains a string with 100 letters.
For example,
Table - tbl.
Columns - id, subject, value.
SQL Query:
SELECT subject FROM tbl WHERE id ='$id';
The result I am getting is, for example
Hello, this is my subject and how are you
I only require the first 10 characters
Hello, thi
I can understand that I can remove the rest of the characters using php substr() but that's not possible in my case. I need to get the excess characters removed by MySQL. How can this be done?

Using the below line
SELECT LEFT(subject , 10) FROM tbl
MySQL Doc.

SELECT SUBSTRING(subject, 1, 10) FROM tbl

Have a look at either Left or Substring if you need to chop it up even more.
Google and the MySQL docs are a good place to start - you'll usually not get such a warm response if you've not even tried to help yourself before asking a question.

Related

Mysql query to find only exact value?

I have value in my db
like
1,12,13,25,44,414,2114
I have to find exact 14 from the db.
but it also return the value 414 and 2114
but i want exact 14.
How can i achieve this through sql query Please help
i have tried this but didn't worked.
Select * from tb_name where columnNAme like '%value%'
If you want search exactly 14, then you need search %,14,%, but 14 may appears at start or at end of string, so you need add commas to column also.
Well, you can use:
select * from tb_name where concat(',',columnNAme ,',') like '%,14,%'
Side note, comma separated values in one column is bad database design
Select * from tb_name where columnNAme like 'value'
% - The percent sign represents zero, one, or multiple characters
SELET * FROM database WHERE column LIKE 14 should work fine, just double checked it on my database.
If this doesn't work can you show your query please?
Above answer is correct, I didn't see your query before I posted this.

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!!

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: How to replace all characters in a field except the last 4 chars? (to protect credit card #'s)

I've got a mySQL database table called "booking", which contains a column of credit card numbers. Obviously this is not good or secure, and I really only need the last 4 digits in that field. I need a mySQL call that will fix this mistake.
Is there a way have one SQL statement that could go through the entire database table and replace all the Visa, Mastercard, Discover card #'s (which are different lengths), and overwrite all but the last 4 chars with XXX's. Or would I need to write something that loops through each record to do the replace?
The table is called "booking", and the field is called "creditcardnumber"
Try like this
SELECT
CONCAT(
REPEAT('X', CHAR_LENGTH(card_number) - 4),
SUBSTRING(card_number, -4)
) AS masked_card
FROM
table_name ;
Check on this reference: * SQLFIDDLE.
Don't mind the table schema, it's just the sample old data. I changed the first record to a credit card number (duh - it's not starting with 5, 4, or 3 ;) but LAPD does the job.
Query:
select right(hasha,4),
lpad(right(hasha,4),length(hasha),'x')
from actors
where id = 1
;
results:
RIGHT(HASHA,4) LPAD(RIGHT(HASHA,4),LENGTH(HASHA),'X')
3456 xxxxxxxxxxxx3456

Limit characters for mysql_query(select) lookup

Is there an efficient way to limit the number of characters for a mysql query lookup? So, if a text column field had 18000 characters, but I only need to query 500 characters, is there a way to mysql_query("select text_column from random_table limit_char_count 500")?
How about using the SUBSTRING() function?
Below example selects 4 characters starting from 1st position
SELECT SUBSTRING(text_column,1,4)
FROM random_table
WHERE something = something else
EDIT - edited based on - for all forms of SUBSTRING(), the position of the first character in the string from which the substring is to be extracted is reckoned as 1.