SQL using REPLACE on one of many columns - sql

I have the following query:
SELECT * FROM MailingList
There are about 20+ columns in the MailingList table, one which is called Address. This column has some fields which contain commas, which I need to take out. So I updated my query to:
SELECT REPLACE(Address, ',', '') AS Address, * FROM MailingList
But now I have two Address columns. Is there a way to only display one Address column while still using the wildcard (*) for all the other columns?

There is not a way to do this, though listing the columns you want explicitly is a good idea anyway.

You can trick as following query:
Get the data into a temp table
Drop the cloumns that are not needed
Get results and drop temp table
SELECT *, REPLACE(Address, ',', '') AS Address2
INTO #TempTable
FROM MailingList
ALTER TABLE #TempTable
DROP COLUMN [Address]
SELECT * FROM #TempTable
DROP TABLE #TempTable

I agree with Shadow - avoid using the * wild card if you can...
I know listing out ALL of the columns in select statement for big tables is a pain so here is a quick short cut you may not be aware of: In SQL Server Management Studio, browse through the object explorer and find the table you want to select from (MailingList). Right-click it to view the context menu and choose "Script Table as" then "SELECT TO" then "New Query Editor Window". This will create a new select statement with each column spelled out. In the future, use this method to create select statements, queries, procedures, etc. rather then the * wildcard. Performance is better and it just looks nicer :-)
Then you can solve your alias issue with the replace function.

Related

VBA Access Table reference in SQL query

I have been running into trouble executing SQL code in VBA Access when I refer to certain Table names.
For example,
INSERT INTO TempTable (ClientName) SELECT DISTINCT 1_1_xlsx.ClientName FROM 1_1_xlsx'<--does not work
The code works fine when I changed the Table name from 1_1_xlsx to Stuff.
INSERT INTO TempTable (ClientName) SELECT DISTINCT Stuff.ClientName FROM Stuff '<--works
I have no idea why the first query results in a syntax error and the second code is runs fine even when they refer to the same thing. I suspect it should be the naming conventions but I could not find any concrete answers.
Also, are there any ways that I could use 1_1_xlsx as my table name? Or am I just writing my query wrong?
try this:
INSERT INTO TempTable (ClientName) SELECT DISTINCT [1_1_xlsx].ClientName FROM [1_1_xlsx]
In many SQL based databases you can't have a table name or field name that starts with a number.
I suspect this is the underlying reason for your problem. Although Access will allow it, I have seen it cause problems in the past.
The problem is the number at the beginning of the table name. That is bad -- because it confuses the parser.
This is a bad table name, but SQL allows you to define table aliases. And, in this case, you don't even need to repeat the table name. So, here are two simple solutions:
INSERT INTO TempTable (ClientName)
SELECT DISTINCT ClientName
FROM 1_1_xlsx;
Or:
INSERT INTO TempTable (ClientName)
SELECT DISTINCT t.ClientName
FROM 1_1_xlsx as t
There is no reason to use the complete table name as an alias. That just makes the query harder to write and to read.

How to Unselect The Field in select Query using sql

hi in my database i am store more than 50 field with primarykey (Auto increment) i am not sure about the fields name but i wants to select the entire data in that table , i am using
SELECT * FROM tablename
i want to select all the fields except that ID but this query populate the entire table so is there is possible to unselect the particular field in the select query. Can anyone have an idea please guide me. Thanks in Advance
The * indicates that you want to select ALL fields from a given table. If you want to select only a few fields, or all but one, then you will need to specify the ones you want manually:
select field1,field2,field3 from tablename
The SQL standard does not offer an "except" notation. It would be neat if we could
select t.* -t.ID
from some_table t
/
but it is not supported.
On the other hand, SELECT * is a dangerous construct. It is always better to explicitly list the columns we want in any given situation.

