In this small article, i would like to share with you how to extract the first word of string in sql query. i did that using SUBSTR, LOCATE and If statement.
Yesterday, i was working on my laravel project and i require to get first name from fulname column like as example:
Hardik Savani is a full name but i just need to get first work "Hardik" from fullname of users table in phpmyadmin. So i can consider First name from fullname. So basically here also malke it possibility to if just one word then what should it returns. But i use If for that.
For getting first name i use SUBSTR, LOCATE and If condition, So here i made sql query for getting firstname from fullname column.
id, IF((SUBSTR(fullname, 1, LOCATE(' ' ,fullname))) = '',fullname, (SUBSTR(fullname, 1, LOCATE(' ' ,fullname)))) as firstname
As write above query, you can simply get first name from fullname.
i hope you found your best solution...
Do you like below Tutorials ?
- PHP Laravel 5.5 - import export data into excel and csv using maatwebsite
- Laravel 7.x and 6.x create custom facade example from scratch
- Node JS - Get Real time online users counter using socket io example
- PHP Laravel 7.x and 6.x - Bootstrap Admin Theme Integration example from scratch
- MySQL Query - How to select records of today to next Sunday ?
- Laravel 7.x and 6.x - username custom validation example
- Laravel 5.5 - Import Extract zip file using chumper/zipper package
- PHP Laravel Set Custom Validation Error Messages Example