Select * with specific alias [syntax] - sql

I want to use select * to select all the fields from a table but I also want to use an alias on just one field. Is it possible? If so what would the syntax be.
Example
select *,
item1 as hats
from factory
I have not been able to make anything like this work, Thanks!

I just tried
select *, item1 as hats from factory
on mysql and postgres and on both DBMS it runs fine. Only take into consideration that you get two columns with item1. One column named item1 and one named hats.

That should work, but keep in mind that your item1 column will be duplicated!
Suppose your table has the columns (id, item1, item2) then your proposed select will return (id,item1,item2,hats).

It's valid in MS Sql Server as well but the column you are alias'ng will be duplicated.
Tried the query in Sql server 2005 and it did worked.
select *,col2 as 'customcol' from table1

Yes, it is possible to use that format however, use of Select * is not recommend. It is better to enumerate the columns you want. Some database products may balk if the list of columns in combination with your aliased column produces a duplicate column name. However, again, you can solve this by enumerating the columns.

Related

SQL Selecting and Returning Data Based on Pattern

At my office, one of the tables we use keeps track of our Order Numbers. The problem is that the employees don't enter the number consistantly into the database field.
Some of the examples are listed:
'7-26-13 543006-27031', '345009-27031', 'KWYD-863009-27031'.
I need to to find a way to return just the 'nnnnnn-nnnnn' substring
no matter where in the field it is. Most of the time, this pattern is at the end of the field, but that is not always the case. I've already limited the data records to just those with that pattern using a LIKE expression in my WHERE clause, but I have no idea how to best return just that pattern as a column.
Edit:
We are still using SQL Server 2000
What I'm looking to do is along the lines of:
SELECT SUBSTRING(VendorOrderNo, ??, 12) AS OrderNo
FROM Orders
WHERE VendorOrderNo LIKE '%[0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9]%'
select 'nnnnnn-nnnnn' as employeeid
from table
where employees like'%nnnnnn-nnnnn'
It would be better if you adjust your data first, you can give this a try (its for MYSQL)
SELECT * FROM your_table_name WHERE order_number REGEXP '[0-9]+-[0-9]+';
SQLFIDDLE

Select all values in a column into a string in SQLite?

In SQL Server, I can do this with the help of Cursor (to loop through each row in the result set and build my string). But I don't know how to do the same in SQLite, this task is not like the so-called Pivoting which as far as I know, we have to know the exact rows we want to turn to columns. My case is different, I want to select all the values of a specified column into a string (and then can select as a column of 1 row), this column can have various values depend on the SELECT query. Here is a sample of how it looks:
A | B
------------
1 | 0
8 | 1
3 | 2
... ....
I want to select all the values of the column A into a string like this "183..." (or "1,8,3,..." would be fine).
This can be used as a column in another SELECT, I have to implement this because I need to display all the sub-infos (on each row of column A) as a comma-separated list in another row.
I don't have any idea on this, it seems to need some procedural statements (such as placed in a procedure) but SQLite limits much on how I can do with loop, variable declaration,...I'm really stuck. This kind of task is very common when programming database and there is no reason for me to refuse doing it.
Please help, your help would be highly appreciated! Thanks!
If you're just trying to get all the values from Column A into a single record, then use GROUP_CONCAT:
select group_concat(a, ',')
from yourtable
SQL Fiddle Demo
The main problem is that you are think like an application programmer. SQL does a lot of things for you.
SELECT *
FROM tableA
WHERE A IN (SELECT A
FROM tableB)
No need to resort to cursors, stored procedures and multiple queries.

Displaying RowID in Select * (all) Statement

I am trying to display the RowID alongside all columns from a Select * statement.
I am using Oracle with Toad to run the SQL statement.
I have two tables that I need to compare, but I do not have any unique identifiers to use when sorting the two tables for comparison. So I thought that using the RowID to sort the two tables in order to compare them could help.
Is there a way to add RowID to a Select * statement? I cannot add all the columns names as there are over 50 of them. I will be doing this to multiple sets of tables where the number and name of columns will vary.
Any help or ideas around this would be greatly appreciated.
Thanks in advance,
Marwan
You can do something like
SELECT rowid, a.*
FROM table_name a
But I'm not sure that is actually going to help you. Sorting the data on ROWID is not going to be particularly useful since that is just a physical location on disk. It's just as arbitrary as presenting the data unsorted.

SELECT COUNT(*) ;

