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

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.

Related

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 display string as date? [duplicate]

This question already has answers here:
How to convert a string to date in MySQL?
(5 answers)
Closed 5 years ago.
I have a column(string (8)) that contains data like 12032008 and I have to display it as 12/03/2008. What should I do in retrieve query?
In mysql first convert it to date object using STR_TO_DATE then to format use DATE_FORMAT
SELECT DATE_FORMAT(STR_TO_DATE('12032008','%d%m%Y'),'%d/%m/%Y')
But ideally you should not save the date or date time as strings in table it will make things complex for mysql and for you also
Demo
The most appropriate thing for you to do here would be to stop storing date information in text columns. Instead, put 12032008 into a bona fide datetime column. This will make your life easiest in the long run. If you can't do that in the short term, here are two options for you to consider.
The short way, just using base string functions:
SELECT
CONCAT(LEFT(col, 2), '/', SUBSTRING(col, 3, 2), '/', RIGHT(col, 4)) AS output
FROM yourTable;
The longer way, and probably the better long term solution, would be to first convert your date string into a date, then convert it back to a string using the formatting you want:
SELECT
DATE_FORMAT(STR_TO_DATE(col, '%d%m%Y'), '%d/%m/%Y') AS output
FROM yourTable;
Demo

Modify str in db [duplicate]

This question already has answers here:
MySQL search and replace some text in a field
(7 answers)
Closed 5 years ago.
I have a table, and in one colu,m I store comma separated strings, like:
book, table, lamp (New)
need to write query loop through all the strings and remove
(New)
I think I can do it by first running a query to fetch results containing (New), then using PHP's REGEX, remove (New) and then update the the same row with new string. However this sounds a bit convoluted. Isn't there a way to do this with a single query?
There is a replace string function in mysql: https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_replace
UPDATE <table> SET <column> = REPLACE(<column>,'(NEW)','')
Try it with select first to see if the result is what you want (you may want to remove the trailing space if new is always preceded by it)

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

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.

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,.<>="\' /:;&=-]'