Having said that, there may be times where REPEAT() works better for a particular scenario. SELECT CONCAT('Cherry', REPEAT(' ', 8), 'Blossom') AS Result Īlthough, as you can see, this requires some extra code that you don’t need to specify when using the SPACE() function. Syntax CONCAT ( expression1, expression2, expression3 .) Parameter Values Technical Details Works in: From MySQL 4. Note: Also look at the CONCATWS () function. Eg: 1, vishnu, 9961907453, 91 will return phone number as +919961907453. Definition and Usage The CONCAT () function adds two or more expressions together. Assume that you have 4 columns (id, name, phonenumber, countrycode) in a user table and you want to print phone number in this format: +countrycodephonenumber. This works similar to SPACE() except that REPEAT() allows you to specify other characters (i.e. try to use coalesce () and concat () to combine columns in the SQL query. Another Option: The REPEAT() FunctionĪnother option is to use the REPEAT() function. Plus using SPACE() makes the code easier to read. The difference is that you don’t have to type out all those characters. This has the same result as actually typing out all 8 spaces: SELECT CONCAT('Cherry', ' ', 'Blossom') AS Result To do this, I use the SPACE() function as one of the arguments to the CONCAT() function. In this case, I concatenate two strings and add 8 spaces between them (so strictly speaking, I’m actually concatenating three strings). Here’s a quick example to demonstrate how SPACE() works: SELECT CONCAT('Cherry', SPACE(8), 'Blossom') AS Result So if you need 10 spaces you’d use SPACE(10). Where N is the number of spaces you need returned. The SPACE() function is an easy way for you to return as many space characters as you need. Either way, MySQL has the SPACE() function to help you. Or maybe you need to replace a single space within the string with many spaces. Add three columns (and add a space between them) into one 'Address' column: SELECT CONCATWS (' ', Address, PostalCode, City) AS Address. Maybe you need to pad a string by adding leading or trailing spaces. Occasionally when working with MySQL databases, you might find yourself needing to add multiple space characters to a string.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |