Missing Operator in query expression in MS Access - ms-access

I am trying to run a query that returns letters before the space from an address column
SELECT col1,(Case When CHARINDEX(' ',address) = 0 then address Else LEFT(address,CHARINDEX(' ',address)-1) END) as streetNumber from table1
where stateID = 15
When I run this query in SQL Server Management Studio it runs but running it in Access gives me:
Syntax error (missing operator) in query expression '(Case WHEN
CHARINDEX(' ',address) =0 then address Else LEFT(address,CHARINDEX('
',address)-1) END)
But there are no operators missing. I am not sure why I am getting this error.

Use Access SQL and VBA:
Select
col1,
IIf(InStr([address], ' ') = 0, [address], Left([address], InStr([address], ' ') - 1)) As streetNumber
From
table1
Where
stateID = 15

Related

Run MySQL Query if condition is true else run another query

I need to split address column into streetName and streetNumber. The problem is sometimes the complete address in located in the streetName column , and sometimes too inside the streetNumber column. I am using this code to do the splitting
replace(streetName, substring_index(streetName, ' ', -1), '') as
street,substring_index(streetName, ' ', -1) as number
and
replace(streetNumber, substring_index(streetNumber, ' ', -1), '') as
street,substring_index(streetNumber, ' ', -1) as number.
What I want to do is run this section of my select query statement if streetName is null or empty execute the code with the streetNumber and vice versa.
I have to sections ifStreet() and IfStreetNumber() in a stored Procedure. But I get an error when I run it , and If I put the code directly inside the case statements it does not work as well.I get an error message, You have an error in your SQL Syntax. Is it possible to run a query base on condition or what is wrong with my approach? Thank you,
SELECT
firstName,
lastName,
D.email AS email,
streetName,
streetNumber,
zipCode,
city,
IF(length(zipCode) =5,'Germany','') As country,
registeredOn,
N.email AS matchedEmail,
CASE
WHEN gender = 'Frau' OR gender = 'f' THEN 'f'
WHEN gender = 'Herr' OR gender = 'm' THEN 'm'
ELSE ''
END AS Title,
CASE
WHEN gender = 'Frau' OR gender = 'f' THEN 'Frau'
WHEN gender = 'Herr' OR gender = 'm' THEN 'Herr'
ELSE ''
END AS Salutation,
CASE
WHEN streetName !='' THEN
call ifStreet()
ELSE
call ifStreetNumber()
END
FROM
matchFiles.TableA AS D
INNER JOIN
matchFiles.TableB AS N ON D.email = N.email
WHERE
registeredOn <= '2018-08-31';
Stored Procedures are not allowed within SELECT Statements. So your approach will not work.
Try to shift your logic from your stored procedure into adequate subquery.
I restructured the query and also dropped using a stored procedure . I using an IFNULL function to do the alternation to use either streetName or streetNumber depending on which column has a the full address string.
replace(IFNULL(streetName,streetNumber), substring_index(IFNULL(streetName,streetNumber), ' ', -1), '') as street,substring_index(IFNULL(streetName,streetNumber), ' ', -1) as number

Can Sybase CASE expressions have a default column name for their result?

I have a sybase query that is structured like this:
SELECT
case
when isnull(a,'') <> '' then a
else convert(varchar(20), b)
end
FROM table_name
WHERE b=123
It used to return the results of the 'case' in a column named 'converted'. It now returns the results of the 'case' in a column with an empty string name ''.
How could this be? Could there be some database configuration that defaults the results of a 'case' with no name?
(I've fixed the broken query by adding " as computed" after 'end' but now I'd like to know how it used to return as 'computed' before I added the fix?)
Is this what you want?
SELECT (case when isnull(a, '') <> '' then a
else convert(varchar(20), b)
end) as converted
-------------^
FROM table_name
WHERE b = 123;
By the way, you could write the select more succinctly as:
SELECT coalesce(nullif(a, ''), b) as converted

CASE WHEN does not exist on Access SQL

I discovered a couple of hours ago that CASE WHEN doesn't work on access queries. I have a complex CONCAT on my SELECT statement but it is written in MySQL:
SELECT CONCAT(
'www.mywebsite.com/catalogsearchtool?title='
, REPLACE(tblTitles.Content_Name, '&', '%26')
, '&contributor=ln:'
, CASE WHEN LOCATE('; ',tblTitles.Contributors) > 0
THEN REPLACE(REPLACE(REPLACE(
CASE WHEN RIGHT(TRIM(tblTitles.Contributors), 1) = ',' THEN LEFT(TRIM(tblTitles.Contributors), CHAR_LENGTH(TRIM(tblTitles.Contributors)) - 1) ELSE TRIM(tblTitles.Contributors) END
, '&', '%26'), '; ', ',rl:Author&contributor=ln:'), ', ', ',fn:')
ELSE REPLACE(REPLACE(
CASE WHEN RIGHT(TRIM(tblTitles.Contributors), 1) = ',' THEN LEFT(TRIM(tblTitles.Contributors), CHAR_LENGTH(TRIM(tblTitles.Contributors)) - 1) ELSE TRIM(tblTitles.Contributors) END
, '&', '%26'), ', ', ',fn:')
END
, ',rl:Author&language='
, LOWER(tblTitles.language)
) AS 'link'
Basically what that does is that it generates a link based on names that can be found in a column called Contributors. That column has the following order:
last_name, first_name
or
last_name, first_name; last_name, first_name
For reasons out of my control, there can be many names in that column. So for the query above a couple of results would be (for 2 names):
www.mywebsite.com/catalogsearchtool?title=tile123&contributor=ln:smith,fn:john,rl:Author&contributor=ln:smith,fn:jane,rl:Author&language=english
And (for 1 name):
www.mywebsite.com/catalogsearchtool?title=title5678&contributor=ln:smith,fn:john,rl:Author&language=english
Can anyone please help me translating this logic to Access SQL?

am trying to execute the query using the CONCAT function in the mysql console

the query is as follows
SELECT id, phonenumber CONCAT( 'A reminder for ', name, 'underfive number ', underfiveNO, 'for ', message, 'tomorrow.If shot was administered please ignore this message ' )
FROM appointment WHERE MONTH (current_date) = MONTH (appointmentdate)
AND DAY (current_date) < DAY (appointmentdate)
AND (NOT lastnotified = current_date) OR lastnotified IS NULL;
the error am getting is
ERROR 1064(42000) you have an error in your sql syntax; check the manual that corresponds to your mysql server version for the right syntax to use near '('A reminder for',name,'under five number',undefiveNo, 'for', message'at line1
You forgot a comma
SELECT id, phonenumber, CONCAT( '...
^-----------------herre
There should be separator in between phonenumber and CONCAT() in your query: try with this
SELECT id, phonenumber, CONCAT('A reminder for ', name, ' underfive number ', underfiveNO, ' for ', message, ' tomorrow. If shot was administered please ignore this message' )
FROM appointment WHERE MONTH (current_date) = MONTH (appointmentdate)
AND DAY (current_date) < DAY (appointmentdate)
AND ((NOT lastnotified = current_date) OR lastnotified IS NULL);
and also as you are adding a OR condition on lastnotified column, it should be enclosed with in paranthesis to meet either of the condition

Incorrect parameter count in the call to native function 'ISNULL'

I have a query that I am trying to convert to MySQL from MS SQL Server 2008. It runs fine on MSSQL,
I get the error
"Incorrect parameter count in the call to native function 'ISNULL'".
How do I solve this?
SELECT DISTINCT
dbo.`#EIM_PROCESS_DATA`.U_Tax_year,
dbo.`#EIM_PROCESS_DATA`.U_Employee_ID,
CASE
WHEN dbo.`#EIM_PROCESS_DATA`.U_PD_code = 'SYS033' THEN SUM(dbo.`#EIM_PROCESS_DATA`.U_Amount)
END AS PAYE,
CASE
WHEN dbo.`#EIM_PROCESS_DATA`.U_PD_code = 'SYS014' THEN SUM(dbo.`#EIM_PROCESS_DATA`.U_Amount)
END AS TOTALTAXABLE,
dbo.OADM.CompnyName,
dbo.OADM.CompnyAddr,
dbo.OADM.TaxIdNum,
dbo.OHEM.lastName + ', ' + ISNULL(dbo.OHEM.middleName, '') + '' + ISNULL(dbo.OHEM.firstName, '') AS EmployeeName
FROM
dbo.`#EIM_PROCESS_DATA`
INNER JOIN
dbo.OHEM ON dbo.`#EIM_PROCESS_DATA`.U_Employee_ID = dbo.OHEM.empID
CROSS JOIN
dbo.OADM
GROUP BY dbo.`#EIM_PROCESS_DATA`.U_Tax_year , dbo.`#EIM_PROCESS_DATA`.U_Employee_ID , dbo.OADM.CompnyName , dbo.OADM.CompnyAddr , dbo.OADM.TaxIdNum , dbo.OHEM.lastName , dbo.OHEM.firstName , dbo.OHEM.middleName , dbo.`#EIM_PROCESS_DATA`.U_PD_code
MySQL
SELECT DISTINCT
processdata.taxYear, processdata.empID,
CASE WHEN processdata.edCode = 'SYS033' THEN SUM (processdata.amount) END AS PAYE,
CASE WHEN processdata.edCode = 'SYS014' THEN SUM (processdata.amount) END AS TOTALTAXABLE,
company.companyName, company.streetAddress, company.companyPIN, employeemaster.lastName + ', ' + IFNULL(employeemaster.middleName, '')
+ ' ' + IFNULL(employeemaster.firstName, '') AS EmployeeName
FROM
processdata INNER JOIN
employeemaster ON processdata.empID = employeemaster.empID
CROSS JOIN company
GROUP BY processdata.taxYear, processdata.empID, company.companyName, company.streetAddress, company.companyPIN,
employeemaster.lastName, employeemaster.firstName, employeemaster.middleName, processdata.edCode
The MySQL equivalent of ISNULL is IFNULL
If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns
expr2.
Maybe also look at SQL NULL Functions
The ISNULL from MySQL is used to check if a value is null
If expr is NULL, ISNULL() returns 1, otherwise it returns 0.
I would suggest that you switch to the ANSI standard function coalesce():
(dbo.OHEM.lastName + ', ' + coalesce(dbo.OHEM.middleName, '') + '' + coalesce(dbo.OHEM.firstName, '')
) AS EmployeeName
You could also make your query easier to read by including table aliases.
EDIT:
As a note, I seemed to have missed the direction of conversion. The MySQL query would use concat():
CONCAT(OHEM.lastName, ', ', coalesce(OHEM.middleName, ''),
coalesce(concat(' ', OHEM.firstName), '')
) AS EmployeeName
I was getting an error when running JUnit tests against a query which had ISNULL(value) with the error saying ISNULL needed two parameters. I fixed this by changing the query to have value is null and the code works the same while the tests now work.