Connecting Tech Pros Worldwide Help | Site Map

sql server query problem

  #1  
Old July 21st, 2008, 09:35 AM
Dean g
Guest
 
Posts: n/a

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, 09:45 AM
Erland Sommarskog
Guest
 
Posts: n/a

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, 10:15 AM
Dean g
Guest
 
Posts: n/a

re: sql server query problem



Thanks alot, it works perfectly

Dean


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
MsAccess2003/SQL Server : Query should be updateable wquatan answers 1 October 30th, 2007 10:28 PM
"Timeout expired" for simple ADO.NET SQL Server query Nils Magnus Englund answers 3 November 19th, 2005 07:14 PM
PLEASE HELP... SQL SERVER Query in Access meyvn77@yahoo.com answers 5 November 13th, 2005 04:25 PM
Access and SQL Server - Query based on Query issue Flavio answers 2 November 13th, 2005 09:08 AM