Connecting Tech Pros Worldwide Help | Site Map

sql server query problem

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 21st, 2008, 08:35 AM
Dean g
Guest
 
Posts: n/a
Default sql server query problem


Hi,
I'm trying to use the result from the select within the same query.
See code

SELECT accountnum, char(ASCII(SUBSTRING(accountnum, 1, 1))) as
firstChar from questions where firstchar='m'

Is it possible to use the result from char(ASCII(SUBSTRING(accountnum,
1, 1))) within the same query without any nested loops, like i'm trying
to use it?

Thanks,
Dean

*** Sent via Developersdex http://www.developersdex.com ***

  #2  
Old July 21st, 2008, 08:45 AM
Erland Sommarskog
Guest
 
Posts: n/a
Default Re: sql server query problem

Dean g (big_deanus@hotmail.com) writes:
Quote:
I'm trying to use the result from the select within the same query.
See code
>
SELECT accountnum, char(ASCII(SUBSTRING(accountnum, 1, 1))) as
firstChar from questions where firstchar='m'
>
Is it possible to use the result from char(ASCII(SUBSTRING(accountnum,
1, 1))) within the same query without any nested loops, like i'm trying
to use it?
Use a derived table:

SELECT accountnum, firstchar
FROM (SELECT accountnum, substring(accountnum, 1, 1) AS firstchar
FROM questions) AS s
WHERE firstchar = 'm'

In SQL 2005 you can also use a common-table expression (CTE):

WITH extract AS (
SELECT accountnum, substring(accountnum, 1, 1) AS firstchar
FROM questions
)
SELECT accountnum, firstchar
FROM extract

This may look a little clunky, but keep in mind that SQL Server will
compute it more directly.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
  #3  
Old July 21st, 2008, 09:15 AM
Dean g
Guest
 
Posts: n/a
Default Re: sql server query problem


Thanks alot, it works perfectly

Dean


*** Sent via Developersdex http://www.developersdex.com ***
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.