String concatenation as an alias displays 0 in SQLite? - sql

I was looking at this tutorial from W3Schools, where they combine address, zip code and country together as a single column "Address". So I've been playing with the Northwind DB and wanted to do the same thing for employee name:
SELECT EmployeeID, FirstName +" "+ LastName AS Name FROM Employees;
But all I get is 0's in the "Name" column. I figure SQLite has implicitly converted the string to integer, but it doesn't make sense...

Replace + with || wich is the string concatenation operator in SQLite and many other databases.
SELECT EmployeeID, FirstName || ', ' || LastName AS Name FROM Employees;

Related

Oracle SQL - combine two columns

How can I combine two columns in Oracle SQL without using CASE?
I have to combine FirstName and LastName in the same row.
I have a table as below:
FirstName LastName
-----------------------
Ken Chan
John Ng
Joe Lam
The data type of these columns is VARCHAR2.
I try to apply the code as follow
SELECT
CONCAT(LastName, ‘,’, FirstName) AS FullName
FROM
LIST
ORDER BY
Place;
SELECT
LastName, ‘,’, FirstName AS FullName
FROM
LIST
ORDER BY
Place;
But both of them result in the the same error
ORA-00909: invalid number of arguments.
May I also ask how can I not adding the ‘,’ while there is missing LastName or FirstName?
Such as not adding ‘,’ when there is only having LastName Chan. The FullName only display as Chan but not ,Chan.
Thanks a lot for helping me.
Use Concatenation Operator that is ||. It concatenates character strings and results in another character string.
To make , comma optional in case if any of the LastName or firstName field is empty or null, use CASE statement.
Solution for your problem:
SELECT LastName || (CASE WHEN LastName IS NOT NULL AND FirstName IS NOT NULL THEN ',' END) || FirstName as FullName
FROM LIST
ORDER BY place;
Working Example: db<>fiddle Link
For more info on Concatenation Operator follow below link to official docs:
https://docs.oracle.com/cd/B19306_01/server.102/b14200/operators003.htm
Oracle's implementation of the concat() function only permits 2 parameters, and you have tried 3, that is why you got the error message.
It is simpler, in Oracle, to use the more traditional double pipe operator to form a concatenated string.
SELECT LastName || ‘,’ || FirstName as FullName
from LIST
order by Place;

Why do I get an error querying from column alias? [duplicate]

This question already has answers here:
Using an Alias column in the where clause in Postgresql
(6 answers)
access a column aliases in the where clause in postgresql
(2 answers)
How to use new created column in where column in sql?
(2 answers)
Closed 1 year ago.
I'm getting an error querying from the column alias and don't understand why. In the example below, if I run a query from the actual column, no problem. I concatenate first_name and last_name columns into a fullname column alias and then get the output.
SELECT first_name ||' '|| last_name AS fullname
FROM actor;
Output:
Now, if I create a column alias, I get the error. In this example, I'm concatenating first_name and last_name into a fullname column alias, and then query the names between value1 and value2.
SELECT first_name ||' '|| last_name AS fullname
FROM actor;
WHERE fullname BETWEEN 'Zero Cage' AND 'Fred Costner';
Output:
Thanks in advance for your taking the time to help!
In postgres document:
An output column's name can be used to refer to the column's value in ORDER BY and GROUP BY clauses, but not in the WHERE or HAVING clauses; there you must write out the expression instead.
That's according to the SQL standard and may not be very intuitive. The (historic) reason behind this is the sequence of events in a SELECT query. WHERE and HAVING are resolved before column aliases are considered, while GROUP BY and ORDER BY happen later, after column aliases have been applied.
Also note that conflicts between input and output names are resolved differently in ORDER BY and GROUP BY - another historic oddity (with a reason behind it, but potentially confusing nonetheless).
You can use one of the below manners:
Use full both column name
SELECT first_name || ' ' || last_name AS fullname
FROM actor
WHERE first_name || ' ' || last_name BETWEEN :conditio1 AND :conditio2
Use CTE
WITH data s (
SELECT first_name || ' ' || last_name AS fullname
FROM actor
)
SELECT *
FROM data
WHERE fullname BETWEEN :conditio1 AND :conditio2
Use subquery
SELECT *
FROM (
SELECT first_name || ' ' || last_name AS fullname
FROM actor
) tmp
WHERE tmp.fullname BETWEEN :conditio1 AND :conditio2

How to get length of string using Oracle query

In a table I have columns FIRST_NAME and LAST_NAME. I have to concatenate the two names and retrieve names containing more than 12 characters.
I tried the following query:
select *
from (select first_name, last_name
from customer as name
)
where length(NAME) = 12
Select *
from (
select first_name||last_name as name
from customer
)
where length(name)>12
You need only one SELECT
SELECT first_name || last_name AS name
FROM customer
WHERE LENGTH(first_name||last_name) > 12
Optionally TRIM name and last_name from spaces.
As an alternative, if you rewrite your question to check if the total length of first name and last name is more than 12, you probably get a different response from people with a more efficient code. It is not necessary to make Oracle actually concatenate the columns.
Programmers are too literal these days. :)
You are not concatenating the first name and last name.
Also no need to write an inline view, below is the query.
select FIRST_NAME || LAST_NAME as NAME
from CUSTOMER
where length(FIRST_NAME || LAST_NAME) > 12;

Adding columns and returning sum to select list in APEX

I'm having trouble adding three columns: first name, second name and surname together for example "Bob Bobby Smith" and returning them to display in select list item in Oracle Apex. Can't find anything on the internet, i'm not even sure if it's possible.
Are you looking for ||, the string concatenation operator?
select (firstname || ' ' || secondname || ' ' || surname) as fullname
from t;

CREATE VIEW with concatenated fields

I have a table with Last Names, First Names, Hours and GPA's.
How do I create a view that displays a concatenated first name and last name, the StudentID and the GPA of the students who have passed at least 90 hours.
The concatenated names should be separated with one space.
The three column headings should be FullName, StudentID and GPA.
The rows should be sorted by last names, then first names.
Please help. I am lost as to how to approach this.
Use the operator || for concatenation (so you don't have to do nested CONCAT()).
Example:
create view v as
select (firstname || ' ' || lastname) "FullName", GPA, StudentId
from table
where Hours>90
order by lastname, firstname