I keep getting a syntax error using Microsoft Access. I believe I am putting in the iif statement incorrectly. I am even using the expression builder. However, it keeps saying that there may be a misplaced comma
Please help
Respectfully,
In Design View instead of , use ; in the IIF() function:
IIf([LastName]="Smith";1;0)
If you see the query in SQL View though, you will see the function with commas.This is a case applying to regions where the comma could be a decimal separator (check your regional settings of your PC).
The error is not due to the use of the comma in place of a semi-colon, but rather because you have set the Table row to Orders within the query grid:
As such, the query is attempting to access a field called [iif([LastName]="Smith",1,0)] from the table Orders, and this is an invalid field name.
Since you are using a calculated field which isn't being sourced directly from any particular table, clear Orders from the table row for this field to fix the error.
Related
I have been working on a database for my coursework and have used phpMyAdmin to build it. Now I am working on the queries using the query tool.
When I pick the tables and data I want to query and press "update query" it will generate the query which looks something like this:
SELECT `Customer`.`CustomerName`, `OrderDetails`.`Product`, `OrderDetails`.`QuantityOrdered`
FROM `Customer`
LEFT JOIN `Order` ON `Order`.`Customer` = `Customer`.`CustomerID`
LEFT JOIN `OrderDetails` ON `OrderDetails`.`Order` = `Order`.`OrderID`
This works fine and gives me the results I was expecting. However when I try and write my own query and put something like "SELECT Customer.CustomerName," WITHOUT the ' symbol it won't work and just throws up an error message.
Must I always wrap them using the ' symbol for the query to work?
Forward quotes are used to escape object names in MySQL. You don't have to use them unless you use names that wouldn't be valid identifiers - in this case, the table name order is a reserved word, and must be escaped. All the other tables and columns you're using seem to be OK.
Except for the visual nightmare and ability to create horrendous table names, backticks are entirely unnecessary. You will, however, be required to wrap any variables in single quotes.
As you can see from my example below, using backticks is not a requirement with PHPMYADMIN;
The reason it is not working when you remove the backticks is because you have a column called 'order'. Order is a keyword in SQL and therefore cannot be used as a column name without being wrapped in either quotes or backticks.
I'm using SSIS to separate good data from unusable date. In order to do that I used derived columns, script task and conditional split where I assigned certain conditions. One of the conditions I need to apply is that none of the numbers in one column cannot be negative. I'm guessing that the best way to solve this would be using conditional split, but I cannot get it to work. I'm new to SSIS, so any help would be appreciated.
You'd have an Expression like
[MyCaseSensitiveColumnName] < 0
and then name the output path something like BadData_NegativeValue
From the comments
that is what I did before, but I'm getting an error saying that The data types "DT_WSTR" and "DT_I4" are incompatible for binary operator ">"
That error message indicates that you are attempting to compare a unicode string (DT_WSTR) and an integer (DT_I4) and that the expression language does not allow it.
To resolve this type incompatibility, you would need to first convert the value of MyCaseSensitiveColumnName from DT_WSTR to an integer.
I'd likely add a Derived Column Component to my data flow and create a new column called MyCaseSensitiveColumnNameAsInteger with an expression like
(DT_I4) [MyCaseSensitiveColumnName]
Now, that may be perilous depending on the quality of your source data. I don't know why you are pulling numeric data in as a string. If there could be non whole numbers in the data set, then we will need to check before making the cast. If there are NULLs in that dataset, those too may cause issues.
That would result in our conditional split check becoming
[MyCaseSensitiveColumnNameAsInteger] < 0
I'm having trouble building a query in Access 2013. The database isn't mine and the only thing I really have control over is this query. There is a table, I'm pulling 7 fields from it and eventually adding an 8th field to the query to do some string manipulation.
However, I keep getting getting "Syntax error in date in query expression 'fieldname'." error whenever I click on the arrow to sort the fields. The odd thing is these errors pop up when sorting non-date fields. When sorting the date field I get "Syntax error (missing operator) in query expression 'Release Date'."
This happens after a fresh build. I have no WHERE conditions, just SELECT and FROM. Ideas?
Here's the sql query, though I'm mainly working in the query design view:
SELECT Transmissions.[Job#], Transmissions.[Part#], Transmissions.TransmissionSN, Transmissions.Status, Transmissions.[Release Date], Transmissions.[Build Book Printed], Transmissions.[ID Tags Required]
FROM Transmissions;
Well... it seems you are the lucky inheritor of a poorly designed database.
Using special characters in a field name is just asking for trouble. And you've found what that trouble is.
Access uses the # sign to designate a Date type for query comparisons. Such as:
dtSomeDate = #2/20/2017#
You surround the date with the # signs.
In your case, the query thinks [Job#] and [Part#] are trying to wrap dates. But of course, that's not the case and thus it fails.
You can try a couple of work arounds. (I leave it to you to experiment.)
1) You can try to rename the problem fields within your query. So that:
Transmissions.[Job#] becomes Transmissions.[Job#] as JobNum
and
Transmissions.[Part#] becomes Transmissions.[Part#] as PartNum
2) You can try to copy the [Transmissions] table to a new table that you create
that does not have the naming problems.
3) Export the [Transmissions] table to a CSV file and re-import it to a new
table (or possibly new database) without the naming problems.
Here is a link to a microsoft article that tells you why to avoid special characters in Access:
Big Bad Special Chars.
Hope that puts you on the right track. :)
Typically, this means that the field names are missing or misspelled.
Try running this to see:
SELECT * FROM Transmissions;
I'm using the "ASC" function in Access (Version 365 Proplus, 32 bit).
Have created a query that uses a table with a post code that needs validating. I'm looking at the first character in the postcode, converting it to ASCII character then planning on filtering out the ones I don't want.
The formula looks like this:-
Site_PostCode_String_Validation_P1: Asc(Left([site_postcode],1))
This works fine and converts as expected. However, when I try sorting or filtering using the Query Criteria on my Ascii list I get the following message:-
"Data Type Mismatch in Criteria Expression"
I have tried converting to a string, for example, using the below:-
Str(Asc(Left([site_postcode],1)))
But this has made no difference, get the same error message when applying criteria or sorting.
I have tried filtering using text and numbers but get the same error.
I have searched here and have Googled but can not see anything relating to the above.
Thanks for any suggestions.
You might simplify it a bit:
Site_PostCode_String_Validation_P1: Asc(Nz([site_postcode], Chr(0))
I tried creating a new table based on the results, thinking that this would then enable me to apply my filtering, during this process I got a more detailed error message stating error due to Null type conversion failure. So then realized that I need to make sure there were no Null entries before doing the ascii conversion. Hopefully this will be of help to someone else. Final formula now works as below:
IIf(IsNull([site_postcode])=-1,Null,Asc(Left([site_postcode],1)))
I am having the following problem.
I want to execute as sql statement that filters the results with HAVING. However the having is on a column that is calculated from an IF() function inside the select. This way, MySQL server complains that the column inside having clause is unknown!
EX:
SELECT col1,col2,IF(expr1,expr2,expr3) AS `wantedColumn`
FROM....
WHERE ...
HAVING LENGTH(`wantedColumn)>0
It is as mysql cannot understand that the column returned by the if expression is named wantedColumn... If I use other columns it is working correctly. But I need to filter on that. Any suggestions?
Thanks
This is due to mode configurations:
If your database is using the mode 'ONLY_FULL_GROUP_BY', you will not be able to use a column alias in WHERE, GROUP BY or HAVING, it does not work. You have to either repeat the whole expression or use sub query.
In order to know if you are using 'ONLY_FULL GROUP_BY' mode, use the following query:
SELECT ##sql_mode;
If you want to change it to a mode that allows it:
SET SESSION sql_mode =STRICT_TRANS_TABLES;
To know more about SQL modes:
http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html
Answer by comment from Inanda Menezes #Inanda:
Look at it: sqlfiddle.com/#!2/5294e4/6 , I just removed the backticks
from the alias used inside the having and your example worked.
According to mysql documentation you don't need to use backticks on
identifiers that does not have special words or characters. If you use
it, in most of cases, it will not cause your query to fail, but it's
not necessary at all to quote normal identifiers with non-standard
escape. Anyway, it seems to fail when escaping with backsticks a alias
in having clause which uses length function, so just don't escape this
one. – Inanda Menezes