I'm upgrading an Access 2016 back-end database to MySQL 8.0 and having problems with the generated columns. I used a third-party tool to migrate my Access tables to MySQL, and for the most part it worked fine. The Access calculated columns are not migrated. Or rather, they are, but as standard columns. I need to go in and redefine the calculations in MySQL.
In Access, I have a Heading column which combines title, last name, first name, spouse name, spouse last name into a single heading. Here is the Access calculation which works just fine.
IIf([UseTitle],[Title] & " ","") & IIf(IsNull([DonorFirstName]),[DonorLastName],[DonorFirstName] & IIf(IsNull([SpouseName])," " & [DonorLastName],IIf(IsNull([SpouseLastName])," and " & [SpouseName] & " " & [DonorLastName]," " & [DonorLastName] & " and " & [SpouseName] & " " & [SpouseLastName])))
If this person wants to use a title (such as "Mr. & Mrs."), insert the title. If no first name, the last name's a company, so add the LastName field, otherwise add the FirstName field. If no spouse, add space and LastName field. If spouse with same last name, add " and " plus SpouseLastName. Otherwise, add LastName plus " and " plus SpouseFirstName plus space plus SpouseLastName. So now you either have "Mr. & Mrs. John and Jane Doe" or "Mr. & Mrs. John Doe and Jane Smith".
I tried doing this in MySQL using a combination of CONCATs and IFs like this:
`Heading` varchar(509) CHARACTER SET utf8 COLLATE utf8_general_ci GENERATED ALWAYS AS (concat(if(((`UseTitle` = 0) or (`Title` is null)),_utf8mb3'',concat(`Title`,_utf8mb3' ')),if((ifnull(`DonorFirstName`,_utf8mb3'') = _utf8mb3''),`DonorLastName`,concat(`DonorFirstName`,_utf8mb3' ',if((ifnull(`SpouseName`,_utf8mb3'') = _utf8mb3''),`DonorLastName`,if((ifnull(`SpouseLastName`,_utf8mb3'') = _utf8mb3''),concat(_utf8mb3'and ',`SpouseName`,_utf8mb3' ',`DonorLastName`),concat(`DonorLastName`,_utf8mb3' and ',`SpouseName`,_utf8mb3' ',`SpouseLastName`))))))) STORED,
This does NOT work, and in fact causes an error in the table DDL. It seems to update properly, but when I try to open the table design in MySQL Workbench, it gives me an error saying Error parsing DDL for tableName
When I choose to view the DDL in another tab, the generated field line is identified with an asterisk, and shows this:
`Heading` varchar(509) CHARACTER SET utf8 COLLATE utf8_general_ci GENERATED ALWAYS AS (concat(if(((`UseTitle` = 0) or (`Title` is null)),_utf8mb3'',concat(`Title`,_utf8mb3' ')), `DonorFirstName`,_utf8mb3' ',`DonorLastName`)) VIRTUAL,
The '' after the _utf8mb3 is underlined, and hovering over it shows:
"Syntax error: extraneous input found - expected 'comma'"
Luckily, this is a brand new database, and I can simply drop and recreate the table, but not if I cannot figure out how to properly generate that column.
I'm obviously doing something wrong here. Is there a correct way to make this generated column properly work?
Your alter table command is incorrect. A valid command would look like this. Remove all _utf8mb3 and correct the if and concat native functions.
ALTER TABLE tableName
`Heading` varchar(509) CHARACTER SET utf8 COLLATE utf8_general_ci GENERATED ALWAYS AS (
concat(
if( ((not `UseTitle`) or IsNull(`Title`)),
'',
concat(`Title`,' ', `DonorFirstName`, ' ',`DonorLastName`)
)
)
) VIRTUAL,
PS: concat(value1, value2) will return null if any value within the function is null.
concat_ws('', value1, value2) will ignore nulls and add all non null values.
Thanks to those who answered or commented.
If I make the changes in the SQL editor, it works just fine. The changes are accepted as expected. Still, I get a hard error trying to view the table definition in MySQL Workbench. So, the problem seems to be there instead. I will re-post the question differently to solve that issue.
Related
I'm trying to concatenate the title, first name, and last name of a contact into one string in an Access report. Currently I am using a text field with the control set to
=[ContactTitle] & ' ' & [FirstName] & ' ' & [LastName]
However, some contacts don't have a title associated with them and my method leaves a leading space which makes the text alignment on my report look sloppy. So I'm wondering, is there a way to concatenate but only include that first space if the contact title is not null? I am aware of the plus operator but not experienced enough to see a way to use it in this case without just making my entire string null.
You can use the + operator for concatenation.
Concatenating with + yields Null if any of the values you're concatenating are Null:
=([ContactTitle] + " ") & ([FirstName] + " ") & [LastName]
Do note that some devs frown upon using + for concatenation, stating that & is the concatenation operator in VBA.
Also note that if one of the parameters is a zero-length string, this won't work. Only a real Null will lead to the result being Null.
You could use the IIf() method to check if the title is null and then depending on the answer insert or don't insert a space.
You can also use the Nz() method to check for zero-length strings at the same time as null strings by setting all null values to be a zero-length string and then comparing the result to "".
The code I would recommend you use here is:
=IIf(Nz([ContactTitle],"") = "", "", [ContactTitle] & " ") & [FirstName] & " " & [LastName]
If you you have any problems with this or need a better explaination leave a comment and I'll get back to you.
I have a table which carries around 172 entries, with different column names, however I want to update all of them with a simple query.
I have a name entered in the name column (http://prntscr.com/j9qeg6)
I would like to replace the III with I've using a simple query,
Now I've been checking and trying however it does not seem to work.
I used the following query which got me closest to the result however its not working.
UPDATE item_template SET name = CONCAT("IV", SUBSTRING(name, LENGTH("III ")+1));
Does anyone have an idea on this?
Apostrophe ' instead of Double quotes "
You can try use REPLACE function.
UPDATE item_template
SET name = REPLACE(name, ' III', ' IV');
sqlfiddle:http://sqlfiddle.com/#!9/b4b8d6/1
Using: MySQL Workbench & MySQL for Excel
I have a number returned that is 12 digits long, however when imported in to excel using any manner this results in: 1.20523E+11.
I want (need) to specify that the value needs to be returned as text so to complete the query via MySQL for Excel without this error.
My existing query:
Select
-- Get Order data --
T5.orders_id As OID,
DATE_FORMAT(T5.date_purchased, '%Y-%m-%d') As ODate,
T3.products_name As PName,
T3.products_id As PID,
error:
DATE_FORMAT(T5.date_purchased,'%y%m%d%H%i%s') As DecRepID,
-- This results in 1503070244
-- Transaction ID to match with Payments
I need the 1503070244 to return as TEXT so excel imports it without me needing to then convert the text.
I've looked at CONVERT & CAST however I can't get the query to work correctly & they resolve in to syntax errors.
CONVERT(VARCHAR(12),(DATE_FORMAT(T5.date_purchased,'%y%m%d%H%i%s'))) As DecRepID,
CAST returns the correct values, however Excel doesn't recognise as text.
cast(DATE_FORMAT(T5.date_purchased,'%y%m%d%H%i%s') as char) As DecRepID
I know it's something simple, however I can't find what is the cause of the error.
Is there a way to correct this through code, or am I stuck with post-formatting after import in to Excel?
Thank you.
Using the idea that Shahkalpesh had, you could do 2 different things:
1) You could try escaping the ' by using: '\''
concat('\'', DATE_FORMAT(T5.date_purchased,'%y%m%d%H%i%s')) As DecRepID,
2) You could try escaping the ' by using: "'"
concat("'", DATE_FORMAT(T5.date_purchased,'%y%m%d%H%i%s')) As DecRepID,
By escaping the ' the column will import in to Excel as: '1503070244 which is what you will need to have Excel recognise it as text to stop the shortened result.
You can read more here: http://dev.mysql.com/doc/refman/5.7/en/string-literals.html
I have table named "city" with column named "city_name" with about 200 records.
I have created another colum named slugs where I want to copy all the records from "city_name" in the same row with spaces replaced with dash - and lowercase.
How can I achieve this via phpmyadmin.
Thanks
You should be able to do this via the following query:
UPDATE city SET slugs=LOWER(REPLACE(city_name, " ", "-"))
Breaking this down, we're using REPLACE to swap all instances of " " with "-" in the existing city_name column, and then passing the result of this to the LOWER function (to convert the data to lower case) before setting the slugs field with this value.
Depending on how "clean" your data is, you might also want to TRIM the data (to remove any leading or trailing spaces in the city_name field) before you apply the REPLACE as such:
UPDATE city SET slugs=LOWER(REPLACE(TRIM(city_name), " ", "-"))
Incidentally, if you've not used (My)SQL much I'd recommend a read of the String Functions manual page - the time you spend on this now will more than repay itself in the future.
Here is SQLFiddle
MYSql documentation for function LOWER(str) and REPLACE(str,from_str,to_str)
UPDATE city SET slugs = LOWER(REPLACE(city_name," ", "-"));
I've been given an MS-Access application to maintain and being more acquainted with Oracle as dbms I bump into issues now and then..
Today it looks like MS-Access has problems when a hyphen is used for a column name...
The following insert statement was coupled to the NotInList event to add an extra entry to a listbox.
INSERT INTO tblProductInfo ( ProductInfo-Product )
"SELECT """ & NewData & """ AS ProductInfo-Product;"
But it's not working (anymore? first time the issue is reported, not sure if the original developer tested it out).
I've tested it out with a single-record append query and it looks like the hyphen is the culprit and I just cannot find a way to escape that..
INSERT INTO tblProductInfo ( ProductInfo-Product ) VALUES ("myData")
The error given is "Syntax error in INSERT INTO statement"
There does not seem any other way to specify the MS-Access fieldname, is it? (square brackets are only used for SELECT statements,
So... I'm calling for the wisdom of the Stackoverflow gods and am hoping someone knows how to solve this...
Thanks in advance !!
You need square brackets on that:
"INSERT INTO tblProductInfo ([ProductInfo-Product]) Values (""" & NewData & """)"
Or better yet, avoid odd characters and spaces in field and table names.
Square brackets are used in any sql statement where the field or table name is problematical:
It is a reserved word
It contains a space
It includes a special character
You can even use them with DDL:
Create Table tblProductInfo ( [ProductInfo-Product] Text(50))