In the below piece of code, I am creating an Address field by concatenating various parts of an address.
However, if for instance address2 was empty, the trailing , will still be concatenated into Address.
This means if all fields were empty, I end up with a result of ,,,,.
If address1 is "House Number" and everything else is empty, I end up with House Number,,,,.
CONCAT( COALESCE(address1,'') , ', ' ,
COALESCE(address2,'') , ', ' ,
COALESCE(address3,'') , ', ' ,
COALESCE(city,'') , ', ' ,
COALESCE(zip, '')
) AS Address,
Is there some way of conditionally placing the commas between address parts only if the content of an address part is not empty.
Such as something along the lines of (pseudo-code) IF(address1) is NULL use '' ELSE use ','
Thank you.
CONCAT_WS(', ',
IF(LENGTH(`address1`),`address1`,NULL),
IF(LENGTH(`address2`),`address2`,NULL),
IF(LENGTH(`address3`),`address3`,NULL),
IF(LENGTH(`city`),`city`,NULL),
IF(LENGTH(`zip`),`zip`,NULL)
)
Using CONCAT_WS as Mat says is a very good idea, but I thought I'd do it a different way, with messy IF() statements:
CONCAT( COALESCE(address1,''), IF(LENGTH(address1), ', ', ''),
COALESCE(address2,''), IF(LENGTH(address2), ', ', ''),
COALESCE(address3,''), IF(LENGTH(address3), ', ', ''),
COALESCE(city,''), IF(LENGTH(city), ', ', ''),
COALESCE(zip,''), IF(LENGTH(address1), ', ', ''),
) AS Address,
The IF()s check if the field has a length and if so returns a comma. Otherwise, it returns an empty string.
try with MAKE_SET
SELECT MAKE_SET(11111,`address1`,`address2`,`address3`,`city`,`zip`) AS Address
It will returns a string with all NOT NULL value separated by ,
CONCAT_WS(', ',
NULLIF(`address1`,''),
NULLIF(`address2`,''),
NULLIF(`address3`,''),
NULLIF(`city`,''),
NULLIF(`zip`,'')
)
CONCAT_WS combines non-NULL strings.
NULLIF writes NULL if left and right side are equals. In this case if values are equals an empty sting ''.
Related
I know that I can combine multiple columns in mysql by doing:
SELECT CONCAT(zipcode, ' - ', city, ', ', state) FROM Table;
However, if one of the fields is NULL then the entire value will be NULL. So to prevent this from happening I am doing:
SELECT CONCAT(zipcode, ' - ', COALESE(city,''), ', ', COALESCE(state,'')) FROM Table;
However, there still can be situation where the result will look something like this:
zipcode-, ,
Is there a way in MySQL to only have to comma and the hyphen if the next columns are not NULL?
There is actually a native function that will do this called Concat with Separator (concat_ws)!
Specifically, it seems that what you would need is:
SELECT CONCAT_WS(' - ',zipcode, NULLIF(CONCAT_WS(', ',city,state),'')) FROM TABLE;
This should account for all of the null cases you allude to.
However, it is important to note that a blank string ('') is different than a NULL. If you want to address this in the state/city logic you would add a second NULLIF check inside the second CONCAT_ws for the case where a city or a state would be blank strings. This will depend on the database's regard for the blank field and whether you are entering true NULLS into your database or checking the integrity of the blank data before you use it. Something like the following might be slightly more robust:
SELECT CONCAT_WS(' - ', zipcode, NULLIF(CONCAT_WS(', ', NULLIF(city, ''), NULLIF(state, '')), '')) FROM TABLE;
For more, check out the native documentation on concat_ws() here:
https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat-ws
I think you need something like this
Set #zipcode := '12345';
Set #city := NULL;
Set #state := NULL;
SELECT CONCAT(#zipcode, ' - ', COALESCE(#city,''), ', ', COALESCE(#state,''));
Result: 12345 - ,
SELECT CONCAT(#zipcode, IF(#city is NULL,'', CONCAT(' - ', #city)), IF(#state is NULL,'', CONCAT(', ',#state)))
Result 12345
You need to make the output of the separators conditional on the following values being non-NULL. For example:
SELECT CONCAT(zipcode,
CASE WHEN city IS NOT NULL OR state IS NOT NULL THEN ' - '
ELSE ''
END,
COALESCE(city, ''),
CASE WHEN city IS NOT NULL AND state IS NOT NULL THEN ', '
ELSE ''
END,
COALESCE(state, '')
) AS address
FROM `Table``
Output (for my demo)
address
12345 - San Francisco, CA
67890 - Los Angeles
34567 - AL
87654
Demo on dbfiddle
I am doing a GROUP_CONCAT to display names in the format of
Lastname1, Firstname1; Lastname2, Firstname2
There are occasions where the lastname also contains strings enclosed in curved brackets - ( and ). Since these will get displayed in the middle of the concatenated string I'm trying to move it to the end.
My solution so far is this:
GROUP_CONCAT(
DISTINCT
CASE
WHEN UPPER(psn.surname) LIKE '%INACTIVE%' THEN CONCAT(TRIM(REPLACE(psn.surname, '(Inactive)', '')), ', ', psn.firstname, ' (Inactive)')
ELSE CONCAT(psn.surname, ', ', psn.firstname)
END
ORDER BY
CASE
WHEN UPPER(psn.surname) LIKE '%INACTIVE%' THEN CONCAT(TRIM(REPLACE(psn.surname, '(Inactive)', '')), ', ', psn.firstname, ' (Inactive)')
ELSE CONCAT(psn.surname, ', ', psn.firstname)
END
ASC
SEPARATOR '; '
) AS contacts
So far this works but it only looks for a specific string, there are also cases when the string within the curved brackets isn't Inactive and I don't want to hard code all of those.
So basically how do I move a string enclosed in curved brackets to the end of a string. I imagine regex is the best solution to this problem but I don't know how to use regex.
Hmmm . . . something like this might work:
GROUP_CONCAT(DISTINCT (CASE WHEN psn.surname LIKE '%(%'
THEN CONCAT(TRIM(SUBSTRING_INDEX(psn.surname, '(', 1)), ', ',
psn.firstname, '('
SUBSTRING_INDEX(psn.surname, '(', -1)
)
ELSE CONCAT(psn.surname, ', ', psn.firstname)
END)
ORDER BY psn.surname, psn.firstname ASC SEPARATOR '; '
) AS contacts
I didn't repeat the expression for the order by. That seems like overkill.
I have around 200,000 records of data with phone numbers, but the numbers are inconsistent.
for example, some may be 10 digits (missing a 0 at the beginning), some have spaces in there, some have a '-' in the middle and some begin with '+44' instead of 0.
Is there a way in mySQL to condition all these and cleanse the data in one query?
Without sample data and without an example output this is purely speculative and assuming you want the output in the format of 01234567891.
Use a combination of LENGTH, REPLACE' ANDLEFT` functions to resolve the 4 issues you highlighted:
Missing 0 at beggining.
Spaces in the string.
-'s in the string.
+44 rather than 0.
SELECT CASE WHEN LENGTH(REPLACE(REPLACE(numberfield, '-', ''), ' ', '')) = 10
THEN CONCAT('0', REPLACE(REPLACE(numberfield, '-', ''), ' ', ''))
WHEN LEFT(REPLACE(REPLACE(numberfield, '-', ''), ' ', ''), 3) = '+44'
THEN REPLACE(REPLACE(REPLACE(numberfield, '-', ''), ' ', ''), '+44', '0'
END AS Cleannumber
FROM yourtable
Assuming the phone number field is a string - the following should deal with the conditions you specified :
RIGHT( LPAD( REPLACE( REPLACE( REPLACE('phonenumber', '-', ''), '+44', ''), ' ', ''), 11, '0' ), 11 )
first any '-' are removed, then '+44' is removed, then spaces are removed, then 11 '0's are added to the start of the number, finally the rightmost 11 characters are taken.
So you would do an UPDATE query replacing the phonenumber column.
I've seen a few of these questions asked but haven't spotted one that's helped!! I'm trying to select the first part of a postcode only, essentially ignoring anything after the space. the code I am using is
SUBSTRING(PostCode, 1 , CHARINDEX(' ', PostCode ) -1)
However, I am getting:
Invalid length parameter passed to the LEFT or SUBSTRING function
There's no nulls or blanks but there are some the only have the first part. Is this what causing the error and if so what's the work around?
That would only happen if PostCode is missing a space.
You could add conditionality such that all of PostCode is retrieved should a space not be found as follows
select SUBSTRING(PostCode, 1 ,
case when CHARINDEX(' ', PostCode ) = 0 then LEN(PostCode)
else CHARINDEX(' ', PostCode) -1 end)
CHARINDEX will return 0 if no spaces are in the string and then you look for a substring of -1 length.
You can tack a trailing space on to the end of the string to ensure there is always at least one space and avoid this problem.
SELECT SUBSTRING(PostCode, 1 , CHARINDEX(' ', PostCode + ' ' ) -1)
This is because the CHARINDEX-1 is returning a -ive value if the look-up for " " (space) is 0. The simplest solution would be to avoid '-ve' by adding
ABS(CHARINDEX(' ', PostCode ) -1))
which will return only +ive values for your length even if CHARINDEX(' ', PostCode ) -1) is a -ve value. Correct me if I'm wrong!
One of the selected column is null or empty.
Something else you can use is isnull:
isnull( SUBSTRING(PostCode, 1 , CHARINDEX(' ', PostCode ) -1), PostCode)
I expected to get full name without middle initial if there is no middle name, but if column "M_NAME" is Null, the select statement returns empty. How can i solve this?
SELECT CONCAT(`Employee`.`F_NAME`, ' ', LEFT(`Employee`.`M_NAME`, 1), '. ', `Employee`.`L_NAME`) FROM `ccms`.`Employee` WHERE HR_ID = '223';
CONCAT returns NULL if any argument is null. You can solve this by making sure that no null argument is null by wrapping any nullable column in either IFNULL or COALESCE (the latter can take more than two arguments).
SELECT
CONCAT(
IFNULL(F_NAME, ''),
' ',
IFNULL(CONCAT(LEFT(M_NAME, 1), '. '), ''),
IFNULL(L_NAME, '')
)
FROM
ccms.Employee
WHERE
HR_ID = '223';
What this does is replace NULL column values with an empty string, which is probably your intent. Note that I updated the selection of M_NAME so that the period is only added if the value is not null by using this very behavior.
EDIT: You can use backticks and qualify column names if you want, but it's not necessary for this exact query.
Use COALESCE()
Returns the first non-NULL value in the list, or NULL if there are no non-NULL values.
SELECT CONCAT(`Employee`.`F_NAME`, ' ', COALESCE(LEFT(`Employee`.`M_NAME`, 1), ''), '. ', `Employee`.`L_NAME`) FROM `ccms`.`Employee` WHERE HR_ID = '223';
Use IFNULL().
Any place you have a possibly NULL field, wrap it with IFNULL(whatever, '') and then you'll get an empty string instead of a result-killing NULL.
another simple solution is use CONCAT_WS. It will work defenitely.
SELECT
CONCAT_WS(''
F_NAME,
'',
LEFT(M_NAME, 1),
L_NAME) FROM
ccms.Employee WHERE HR_ID = '223';