output with single quote in sql - mysql

Table Values
CNAME
Firstname
Amount
Postalcode
Lastname
Accountnumber
REQUIRED O/P
CNAME
'Firstname'
'Amount'
'Postalcode'
'Lastname'
'Accountnumber'

In mysql you can use the function concat():
SELECT CONCAT("'", CNAME, "'") FROM yourTable
In oracle you can use the same function as above concat() or the concatenation operator:
SELECT '''' || CNAME || '''' FROM yourTable;
SELECT CONCAT('''', CNAME, '''') FROM yourTable;

Related

SQL ignore parenthesis in name field for ORDER BY

I would like rows returned in a MySQL query to be sorted alphabetically by surname for which I have an SQL query like:
SELECT
id,
substring_index(name, ' ', -1) as surname
FROM
my_table
ORDER BY
surname asc
However, some names have parenthesis to denote some special circumstance such as: Laura Angel (retired)
How can I modify my SQL above to ignore the parenthesised text, to sort by surname alphabetically?
Try with nested replaces to remove the parentheses.
SELECT
id,
substring_index(name, ' ', -1) as surname
ORDER BY
REPLACE( REPLACE( surname , '(' , '') , ')' , '') ASC;
Test and modify according to you version of SQL.
Not tested.
You can use this solution:
SELECT id,
substring_index(rtrim(substring_index(name, '(', 1)), ' ', -1) as surname
FROM test.test
ORDER BY
surname asc;

SQL Compound query if-else or Case Statement

I have a table with Customer_ID, Customer_Name, Address1, Address2, Mail_Address1, Mail_Address2, etc etc.
In some cases, Mail_Address1 = 'Same' or ''.... which basically means that I need to extract Address1 + Address2 otherwise ignore and take Mail_Address1 + Mail_Address2.
My pseudo SQL Query looks like:-
// The query that would give me all the records with the same address as Address1 for Mailing Address //
SELECT Customer_ID, Customer_Name, Address1 + ', ' + Address2 as Address, ....
FROM Customers
Where Customer_ID != 0 AND (Mail_Address1 ='Same' or Mail_Address1 ='')
AND
SELECT Customer_ID, Customer_Name, Mail_Address1 + ', ' + Mail_Address2 as Address, ....
FROM Customers
Where Customer_ID != 0 AND Mail_Address1 !='Same' AND Mail_Address1 !=''
How do I combine these two queries so that I can have one query which would choose the correct address depending on the condition. (Same or empty string).
Have you considered a CASE statement?
SELECT
CASE
WHEN Customer_ID != 0 AND (Mail_Address1 ='Same' OR Mail_Address1 = '')
THEN Address1 + ', ' + Address2
ELSE Mail_Address1 + ', ' + Mail_Address2
END AS address
FROM Customers

Use concat with as inside concat funcation in MySQL

I have three columns in my table: firstName, lastName and jobTitle.
I want to concat firstName and lastName as Fullname then concat Fullname and jobTitle.
How can I do that?
You would do:
select concat_ws(' ', firstName, lastName) as Name,
concat_ws(' ', firstName, lastName, jobTitle) as NameTitle
You cannot re-use a column alias in the same select, so you have to repeat the expression.
EDIT:
If you want one column of that form, then perhaps:
select concat(firstname, ' ', lastname, ', ', jobtitle)
I don't believe you can use a column alias for this, you'd have to simply select all three of the columns at once.
CONCAT(firstName, lastName, jobTItle) as nameJob
Maybe like that
SELECT CONCAT(firstname, ' ', lastname) as fullname,CONCAT(fullname,'-',jobTitle) as info
FROM yourTable
another example
SELECT CONCAT(firstname, ' ', lastname) as fullname,CONCAT(fullname,'-',jobTitle) as info
FROM yourTable WHERE CONCAT(firstname, ' ', lastname) = "Bob Marley"

MySql Error "Unknown column 'inDate' in 'where clause'"

I have been trying to run this stored procedure in MySql. But its throwing an Error:
"Unknown column 'inDate' in 'where clause"
CREATE DEFINER=`root`#`localhost` PROCEDURE `Cus_Emails`(inDate varchar(10))
truncate table tt;
insert tt(CompanyName, CustomerNumber, ServicePoint, EmailAddressSequenc, EmailAddress,
CustomerBusinessUnit)
select distinct '01' CompanyName, s.CustomerNo CustomerNumber, if(d.LINE_DESC like '%/%', substring_index(d.LINE_DESC, '/', -1), '') ServicePoint, '01' EmailAddressSequenc, 'Invoice.inbox#healthscope.com.au' EmailAddress, s.ServicePoint CustomerBusinessUnit
from doc_details d join doc_refs r on d.REF_ID=r.REF_ID
Join service_points s On s.CustomerNo=Trim(substring_index(substring_index(d.LINE_DESC, '/', 1), ' ', -1))
where d.ENTRY_ID like concat(inDate, '%') and d.PAGE_NUM=1 And d.LINE_NUM in (1,2) And (d.LINE_DESC like '-%' Or d.LINE_DESC Like '0%') and d.doc_type='INV'
Union
select distinct '01' CompanyName, s.CustomerNo CustomerNumber, if(d.LINE_DESC like '%/%', substring_index(d.LINE_DESC, '/', -1), '') ServicePoint, '01' EmailAddressSequenc, 'Invoice.inbox#healthscope.com.au' EmailAddress, s.ServicePoint CustomerBusinessUnit
from doc_details d join doc_refs r on d.REF_ID=r.REF_ID
Join service_points s On s.CustomerNo=Trim(substring_index(substring_index(d.LINE_DESC, '/', 1), ' ', -1))
where d.ENTRY_ID like concat(inDate, '%') and d.PAGE_NUM=1 And d.LINE_NUM in (3) and d.doc_type='CRE';
Check the MySQL docs:
I'd guess that ....
routine_body:
Valid SQL procedure statement
means you have to enclose your statements (truncate, insert) in BEGIN END. Otherwise routine_body is only the truncate and the variable inDate is out of procedure scope.
Because, in your table, you don't have inDate column.
Check the column name again and correct it!

How to remove NULL results on concatenation

I have the following query:
SELECT concat(first_name, ' ', last_name) as full_name FROM auth_user
How would I do the equivalent of:
SELECT concat(first_name, ' ', last_name) as full_name FROM auth_user
**WHERE full_name IS NOT NULL**
(which produces a Unknown column 'full_name' in 'where clause')
Two ways around this:
SELECT concat(first_name, ' ', last_name) as full_name FROM auth_user
WHERE first_name IS NOT NULL and last_name is not null
or,
select * from (
SELECT concat(first_name, ' ', last_name) as full_name FROM auth_user)
where full_name is not null
SELECT concat(IF(first_name IS NOT NULL, first_name, ''), ' ', IF(last_name IS NOT NULL, last_name, '')) as full_name FROM auth_user;
The difference between using IS NOT NULL in the where clause and using it in the IF clause is that you the where clause will eliminate (filter) the rows with null values, while the if clause will get all the rows but with a full_name that will not be "Unknown" it will just be missing the null components