I have written a query with CASE but I am getting () problem.
select SM.subscriber_name as name ,
SM.accountType as accountType,
SM.middlename as middleName,
SM.lastname as lastName,
SM.title as title,
SM.email as email,
SM.company as company,
SM.email1 as aEmail,
,
SM.zipcode as zipcode,
SM.phone_no as phoneNumber,
SM.landlinenumber as landlineNumber,
SM.login_id as loginId,
SD.subscriberType as subscriptionType,
SD.product_id as productType,
case SM.state when 'null' then '' as state else STDD.state_name as state end,
case SM.city when 'null' then '' as city else CDD.city_name as city end,
case SM.country when 'null' then '' as country else CD.country_name as country end,
SD.fulldownloadaccess as fullDownloadAccess,
SD.emailid_limit as emailLimit,
SD.acessTime as planTime
from subscriber_master SM ,
subsciber_details SD,
city_details CDD,
state_details STDD,
country_details CD
where SM.subscriber_id=16704 and
SM.subscriber_id=SD.subscriber_id and
SM.country = CD.country_id and
SM.state = STDD.state_id and
SM.city = CDD.city_id;
Please help me where I can put the brackets
There are a couple of problems with your query. Firstly your CASE expressions are written incorrectly, the expression must be complete before any AS part (see the manual) so for example,
case SM.state when 'null' then '' as state else STDD.state_name as state end,
should be written as
case SM.state when 'null' then '' else STDD.state_name end as state,
Additionally if you are checking for a NULL value, rather than a string value of 'null', you need to write the CASE expression as:
case when SM.state IS NULL then '' else STDD.state_name end as state
Also part way down you have an extra , (between SM.email1 as aEmail, and SM.zipcode as zipcode,).
However none of these problems will give you the message in your title, is there some code you haven't shown us?
I would say you are interested to show the NULL's as empty values, so, you need to rebuild your query like:
CASE sm.state
WHEN 'null' THEN '' AS state
ELSE stdd.state_name AS state
end,
To be rewritten as:
CASE
WHEN sm.state IS NULL THEN ''
ELSE stdd.state_name END AS state,
And the same for all 'NULL'. Also, the AS should be written after the END of the CASE
REVIEWED: Just realised that the sentence below should return the same but is much more compact and easier to read:
ISNULL(sm.state, '') AS state,
You can change your case statement as shown below it will check both null and blank values
case ISNULL(SM.[state],'') when '' then '' else STDD.state_name end as [state],
case ISNULL(SM.city,'') when '' then '' else CDD.city_name end as city,
case ISNULL(SM.country,'') when '' then '' else CD.country_name end as country
You should use join for providing where conditions between different tables in select statement. Alternatively you can use the below modified query
select SM.subscriber_name as name ,
SM.accountType as accountType,
SM.middlename as middleName,
SM.lastname as lastName,
SM.title as title,
SM.email as email,
SM.company as company,
SM.email1 as aEmail,
SM.zipcode as zipcode,
SM.phone_no as phoneNumber,
SM.landlinenumber as landlineNumber,
SM.login_id as loginId,
SD.subscriberType as subscriptionType,
SD.product_id as productType,
case ISNULL(SM.[state],'') when '' then '' else STDD.state_name end as [state],
case ISNULL(SM.city,'') when '' then '' else CDD.city_name end as city,
case ISNULL(SM.country,'') when '' then '' else CD.country_name end as country,
SD.fulldownloadaccess as fullDownloadAccess,
SD.emailid_limit as emailLimit,
SD.acessTime as planTime
from subscriber_master SM,
subsciber_details SD,
city_details CDD,
state_details STDD,
country_details CD
where SM.subscriber_id=16704
and SM.subscriber_id=SD.subscriber_id and
SM.country = CD.country_id and
SM.[state] = STDD.state_id and
SM.city = CDD.city_id;
Related
I am stuck with an SQL query running from a bash script.
The following works and outputs the file as expected but there are some fields that contain commas and I would like these fields to be included but have the commas escaped.
FILE="/var/lib/mysql-files/UMHP/ConditionAssessment.csv"
mysql --user='root' --password='xxxxxxxxxxxxxxx' UMHPSurvey << EOFMYSQL
SELECT
DateEntry AS 'Entry Date',
FirstName AS 'First Name',
LastName As 'Last Name',
member_id as 'Member ID',
mobile AS 'Mobile',
Doctor AS 'Need help getting connected to a doctor',
CASE WHEN HealthConditions LIKE '%Asthma%' THEN 'Yes' ELSE 'No' END AS 'Asthma',
CASE WHEN HealthConditions LIKE '%COPD%' THEN 'Yes' ELSE 'No' END AS 'COPD',
CASE WHEN HealthConditions LIKE '%Cancer' THEN 'Yes' ELSE 'No' END AS 'Cancer',
CASE WHEN HealthConditions LIKE '%CHF%' THEN 'Yes' ELSE 'No' END AS 'CHF',
CASE WHEN HealthConditions LIKE '%Diabetes%' THEN 'Yes' ELSE 'No' END AS 'Diabetes',
CASE WHEN HealthConditions LIKE '%Hypertension (High Blood Pressure)%' THEN 'Yes' ELSE 'No' END AS 'Hypertension (High Blood Pressure)',
AdditionalConditionsDescription AS 'Additional Health Conditions',
MedicationQuestions AS 'Questions about meds',
InsuranceQuestionsDescription AS 'Healthcare concerns'
FROM UMHPSurvey.Assessment
where Doctor = 'Yes' or ConditionQuestions = 'Yes' or AdditionalConditions = 'Yes'
or MedicationQuestions = 'Yes' or Concerns = 'Yes'
INTO OUTFILE '$FILE' FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
EOFMYSQL
The following doesn't
FILE="/var/lib/mysql-files/ConditionAssessment.csv"
mysql --user='root' --password='xxxxxxxxxxxxxxx' UMHPSurvey << EOFMYSQL
SELECT
DateEntry AS 'Entry Date',
FirstName AS 'First Name',
LastName As 'Last Name',
member_id as 'Member ID',
mobile AS 'Mobile',
Doctor AS 'Need help getting connected to a doctor',
CASE WHEN HealthConditions LIKE '%Asthma%' THEN 'Yes' ELSE 'No' END AS 'Asthma',
CASE WHEN HealthConditions LIKE '%COPD%' THEN 'Yes' ELSE 'No' END AS 'COPD',
CASE WHEN HealthConditions LIKE '%Cancer' THEN 'Yes' ELSE 'No' END AS 'Cancer',
CASE WHEN HealthConditions LIKE '%CHF%' THEN 'Yes' ELSE 'No' END AS 'CHF',
CASE WHEN HealthConditions LIKE '%Diabetes%' THEN 'Yes' ELSE 'No' END AS 'Diabetes',
CASE WHEN HealthConditions LIKE '%Hypertension (High Blood Pressure)%' THEN 'Yes' ELSE 'No' END AS 'Hypertension (High Blood Pressure)',
AdditionalConditionsDescription AS 'Additional Health Conditions',
MedicationQuestions AS 'Questions about meds',
InsuranceQuestionsDescription AS 'Healthcare concerns'
FROM UMHPSurvey.Assessment
where Doctor = 'Yes' or ConditionQuestions = 'Yes' or AdditionalConditions = 'Yes'
or MedicationQuestions = 'Yes' or Concerns = 'Yes'
INTO OUTFILE '$FILE'
FIELDS ESCAPED BY '\\'
OPTIONALLY ENCLOSED BY '\"'
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
EOFMYSQL
Giving the error below
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR at line 2: Unknown command '\"'.
I am not sure what I am doing wrong.
I have also tried the following
FILE="/var/lib/mysql-files/ConditionAssessment.csv"
mysql --user='root' --password='xxxxxxxxxxxxxxx' UMHPSurvey << EOFMYSQL
SELECT
DateEntry AS 'Entry Date',
FirstName AS 'First Name',
LastName As 'Last Name',
member_id as 'Member ID',
mobile AS 'Mobile',
Doctor AS 'Need help getting connected to a doctor',
CASE WHEN HealthConditions LIKE '%Asthma%' THEN 'Yes' ELSE 'No' END AS 'Asthma',
CASE WHEN HealthConditions LIKE '%COPD%' THEN 'Yes' ELSE 'No' END AS 'COPD',
CASE WHEN HealthConditions LIKE '%Cancer' THEN 'Yes' ELSE 'No' END AS 'Cancer',
CASE WHEN HealthConditions LIKE '%CHF%' THEN 'Yes' ELSE 'No' END AS 'CHF',
CASE WHEN HealthConditions LIKE '%Diabetes%' THEN 'Yes' ELSE 'No' END AS 'Diabetes',
CASE WHEN HealthConditions LIKE '%Hypertension (High Blood Pressure)%' THEN 'Yes' ELSE 'No' END AS 'Hypertension (High Blood Pressure)',
AdditionalConditionsDescription AS 'Additional Health Conditions',
MedicationQuestions AS 'Questions about meds',
InsuranceQuestionsDescription AS 'Healthcare concerns'
INTO OUTFILE '$FILE'
FIELDS ESCAPED BY '\\'
OPTIONALLY ENCLOSED BY '\"'
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
FROM UMHPSurvey.Assessment
where Doctor = 'Yes' or ConditionQuestions = 'Yes' or AdditionalConditions = 'Yes'
or MedicationQuestions = 'Yes' or Concerns = 'Yes'
EOFMYSQL
Giving the same error (below)
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR at line 2: Unknown command '\"'.
Can anyone help me understand where I am going wrong
Because you're using parameter expansion to substitute $FILE, you also get backslash quoting inside the "here document".
What the shell is sending to MySQL looks like this:
FIELDS ESCAPED BY '\'
Try double-escaping the backslash:
FIELDS ESCAPED BY '\\\\'
You other use of backslash is unaffected.
This is confusing.
Read that section of the the manual carefully.
To debug the here document, replace your call to mysql with cat:
cat - << EOFMYSQL
I'm having trouble writing the regular expression to do what I need in MySQL syntax.
I have the following value for column http_referer in a database table:
https://www.example.com?id=123&true
And I need to return the value of the query string parameter uid (in this case, 123), to plug in to another query.
`SELECT * FROM sites WHERE id = (http_referer REGEXP 'uid=([0-9]+)&?')
This is my query, that doesn't work, probably because I'm trying to pass in a PHP-style regular expression instead of one MySQL can use (however, I understand that MySQL doesn't even support capture groups, so I'm kind of stumped).
If you use MySQL prior to version 8 which doesn't support REGEXP_REPLACE(), you can use SUBSTRING_INDEX() to extract parts of a string:
mysql> set #a = 'some.domain?id=123&true';
mysql> select substring_index( substring_index(#a, '?id=', -1), '&', 1) as result;
+--------+
| result |
+--------+
| 123 |
+--------+
If the position of the parameter whose value you are interested in is not fixed, i.e. not always the first, or if parameters following it are optional and may not be present, it's a bit more tricky and you have to add more logic.
Old topic but this solution might help for people who don't have mysql version 8.
2 notes:
You need a case set for each character. So this example is for
maximum of 3 characters (0 -999).
You can't use this solution when
your string has multiple occurrences. For example
id=123&secondid=456&anotherid=789.
SELECT
CONCAT(
CASE WHEN content LIKE '%id=?1%' THEN '1' ELSE '' END,
CASE WHEN content LIKE '%id=?2%' THEN '2' ELSE '' END,
CASE WHEN content LIKE '%id=?3%' THEN '3' ELSE '' END,
CASE WHEN content LIKE '%id=?4%' THEN '4' ELSE '' END,
CASE WHEN content LIKE '%id=?5%' THEN '5' ELSE '' END,
CASE WHEN content LIKE '%id=?6%' THEN '6' ELSE '' END,
CASE WHEN content LIKE '%id=?7%' THEN '7' ELSE '' END,
CASE WHEN content LIKE '%id=?8%' THEN '8' ELSE '' END,
CASE WHEN content LIKE '%id=?9%' THEN '9' ELSE '' END,
CASE WHEN content LIKE '%id=?0%' THEN '0' ELSE '' END,
CASE WHEN content LIKE '%id=?_1%' THEN '1' ELSE '' END,
CASE WHEN content LIKE '%id=?_2%' THEN '2' ELSE '' END,
CASE WHEN content LIKE '%id=?_3%' THEN '3' ELSE '' END,
CASE WHEN content LIKE '%id=?_4%' THEN '4' ELSE '' END,
CASE WHEN content LIKE '%id=?_5%' THEN '5' ELSE '' END,
CASE WHEN content LIKE '%id=?_6%' THEN '6' ELSE '' END,
CASE WHEN content LIKE '%id=?_7%' THEN '7' ELSE '' END,
CASE WHEN content LIKE '%id=?_8%' THEN '8' ELSE '' END,
CASE WHEN content LIKE '%id=?_9%' THEN '9' ELSE '' END,
CASE WHEN content LIKE '%id=?_0%' THEN '0' ELSE '' END,
CASE WHEN content LIKE '%id=?__1%' THEN '1' ELSE '' END,
CASE WHEN content LIKE '%id=?__2%' THEN '2' ELSE '' END,
CASE WHEN content LIKE '%id=?__3%' THEN '3' ELSE '' END,
CASE WHEN content LIKE '%id=?__4%' THEN '4' ELSE '' END,
CASE WHEN content LIKE '%id=?__5%' THEN '5' ELSE '' END,
CASE WHEN content LIKE '%id=?__6%' THEN '6' ELSE '' END,
CASE WHEN content LIKE '%id=?__7%' THEN '7' ELSE '' END,
CASE WHEN content LIKE '%id=?__8%' THEN '8' ELSE '' END,
CASE WHEN content LIKE '%id=?__9%' THEN '9' ELSE '' END,
CASE WHEN content LIKE '%id=?__0%' THEN '0' ELSE '' END
) id
FROM 'page?id=123' test WHERE test LIKE '%?id=%' and p.domain_id=18
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
Good day, I have a problem wherein the value of days are formatted with MTWThF, ThF, TTh and so on. I am trying to convert into columns. this is my code:
SELECT
dys,
CASE WHEN dys LIKE 'M%' THEN 'M' ELSE '' END AS M,
CASE WHEN (dys LIKE '%T%' AND NOT '%Th%') THEN 'T' ELSE '' END AS T,
CASE WHEN dys LIKE '%W%' THEN 'W' ELSE '' END AS W,
CASE WHEN dys LIKE '%Th%' AND NOT 'T' THEN 'Th' ELSE '' END AS th,
CASE WHEN dys LIKE '%F%' THEN 'F' ELSE '' END AS F,
CASE WHEN (dys LIKE '%S%' AND NOT 'Su') THEN 'S' ELSE '' END AS S,
CASE WHEN (dys LIKE '%Su%' AND NOT 'S') THEN 'Su' ELSE '' END AS Su
FROM sched
GROUP BY dys
and the results are
Tb result
T is till detected on ThF same S from SSu. Do i missing something? Thank you
I am writing a query to display an alias column with respect to a column value.
below is my code
CASE TRIM(channel_id)
WHEN '' THEN 'General'
ELSE 'Specific'
END AS templateType
When the column channel id is empty/null the templateType column should show 'General'
else should show 'Specific'
I am getting wrong output
Can anyone help me please..?
CASE TRIM(IFNULL(channel_id,''))
WHEN '' THEN 'General'
ELSE 'Specific'
END AS templateType
Try this..
CASE WHEN channel_id > '' THEN
'Specific'
ELSE
'General'
END As templateType
Aside:
SELECT CASE WHEN '' = ' ' THEN 'same' ELSE 'different' END
Results:
same