I'm working in Netezza -- or, you know, pure data for Analytics -- nzsql, but I think this is an ANSI SQL question. The question is so basic, I don't even know how to search for it.
CREATE TEMPORARY TABLE DEMO1 AS SELECT 'SMORK' AS SMORK, 'PLONK' AS PLONK, 'SPROING' AS SPROING;
SELECT SMORK AS PLONK, PLONK, SPROING AS CLUNK, CLUNK
FROM DEMO1;
This returns 'SMORK, PLONK, SPROING, SPROING', which is to say, the query is fine reusing the CLUNK alias, but the PLONK alias is overwritten by the column from the source table. Now, if I really wanted the column from the source table, I could write SELECT SMORK AS PLONK, DEMO1.PLONK et c, but I don't know how to specify that I would prefer the alias I've defined earlier in same the SELECT clause.
Does anybody know a way?
In Netezza, when selecting a column, Netezza will search for table column first, and then alias.
Example:
Suppose we have the following statements:
CREATE TEMPORARY TABLE EMPLOYEES AS
SELECT 1001 AS EMPLOYEE_ID
,'Alex' AS FIRST_NAME
,'Smith' AS LAST_NAME
,'Alex J. Smith' AS FULL_NAME;
SELECT
EMPLOYEE_ID
,FIRST_NAME
,LAST_NAME
,LAST_NAME||', '||FIRST_NAME AS FULL_NAME
,'My full name is :'||FULL_NAME AS DESCRIPTION
FROM EMPLOYEES;
It will return
EMPLOYEE_ID FIRST_NAME LAST_NAME FULL_NAME DESCRIPTION
1001 Alex Smith Smith, Alex My full name is :Alex J. Smith
Notice in DESCRIPTION, the FULL_NAME value is picked from table column, not from alias.
If you want DESCRIPTION column use value from alias FULL_NAME, you can do it in two steps:
Step 1. Create a sub-query includes all columns you want. For all alias names you want to reuse, you need to name them as names not exist in any table columns on your FROM clause;
Step 2. SELECT only column you want from the subquery.
CREATE TEMPORARY TABLE EMPLOYEES AS SELECT 1001 AS EMPLOYEE_ID, 'Alex' AS FIRST_NAME, 'Smith' AS LAST_NAME, 'Alex J. Smith' AS FULL_NAME;
WITH EMPLOYESS_TMP AS (
SELECT
EMPLOYEE_ID
,FIRST_NAME
,LAST_NAME
,LAST_NAME||', '||FIRST_NAME AS FULL_NAME2
,FULL_NAME2 AS FULL_NAME
,'My full name is :'||FULL_NAME2 AS DESCRIPTION
FROM EMPLOYEES)
SELECT
EMPLOYEE_ID
,FIRST_NAME
,LAST_NAME
,FULL_NAME
,DESCRIPTION
FROM EMPLOYESS_TMP;
This will return what you want:
EMPLOYEE_ID FIRST_NAME LAST_NAME FULL_NAME DESCRIPTION
1001 Alex Smith Smith, Alex My full name is :Smith, Alex
Just change the order of your columns. Netezza tries to use your alias so you can either rename the column or change the order.
SELECT SMORK AS PLONK, PLONK, CLUNK, SPROING AS CLUNK
FROM DEMO1;
Related
I'm using SQL Server and I have a column called "Full Name" that I pulled from a separate table. I have a column for First Name & Last name (among many other columns).
Here is the code that I found and used and it worked:
SELECT Full_Name,
LEFT(Full_Name,CHARINDEX(' ',Full_Name )-1) AS First_Name,
RIGHT(Full_Name, LEN(Full_Name)- CHARINDEX(' ', Full_Name)) AS Last_Name
FROM table_name
That extracted the name, but upon viewing the entire table using this command:
SELECT * FROM table_name
I don't see it? Is there a method that I can use to insert the data into the First_name and Last_name columns without adding any additional Rows
Thank you !
Perhaps creating a view wil be sufficient:
CREATE VIEW table_name_v
SELECT Full_Name,
LEFT(Full_Name,CHARINDEX(' ',Full_Name )-1) AS First_Name,
RIGHT(Full_Name, LEN(Full_Name)- CHARINDEX(' ', Full_Name)) AS Last_Name
FROM table_name
Then instead of using the table name in subsequent queries you use the view instead
SELECT * FROM table_name_v
The problem you face otherwise is that if you add first and last name columns to this table that you now end-up with 3 columns to maintain, or that you may need to replace the data entry screen(s) that use full_name to start using first and last name columns instead.
If you really do want to proceed by adding the columns, you could try using "computed columns" which would avoid the necessity to change data entry screens etc.
CREATE TABLE mytable(
full_name VARCHAR(15) NOT NULL
);
INSERT INTO mytable(full_name) VALUES ('fred flintstone');
select * from mytable
full_name
fred flintstone
alter table mytable
add
first_name as LEFT(Full_Name,CHARINDEX(' ',Full_Name )-1)
, Last_Name as RIGHT(Full_Name, LEN(Full_Name)- CHARINDEX(' ', Full_Name))
select * from mytable
full_name
first_name
Last_Name
fred flintstone
fred
flintstone
db<>fiddle for computed columns here
If you realy do want to store these 2 separate columns (rather than computing them) then you need to add the columns to your table and run an update statement to populate them as follows:
alter table mytable
add
first_name varchar(100)
, last_name varchar(100)
update mytable
set
first_name = LEFT(Full_Name,CHARINDEX(' ',Full_Name )-1)
, Last_Name = RIGHT(Full_Name, LEN(Full_Name)- CHARINDEX(' ', Full_Name))
db<>fiddle for adding columns and updating here
Select first names and last names of the table teachers where first names end in 'n'.
I wrote:
select first_name, last_name
from teachers
where type='teacher'
and last_name like '%n';
Unfortunately it selects no rows. It should select at least two.
The problem is with " last_name like '%n' " and I can't find a solution. Does anyone have any idea how i can solve this?Screenshot
The thing is, in your example there are no first_name columns that end with n. Yep.
Why? The the column type of first_name is CHAR instead of VARCHAR2, so Oracle fills up the column with spaces when each first name is shorter than 10 characters.
Solutions?
Use VARCHAR2 instead of CHAR as in:
create table teacher (
last_name varchar2(10), -- VARCHAR2 now!
first_name varchar2(10), -- VARCHAR2 now!
type varchar2(10)
);
With this structure your query will run well.
Alternatively, if you don't want to change the table structure, you can TRIM() the column, as in:
select first_name, last_name
from teachers
where type='teacher'
and trim(first_name) like '%n'; -- use TRIM here!
Try using. This will eliminate the chances of leftout of Case sensitive Data.
select first_name, last_name from teachers where lower(type)='teacher' and lower(first_name ) like '%n';
as per the screenshot the first_name, last_name columns have datatype as CHAR.
the thing about CHAR is that is appends spaces to reach its full length.
Example: for first_name CHAR(10) if u store 'Andra' it will be stored as 'Andra '
so use the trim function:
select first_name, last_name
from teachers
where type='teacher'
and trim(first_name) like '%n';
alternatively use the VARCHAR2 datatype instead of CHAR.
it does not append spaces at the end to reach its full length.
As you were told, your query should work. Problem is probably trivial (does case matters? Are there really lower case "n" letters in those names?).
Meanwhile, a few options for you:
SQL> select ename from emp
2 where ename like '%N';
ENAME
----------
ALLEN
MARTIN
SQL> select ename from emp
2 where substr(ename, -1) = 'N';
ENAME
----------
ALLEN
MARTIN
SQL> select ename from emp
2 where regexp_like(ename, 'N$');
ENAME
----------
ALLEN
MARTIN
SQL>
[EDIT: CHAR datatype issue]
SQL> create table empc (ename char(10));
Table created.
SQL> insert into empc select ename from emp;
12 rows created.
SQL> select ename from empc where ename like '%N';
no rows selected
SQL> select ename from empc where trim(ename) like '%N';
ENAME
----------
ALLEN
MARTIN
SQL>
If you have char data type then you should try trimming of space chars from your first_name values
select first_name, last_name
from teachers
where type='teacher'
and trim(first_name) like '%n';
anyway in your screeshot you have not teacher with firt_name endig with n ..try
select first_name, last_name
from teachers
where trim(first_name) like '%n';
You could try something like
SELECT *
FROM teachers a
WHERE TRIM (LOWER (a.first_name)) LIKE '%n';
SQL Rookie here. Using DB2.
I have a table People with attributes FirstName VARCHAR(20), Salary REAL and some others. I want to query with SELECT FirstName, Salary FROM People and receive this as the output:
FirstName Salary
James 1000
but instead I get Salary in E Notation (because it was created as REAL):
FirstName Salary
James +1.00000E+003
How do I format the query to convert the values in Salary as numeric?
I tried using SELECT INTEGER(Salary) but it changes the Salary attribute header in the output to 2.
Your query:
SELECT FirstName, INTEGER(Salary)
FROM People;
Does not assign a name to the second column. You assign a name using as:
SELECT FirstName, INTEGER(Salary) as Salary
FROM People;
Here is the problem I am working on:
Select all the Oracle database employees whose last names end with “s” Change the
heading of the column to read Possible Candidates.
I tried
SELECT last_name AS possible_candidates FROM * WHERE last_name LIKE '%s';
That returned the error :
ORA-00903: invalid table name
Unless I am reading the question wrong how do I check the entire database for something like this?
To answer the question which is asking for employee names not just last names you would select all employees with a last name that ends in S.
SELECT *
FROM employees
WHERE last_name LIKE '%s'
If the table has more than a first_name and last_name column you can do
SELECT first_name, last_name
FROM employees
WHERE last_name LIKE '%s'
Now to combine your two parts (Select employees and rename column)
Run one of the two queries above but add an AS statement as show in the example below.
SELECT *
AS Possible_Candidates
FROM employees
WHERE last_name LIKE '%s'
Based on feedback: this appears to be your answer...
Is there a table called employees? if that I would interpret the question as from the employees table. Otherwise, you could select table_name from All_TAB_COLS where column_name like = 'LAST_NAME' and then build a union from the resulting tables all of which have a field called 'LAST_NAME'
i want to display the employee names which having names start with a and b ,it should be like list will display employees with 'a' as a first letter and then the 'b' as a first letter...
so any body tell me what is the command to display these...
To get employee names starting with A or B listed in order...
select employee_name
from employees
where employee_name LIKE 'A%' OR employee_name LIKE 'B%'
order by employee_name
If you are using Microsoft SQL Server you could use
....
where employee_name LIKE '[A-B]%'
order by employee_name
This is not standard SQL though it just gets translated to the following which is.
WHERE employee_name >= 'A'
AND employee_name < 'C'
For all variants you would need to consider whether you want to include accented variants such as Á and test whether the queries above do what you want with these on your RDBMS and collation options.
select columns
from table
where (
column like 'a%'
or column like 'b%' )
order by column asc
Regular expressions work well if needing to find a range of starting characters. The following finds all employee names starting with A, B, C or D and adds the “UPPER” call in case a name is in the database with a starting lowercase letter. My query works in Oracle (I did not test other DB's). The following would return for example:
Adams
adams
Dean
dean
This query also ignores case in the ORDER BY via the "lower" call:
SELECT employee_name
FROM employees
WHERE REGEXP_LIKE(UPPER(TRIM(employee_name)), '^[A-D]')
ORDER BY lower(employee_name)
select employee_name
from employees
where employee_name LIKE 'A%' OR employee_name LIKE 'B%'
order by employee_name
What cfengineers said, except it sounds like you will want to sort it as well.
select columns
from table
where (
column like 'a%'
or column like 'b%' )
order by column
Perhaps it would be a good idea for you to check out some tutorials on SQL, it's pretty interesting.
If you're asking about alphabetical order the syntax is:
SELECT * FROM table ORDER BY column
the best example I can give without knowing your table and field names:
SELECT * FROM employees ORDER BY name
select name_last, name_first
from employees
where name_last like 'A%' or name_last like 'B%'
order by name_last, name_first asc
select *
from stores
where name like 'a%' or
name like 'b%'
order by name
Here what I understood from the question is starting with "a " and then "b"
ex:
abhay
abhishek
abhinav
So there should be two conditions and both should be true means you cant use "OR" operator
Ordered by is not not compulsory but its good if you use.
Select e_name from emp
where e_name like 'a%' AND e_name like '_b%'
Ordered by e_name
From A to Z:
select employee_name from employees ORDER BY employee_name ;
From Z to A:
select employee_name from employees ORDER BY employee_name desc ;
Oracle:
Just felt to do it in different way. Disadvantage: It doesn't perform full index scan. But still gives the result and can use this in substring.
select employee_name
from employees
where lpad(employee_name,1) ='A'
OR lpad(employee_name,1) = 'B'
order by employee_name
We can use LEFT in SQL Server instead of lpad . Still suggest not a good idea to use this method.
We can also use REGEXP
select employee_name
from employees
where employee_name REGEXP '[ab].*'
order by employee_name