Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Using the field alias name in WHERE clause

 
   Database Help (Home) -> Programming RSS
Next:  Working with Active Directory from SQL Server 200..  
Author Message
Alex

External


Since: May 15, 2008
Posts: 4



(Msg. 1) Posted: Thu May 15, 2008 9:30 am
Post subject: Using the field alias name in WHERE clause
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Hi,

Is there anyway to use the field alias name within the WHERE part of a
SQL statement in MS SQL Server 2005? Below is an example:

Select
Table1.Name as FullName,
Table1.Address as Address
from
Table1
Where
FullName like '%test%'

What I'm doing is writing a Stored Procedure that accepts a number of
parameters, then dynamically creates a SQL statement to pull in data
for a look-up table. Given that the field names may change we're
using field aliasing. I'm adding two parameters for filter field and
filter value, but with me not wanting to hardcode field names within
the application, I'd like to use the alias here as well... but it
would be dynamically entered into the WHERE part of the SQL statement.

I know some folks will be asking 'why do you want to do this', so I
figured I'd throw out a quick summary of my purpose. But bottom line
is i'd like to find someway to use the alias name within the WHERE
clause.

Thanks --

Alex

 >> Stay informed about: Using the field alias name in WHERE clause 
Back to top
Login to vote
Alex

External


Since: May 15, 2008
Posts: 4



(Msg. 2) Posted: Thu May 15, 2008 12:18 pm
Post subject: Re: Using the field alias name in WHERE clause [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On May 15, 11:33 am, "Aaron Bertrand [SQL Server MVP]"
wrote:
> The, the SELECT list is parsed second-last (before ORDER BY) so the rest of
> the query knows nothing about it.  Use a CTE, e.g.
>
> WITH t AS
> (
>     SELECT FullName = Table1.Name,
>         Address = Table1.Address
>     FROM Table1
> )
> SELECT FullName, Address
> FROM t
> WHERE FullName LIKE '%test%';
>
> "Alex" wrote in message
>
>
>
>
>
> > Hi,
>
> > Is there anyway to use the field alias name within the WHERE part of a
> > SQL statement in MS SQL Server 2005?  Below is an example:
>
> > Select
> >   Table1.Name as FullName,
> >   Table1.Address as Address
> > from
> >   Table1
> > Where
> >   FullName like '%test%'
>
> > What I'm doing is writing a Stored Procedure that accepts a number of
> > parameters, then dynamically creates a SQL statement to pull in data
> > for a look-up table.  Given that the field names may change we're
> > using field aliasing.  I'm adding two parameters for filter field and
> > filter value, but with me not wanting to hardcode field names within
> > the application, I'd like to use the alias here as well... but it
> > would be dynamically entered into the WHERE part of the SQL statement.
>
> > I know some folks will be asking 'why do you want to do this', so I
> > figured I'd throw out a quick summary of my purpose.  But bottom line
> > is i'd like to find someway to use the alias name within the WHERE
> > clause.
>
> > Thanks --
>
> > Alex- Hide quoted text -
>
> - Show quoted text -

That is exactly what I was looking for ... thanks !

Alex

 >> Stay informed about: Using the field alias name in WHERE clause 
Back to top
Login to vote
Aaron Bertrand [SQL Serve

External


Since: Jan 10, 2008
Posts: 2166



(Msg. 3) Posted: Thu May 15, 2008 12:33 pm
Post subject: Re: Using the field alias name in WHERE clause [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

The, the SELECT list is parsed second-last (before ORDER BY) so the rest of
the query knows nothing about it. Use a CTE, e.g.

WITH t AS
(
SELECT FullName = Table1.Name,
Address = Table1.Address
FROM Table1
)
SELECT FullName, Address
FROM t
WHERE FullName LIKE '%test%';



"Alex" wrote in message

> Hi,
>
> Is there anyway to use the field alias name within the WHERE part of a
> SQL statement in MS SQL Server 2005? Below is an example:
>
> Select
> Table1.Name as FullName,
> Table1.Address as Address
> from
> Table1
> Where
> FullName like '%test%'
>
> What I'm doing is writing a Stored Procedure that accepts a number of
> parameters, then dynamically creates a SQL statement to pull in data
> for a look-up table. Given that the field names may change we're
> using field aliasing. I'm adding two parameters for filter field and
> filter value, but with me not wanting to hardcode field names within
> the application, I'd like to use the alias here as well... but it
> would be dynamically entered into the WHERE part of the SQL statement.
>
> I know some folks will be asking 'why do you want to do this', so I
> figured I'd throw out a quick summary of my purpose. But bottom line
> is i'd like to find someway to use the alias name within the WHERE
> clause.
>
> Thanks --
>
> Alex
 >> Stay informed about: Using the field alias name in WHERE clause 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
How can I put an Alias in Group By clause? - Hi My problem is that I would like to use an alias in a Group By clause but I think this is not allowed. I have to use the complete expression. Can someone confirm this for me? The problem is the expression itself is an agregation and hence I cannot us...

using a VALUE + a STRING as an ALIAS? - Hi, I'd like to translate this from Access SQL to 'true' SQL: SELECT "http:/mydomain/" & [Image] AS image_url I've tried several things but SQL Server doesn't accept it. How can I add this string to the value of that field? Thank you v...

Way to alias a database? - Hi, I'm using MS Sql 2005. When I run a query from within "MyDatabase" that accesses another database, "OtherDatabase", is there a way I can create an alias, "OtherDatabaseOtherName", to access "OtherDatabase" so...

Table alias with UPDATE ? - Is there a way to use a table alias with an UPDATE that has no FROM clause? I'm using SQL 2000. For example: UPDATE TableLongName SET myfield3 = 789 WHERE TableLongName.myfield1 = 123 AND TableLongName.myfield2 = 456 works, but isn't as easy to read...

using kind of alias in sqlplus - hi all. Can someone please help me on the following. Example query: Tabel1 contains a lot of items. TABLE1 ITEM VARCHAR2(30), FAM1 VARCHAR2(10), FAM2 VARCHAR2(10), FAM3 VARCHAR2(10), FAM4 VARCHAR2(10) Table contains a lot of records with....
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]