how to take first 3 character from a string using PHP [duplicate] - mysql

This question already has answers here:
MySQL Select Query - Get only first 10 characters of a value
(3 answers)
Truncate a string to first n characters of a string and add three dots if any characters are removed
(20 answers)
Closed 6 years ago.
I need to take only three characters from the string stored in MYSQL and output must be the first 3 character and the login date of customer
I am beginner kindly help me to code in PHP.

SELECT LEFT(string , 3) , logindate FROM tbl;
Hope this will help you.

Related

REGEXP in MYSQL isn't working for full word search all time [duplicate]

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
What is a word boundary in regex?
(13 answers)
Closed 2 years ago.
I've a table like this,
values
-------------------
male,female
PHP 30
PHP (30)
PHP ($30),CSS ($20)
PHP {$30},CSS {$20}
PHP [$30],CSS [$20]
PHP (€30),CSS (20)
I've prepared the sql like this,
SELECT mytable.values FROM mytable WHERE mytable.values REGEXP '[[:<:]]PHP ($30)[[:>:]]'
It should match one row (4th), but it's not working.
It's also not working with
() / {} / [] or any types of symbols like $ / €
I want to mention here that,
MYSQL LIKE isn't the right thing for me I think, because it won't work if we want to match male.
REGEXP is working for PHP 30 here and it's working for only strings.
Could anyone please inform me what will be the best solution to match the exact word or sentence?
Thanks in advance.

Find position of word in string [duplicate]

This question already has answers here:
Locate text position, extract text and insert in new column in MySQL
(2 answers)
Closed 2 years ago.
I have string
good morning what a lovely day today
I need to find the position of 'lovely'. Query should return the result 5.
You can use find_in_set() for this:
select find_in_set('lovely', replace('good morning what a lovely day today', ' ', ','))
Yields:
5
From the documentation:
FIND_IN_SET(str,strlist)
Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. A string list is a string composed of substrings separated by , characters.
So basically the logic is to replace space characters with commas, and then use that function.

How to change date format into the comma separated column? [duplicate]

This question already has answers here:
MySQL Second (or third) Index Of in String
(5 answers)
MySQL: select first element of a comma-separated list
(2 answers)
Closed 4 years ago.
I have comma separated value like 2018-06-20 01:12:53,2018-06-20 01:14:11
But the problem is I need date and time format like day-month-Year H:i when I use DATE_FORMAT(t1.pickup_date_time, "%d-%m-%Y %H:%i")
it could not run because the column(pickup_date_time) has comma separated value.
Your help is appreciated.
Thank you.
Use SUBSTRING_INDEX first to split the column on the comma. Then use STR_TO_DATE to reformat the date. If you have issue with the actual code, let me know.

Is there any function in MySQL for Regex Replace? [duplicate]

This question already has answers here:
How to do a regular expression replace in MySQL?
(13 answers)
Closed 6 years ago.
I have a table which needs stores some name and i need to replace a few characters before comparing them with another string
For instance, My table data is
abc
ghi:dki
ioe dsa
i read a string from user, which is of the form abc, ghi-dki, ioe-dsa. ie, all blankspaces, multiples spaces and symbols are converted to a hyphon(-). Now i need to compare. something like
SELECT MYCOLUMN FROM MYTABLE WHERE {Converted MYCOLUMN} = 'ghi-dki'
Can someone help me for figuring out which MySQL function can do it?
You can't do a regex replace in MySQL, but you can do a match.
SELECT mycolumn FROM tablename WHERE mycolumn REGEXP Replace('ghi-dki', '-', '^[\s:_-]*$');
Note: I didn't completely fill out the symbols character set, you'll have to add whatever you're using.

mySql - find non-Ascii character in html [duplicate]

This question already has answers here:
How can I find non-ASCII characters in MySQL?
(10 answers)
Closed 9 years ago.
I'm trying to find all non-asci characters I have in my DB in a specific table and column. In that column are stored Html description, and in some of them I've exotic or non-existing characters (for example: Hà¶ganà¤s ).
I'm triyng to match them with this query:
SELECT * FROM project_version WHERE description REGEXP '[^()\x00-\xFF\,\.-\<\>="\' /:;&=]'
But I think I'm missing something, cause it returns all of my records. Does anyone any advice?
Thanks in advance
Try moving hyphen to start or end otherwise it needs to be escaped also ^ will be treated as literal ^ in character class:
SELECT * FROM project_version WHERE description REGEXP '[()\x00-\x7F,.<>="\' /:;&=-]'