I have a database, database1, with two tables (Table 1, Table2) in it.
There are 3 rows in Table1 and 2 rows in Table2. Now if I execute the following SQL query SELECT COUNT(*); on database1, then the output is "1".
Does anyone has the idea, what this "1" signifies?
The definition of the two tables is as below.
CREATE TABLE Table1
(
ID INT PRIMARY KEY,
NAME NVARCHAR(20)
)
CREATE TABLE Table2
(
ID INT PRIMARY KEY,
NAME NVARCHAR(20)
)
Normally all selects are of the form SELECT [columns, scalar computations on columns, grouped computations on columns, or scalar computations] FROM [table or joins of tables, etc]
Because this allows plain scalar computations we can do something like SELECT 1 + 1 FROM SomeTable and it will return a recordset with the value 2 for every row in the table SomeTable.
Now, if we didn't care about any table, but just wanted to do our scalar computed we might want to do something like SELECT 1 + 1. This isn't allowed by the standard, but it is useful and most databases allow it (Oracle doesn't unless it's changed recently, at least it used to not).
Hence such bare SELECTs are treated as if they had a from clause which specified a table with one row and no column (impossible of course, but it does the trick). Hence SELECT 1 + 1 becomes SELECT 1 + 1 FROM ImaginaryTableWithOneRow which returns a single row with a single column with the value 2.
Mostly we don't think about this, we just get used to the fact that bare SELECTs give results and don't even think about the fact that there must be some one-row thing selected to return one row.
In doing SELECT COUNT(*) you did the equivalent of SELECT COUNT(*) FROM ImaginaryTableWithOneRow which of course returns 1.
Along similar lines the following also returns a result.
SELECT 'test'
WHERE EXISTS (SELECT *)
The explanation for that behavior (from this Connect item) also applies to your question.
In ANSI SQL, a SELECT statement without FROM clause is not permitted -
you need to specify a table source. So the statement "SELECT 'test'
WHERE EXISTS(SELECT *)" should give syntax error. This is the correct
behavior.
With respect to the SQL Server implementation, the FROM
clause is optional and it has always worked this way. So you can do
"SELECT 1" or "SELECT #v" and so on without requiring a table. In
other database systems, there is a dummy table called "DUAL" with one
row that is used to do such SELECT statements like "SELECT 1 FROM
dual;" or "SELECT #v FROM dual;". Now, coming to the EXISTS clause -
the project list doesn't matter in terms of the syntax or result of
the query and SELECT * is valid in a sub-query. Couple this with the
fact that we allow SELECT without FROM, you get the behavior that you
see. We could fix it but there is not much value in doing it and it
might break existing application code.
It's because you have executed select count(*) without specifying a table.
The count function returns the number of rows in the specified dataset. If you don't specify a table to select from, a single select will only ever return a single row - therefore count(*) will return 1. (In some versions of SQL, such as Oracle, you have to specify a table or similar database object; Oracle includes a dummy table (called DUAL) which can be selected from when no specific table is required.)
you wouldn't normally execute a select count(*) without specifying a table to query against. Your database server is probably giving you a count of "1" based on default system table it is querying.
Try using
select count(*) from Table1
Without a table name it makes no sense.
without table name it always return 1 whether it any database....
Since this is tagged SQL server, the MSDN states.
COUNT always returns an int data type value.
Also,
COUNT(*) returns the number of items in a group. This includes NULL
values and duplicates.
Thus, since you didn't provide a table to do a COUNT from, the default (assumption) is that it returns a 1.
COUNT function returns the number of rows as result. If you don't specify any table, it returns 1 by default. ie., COUNT(*), COUNT(1), COUNT(2), ... will return 1 always.
Select *
without a from clause is "Select ALL from the Universe" since you have filtered out nothing.
In your case, you are asking "How many universe?"
This is exactly how I would teach it. I would write on the board on the first day,
Select * and ask what it means. Answer: Give me the world.
And from there I would teach how to filter the universe down to something meaningful.
I must admit, I never thought of Select Count(*), which would make it more interesting but still brings back a true answer. We have only one world.
Without consulting Steven Hawking, SQL will have to contend with only 1.
The results of the query is correct.

SQL copy sum of 2 varchars to to new table

tis my first question so sorry if its not well structured, I have looked for the answer for a while but no joy so here goes..
basically I have 20 columns and want to take the result of adding columns (a+b) (b+c) etc and make this the value of my new columns,
when i do a simple select statement the values appear as expected but i cant seem to get them to appear into a new table
the columns are varchars
this is one of the 20 select queries
((accidentlogs.before_T18/16-accidentlogs.before_T19/16)/21.954),
It seems like such an easy function and it probably is but stick a fork in me on this one
You can use the result of a SELECT Statement as a value for an INSERT Statement. The exact syntax may vary for the SQL Dialect you use (Oracle, Postgres, MySql...)
This is the code for postgres:
INSERT INTO table (field1, field2...) SELECT 'value1', 'value2'...