Possible to exclude or reorder a column from `*`? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
SQL exclude a column using SELECT * [except columnA] FROM tableA?
Is it possible to exclude a column from a select * from table statement with SQL Server?
I have a need for this and this is my only option other than parsing a raw SQL string to get out the required field names (I really don't want to do that).
Just to be bold. When the query is made I do not have access to the list of fields needed from the table but I do know which field I do not need. This is part of a complex multi-part query.
Surely there must be some way even if it's "hackish" such as using table variables or views
My other option is to reorder the columns. My problem is with ExecuteScalar SQL functions which get the first row and first column.
EDIT
I can't add an answer since this is now closed but the way I ended up doing it was like so:
;with results_cte as (
select (calculation) as calculated_column, * from table
)
select * into #temptable from results_cte
where calculated_column<10 /*or whatever*/
alter table #temptable
drop column calculated_column
select * from #temptable
drop table #temptable
Nope. You'll have to build your statement manually or just select *.
No.
Instead, you could check syscolumns to get all of the field names, or (perhaps) SELECT * and ignore that column.
If you use dynamic SQL, you can generate the query from metadata about the table or view (INFORMATION_SCHEMA.COLUMNS) and exclude columns that way. I do this a lot to generate triggers or views.
But there is nothing in the SQL language which supports this.
The best way to handle this would be to select * and then just not present the excluded column to your users in your frontend. As others have noted, SQL has no direct capability of doing an all-columns-except construct.

how to convert result of an select sql query into a new table in ms access

how to convert result of an select sql query into a new table in msaccess ?
You can use sub queries
SELECT a,b,c INTO NewTable
FROM (SELECT a,b,c
FROM TheTable
WHERE a Is Null)
Like so:
SELECT *
INTO NewTable
FROM OldTable
First, create a table with the required keys, constraints, domain checking, references, etc. Then use an INSERT INTO..SELECT construct to populate it.
Do not be tempted by SELECT..INTO..FROM constructs. The resulting table will have no keys, therefore will not actually be a table at all. Better to start with a proper table then add the data e.g. it will be easier to trap bad data.
For an example of how things can go wrong with an SELECT..INTO clause: it can result in a column that includes the NULL value and while after the event you can change the column to NOT NULL the engine will not replace the NULLs, therefore you will end up with a NOT NULL column containing NULLs!
Also consider creating a 'viewed' table e.g. using CREATE VIEW SQL DDL rather than a base table.
If you want to do it through the user interface, you can also:
A) Create and test the select query. Save it.
B) Create a make table query. When asked what tables to show, select the query tab and your saved query.
C) Tell it the name of the table you want to create.
D) Go make coffee (depending on taste and size of table)
Select *
Into newtable
From somequery

How to know where AS keyword should be used?

There is a AS keyword in TSQL but in what situations I should use it ?
For example : Create View StatisticalData AS Select * from People
We used AS keyword in this statement but when creating tables we don't use it , I mean I am kinda confused.
Could you please tell me, in what kinda position I should use AS. I mean it is used to assign value to a variable?
Thanks in advance.
Main uses:
Aliases for tables and columns
Between CREATE and definition
CAST AS newtype
Examples
SELECT
foo AS tom,
foo + bar AS dick,
CAST(bar AS varchar(50)) AS harry
FROM
fizz AS f
CREATE VIEW/PROC/FUNCTION etc
AS
... proc or view of udf etc definition
GO
The as keyword is used when you create or alter views, procedures, functions and triggers.
Example:
create procedure Test
as
select 1
The keyword is also used with a different meaning to create aliases for tables and fields in a query.
Example:
select u.usnm as UserName, u.pwd as UserPassword
from applicationusertable as u
AS is simply telling SQL that you want to name or type the item before as the name or statment after for example.
SUM(A + B) AS MyTotal
CREATE View MyView Column1, Column2 AS SELECT * From TABLE
SELECT MyColumn(s) FROM table_name AS MyAlias
So basically, AS just casts the item before as the item after, being an alias.
See http://www.w3schools.com/SQl/sql_alias.asp for a better explanation than I could give.
and many more examples.
Regards RE
The AS keyword basically means that you want to 'alias' someting to something else.
The most comman being table anmes and field names...
SELECT
[p].fn AS FirstName
FROM
people AS [p]
Line 2 aliases the field [fn] to the new name [FirstName].
Line 4 aliases the table [people] to the new name [p].
In your view example, and for Stored Procedures, etc, the AS is saying treat this AS . So when you use the name, the database engine treats it AS the code yuou just wrote...
SQL was once referred to as "Structured English Query Language". It's all trying to make the programatic syntax as similar as possible to English syntax.
It's part of the statement in that case defining what to declare the View 'as'.
In other cases, where you can use the AS statement in a SELECT statement on either a field or table, it's option.
The AS keyword is primarily used to create aliases (view, table, column, etc.).
View
Your example works fine. The View name you specified is an alias for the query that follows.
Table
select * from Table1 as t
inner join Table2 as t2
on t.id = t2.tid
Column
--will return a column called sum with a single row of 3
select 1+2 as sum
AS is used all over the place:
CAST('1' AS INT)
SELECT CAST('1' AS INT) AS Col1
WITH ThisCTE AS (SELECT CAST('1' AS INT) AS Col1)
SELECT Col1 FROM ThisCTE AS a
CREATE VIEW ThisView AS
WITH ThisCTE AS (SELECT CAST('1' AS INT) AS Col1)
SELECT Col1 FROM ThisCTE AS a
etc ad nauseum.
Bite the bullet, open up BOL, and start reading!
When you use the CREATE TABLE or VIEW statement, the AS keyword pulls in the records from the statement following, to fill the Table or View with initially.
So your example View will start as an exact copy of the People table.
This allows you choose the fields that you want to load into your Table or View.