I am having some real difficulty converting the below MSSQL statement to produce the same results in MYSQL.
The MSSQL query is as follows:
SELECT Radius_AdslUser.Password, Radius_AdslIp.IpAdd, Route =
CASE WHEN Radius_AdslIP.Route IS NULL
THEN NULL
WHEN Radius_AdslIp.TotalIp = 8
THEN Radius_AdslIP.Ipadd + '/29 ' + SUBSTRING(Radius_AdslIp.Route, 10, Charindex(' ', Radius_AdslIp.Route) - 10) + ' 1'
WHEN Radius_AdslIp.TotalIp = 4
THEN Radius_AdslIP.Ipadd + '/30 ' + SUBSTRING(Radius_AdslIp.Route, 10, Charindex(' ', Radius_AdslIp.Route) - 10) + ' 1'
WHEN Radius_AdslIp.TotalIp = 16
THEN Radius_AdslIP.Ipadd + '/28 ' + SUBSTRING(Radius_AdslIp.Route, 10, Charindex(' ', Radius_AdslIp.Route) - 10) + ' 1'
END, AVPair =
CASE WHEN Radius_AdslUser.bandcap IS NULL
THEN NULL
ELSE 'throttle=' + Radius_AdslUser.bandcap
END, Radius_AdslIp.Subnet
FROM Radius_AdslUser,Radius_AdslIp
WHERE Username = 'username#test.tld'
AND Suspended = 0
AND Radius_AdslUser.IpAddId = Radius_AdslIp.IpAddId
Which produces the following result:
Password IpAdd Route AVPair Subnet
kmbjdatr 10.0.0.1 10.0.0.1/29 10.0.0.0 1 NULL 255.255.255.255
The format of the tables are as follows:
Radius_AdslIp
IpAddId IpAdd Subnet TotalIp UsableIp Route TypeId
944 10.0.0.1 255.255.255.255 8 6 ip:route=10.0.0.0 255.255.255.248 4
Radius_AdslUser
Username Password IpAddId
username#test.tld kmbjdatr 944
I have substituted CHARINDEX for LOCATE and have used CONCAT but am not having much luck.
For example:
WHEN Radius_AdslIp.TotalIp = 8 THEN Radius_AdslIp.Ipadd = CONCAT(Radius_AdslIp.Ipadd, '/29 ')
The table formats in MYSQL are identical.
Any help would be greatly appreciated.
Seems like you are not applying the CONCAT function correctly. Basically, a SQL Server expression like this:
column_name + 'constant string' + function_call() + #variable
would look like this in MySQL:
CONCAT(column_name, 'constant string', function_call(), #variable)
The particular wrong part in your excerpt seems to be Radius_AdslIp.Ipadd = (the one before CONCAT). Not sure what you meant by that, but in any event it's just not needed there.
Related
I've got 5 columns in table:
- id / a /b /operation/ c
where for example: 1 / 2 /3 / + / 5
What is my goal is to check if operation (+/-/*/%) is correct with the formula (a (operation) b = c) and show only those records where formula is true.
Honestly I've been trying to solve that but unfortunately I really don't know how to...
For sure I have to start with operation column:
(IF(operation = '+', a+b, IF( operation = '-', a-b, IF(operation = '*', a*b, IF(operation = '/',a/b, ''))))
Shall I mix it with IF CASE maybe? What's more, zero exepction needs to be added
Use case:
select (c = (case when operation = '+' then a + b
when operation = '-' then a - b
when operation = '/' then a / nullif(b, 0)
. . .
end)
) as expected_equals_actual
I have hundreds of phone number of the world. Each has its country prefix (the prefix varies: some are 1, 2, 3 or 4 digit long) + the phone number. I want to write a mysql query, which will show me the Country name by using the prefix.
Example : If i use sub-string for the first 3 digits, its working fine. But how i can show the prefixes which are 2 or 4 digit long ?
SELECT(
CASE (SUBSTR(Number,1,3))
WHEN '998' Then 'Uzbekistan '
WHEN '996' Then 'Kyrgyzstan '
WHEN '995' Then 'Georgia '
.....
....
ELSE 'OTHERS' END ) AS Country
A simple solution is based on the fact that the CASE statement is evaluated sequentially.
SELECT(
CASE
WHEN SUBSTR(Number,1,2) = '23' Then 'Try For 23 '
WHEN SUBSTR(Number,1,3) = '998' Then 'Uzbekistan '
WHEN SUBSTR(Number,1,3) = '996' Then 'Kyrgyzstan '
WHEN SUBSTR(Number,1,3) = '995' Then 'Georgia '
.....
....
ELSE 'OTHERS' END ) AS Country
I have a normal SQL statement:
SELECT VALUE_ID, UF_CRM_TASK FROM b_uts_tasks_task
Now this returns a a different field everytime but they take the form of the following:
a:1:{i:0;s:7:"CO_2012";} or a:1:{i:0;s:5:"CO_12";} or a:1:{i:0;s:7:"CO_2017";}
Basically they're different everytime. What I need is to just get the number after the CO_ part. I have tried TRIM but because everything changes in the leading and trailing section I don't think this would work.
I have looked on Stack Overflow for a while and cannot find it. I know how to do it in PHP:
$data = $row['UF_CRM_TASK'];
$companyID = substr($data, strpos($data, "CO_") + 1);
$newCompanyID = preg_replace('/[^0-9.]+/', '', $companyID);
But not SQL. Thanks in advance
In MYSQL is a bit ugly:
/*SUBSTRING_INDEX BASED ON CO_ AND THE LAST " - in 2 SUBSTRINGS*/
SELECT `VALUE_ID`, SUBSTRING_INDEX(SUBSTRING_INDEX(`UF_CRM_TASK`, 'CO_', -1), '"', 1) AS `COMPANY_ID` FROM `b_uts_tasks_task`
In PHP you can just unserialize():
$data = unserialize($row['UF_CRM_TASK']);
$companyID = str_replace('CO_', '', $data[0]);
eg:
$data = unserialize('a:1:{i:0;s:5:"CO_12";}');
echo str_replace('CO_', '', $data[0]);
//==> 12
You need to use CharIndex and SubString (Microsoft SQL) or
This is the sample code I made for my Microsoft SQL server:
declare #companyIdString varchar(50) = 'a:1:{i:0;s:7:"CO_2012";}'
print 'Company ID in a string: ' + #companyIdString
print 'Find first position: ' + Cast(charindex('"CO_', #companyIdString) as varchar(2))
print 'Locate the second position (the last "): ' + Cast(charindex('"', #companyIdString, charindex('"CO_', #companyIdString)+4) as varchar(2))
print 'Extracted Company Id: ' + substring(#companyIdString,charindex('"CO_', #companyIdString)+4, charindex('"', #companyIdString, charindex('"CO_', #companyIdString)+4) - charindex('"CO_', #companyIdString) - 4)
select
#companyIdString as CompanyIdString,
substring(#companyIdString,charindex('"CO_', #companyIdString)+4, charindex('"', #companyIdString, charindex('"CO_', #companyIdString)+4) - charindex('"CO_', #companyIdString) - 4) as CompanyId
I also made the same code on a mySQL server:
set #companyIdString := 'a:1:{i:0;s:7:"CO_2012";}';
select
#companyIdString as CompanyIdString,
substring_index(substring_index(substring_index(#companyIdString, '"', 2), '"', -1), '_', -1) as CompanyId
The substring_index starts by locating the second " (string is now a:1:{i:0;s:7:"CO_2012), then it searches backward with the -1 to locate the first " (string is now CO_2012). And then it searches backward for the underscore (string is now 2012).
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.
I use Oracle and MySql, so if you have any answer in both codes please let me know,
Issue:
I have 2 columns in one table called: USers
Column#1= Names
Column#2= UCNames
This list contain names that are from different sources but partially match like:
Names
Alex Jones Marfex
UCNames
Alex Jonnes Mike Marfex
I want to compare both of the columns and find match based on the following attributes:
Search on the first 4 for letters and 4 last words and to store in new column called: verifiyed
Thanks
This gives you the first word in a string
Substring(Col,1,(Locate (' ',Col + ' ')-1)) First
This gives you the last word in a string
Reverse(Substring(Reverse(Col), 1, Locate(' ',Reverse(Col)) - 1)) Last
So your compare could be
Where
Substring(Col1,1,( Locate(' ',Col1 + ' ')-1))
= Substring(Col2,1,( Locate(' ',Col2 + ' ')-1))
And
Reverse(Substring(Reverse(Col1), 1, Locate(' ',Reverse(Col1)) - 1))
= Reverse(Substring(Reverse(Col2), 1, Locate(' ',Reverse(Col2)) - 1))
I went with words which seemed safer for what you are trying to do with just a bit more effort. If you do want to keep the 4 characters, just replace the Locate es with 4.
Based on the answer by #asantaballa , but using MySQL SUBSTRING_INDEX function :-
WHERE SUBSTRING_INDEX(Col1, ' ', 1) = SUBSTRING_INDEX(Col2, ' ', 1)
AND SUBSTRING_INDEX(Col1, ' ', -1) = SUBSTRING_INDEX(Col2, ' ', -1)
or to crudely check the first 4 characters and the last 4 words:-
WHERE SUBSTRING(Col1, 1, 4) = SUBSTRING(Col2, 1, 4)
AND SUBSTRING_INDEX(Col1, ' ', -4) = SUBSTRING_INDEX(Col2, ' ', -4)