Group:  Microsoft Excel ยป microsoft.public.excel.querydao
Thread: Excel SQL syntax question

Geek News

Excel SQL syntax question
"Don S" <NoSpamForMe[ at ]either.com> 5/16/2007 6:39:10 PM
Hello all,
I am querying data from a SQL server and would like the query to
reference data in the spreadsheet. I'm not sure how to compose the SQL
query. Here is what I have so far which, of course, does not work. I get
syntax errors.

Select Count(ReferralNumber)
From Conversion
Where YEAR(ReferralDate) = 'Sheet1'!$B$1


Re: Excel SQL syntax question
"Nick Hodge" <nick_hodgeTAKETHISOUT[ at ]zen.co.uk.ANDTHIS> 5/16/2007 9:17:18 PM
Don

Check here

http://www.nickhodge.co.uk/gui/datamenu/dataexamples/externaldataexamples.htm

(Under using parameters.......)

--
HTH
Nick Hodge
Microsoft MVP - Excel
Southampton, England
nick_hodgeTAKETHISOUT[ at ]zen.co.ukANDTHIS
web: www.nickhodge.co.uk
blog (non-tech): www.nickhodge.co.uk/blog/
"Don S" <NoSpamForMe[ at ]either.com> wrote in message
news:u9qd$l%23lHHA.960[ at ]TK2MSFTNGP03.phx.gbl...
[Quoted Text]
> Hello all,
> I am querying data from a SQL server and would like the query to
> reference data in the spreadsheet. I'm not sure how to compose the SQL
> query. Here is what I have so far which, of course, does not work. I get
> syntax errors.
>
> Select Count(ReferralNumber)
> From Conversion
> Where YEAR(ReferralDate) = 'Sheet1'!$B$1
>

Re: Excel SQL syntax question
"Don S" <NoSpamForMe[ at ]either.com> 5/21/2007 5:14:53 PM
Thanks a million Nick!
Do you know how to get MS Query editor to stop reformatting my queries. I
have a 3-parameter query. It gets reformatted so that I have to enter the
value 12+ times for EACH parameter. It changes this:

elect Count(*)
From Conversion
Where Year(ReferralDate) = ?
and Month(ReferralDate) = ?
and Approachedby = ?
and (ApproachSV = 2
or ApproachFV = 2
or ApproachSkin = 2
or ApproachUB = 2
or ApproachLB = 2
or ApproachMand = 2)
AND (DonorReg = 'Yes' or DonorReg = 'Yes, form on file')

. . . to this:

SELECT Count(*)
FROM Compstat.dbo.Conversion Conversion
WHERE (Conversion.ApproachSV=2) AND (Year(ReferralDate)=?) AND
(Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
(Conversion.DonorReg='Yes') OR (Year(ReferralDate)=?) AND
(Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
(Conversion.DonorReg='Yes') AND (Conversion.ApproachFV=2) OR
(Year(ReferralDate)=?) AND (Month(ReferralDate)=?) AND
(Conversion.ApproachedBy=?) AND (Conversion.DonorReg='Yes') AND
(Conversion.ApproachSkin=2) OR (Year(ReferralDate)=?) AND
(Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
(Conversion.DonorReg='Yes') AND (Conversion.ApproachUB=2) OR
(Year(ReferralDate)=?) AND (Month(ReferralDate)=?) AND
(Conversion.ApproachedBy=?) AND (Conversion.DonorReg='Yes') AND
(Conversion.ApproachLB=2) OR (Year(ReferralDate)=?) AND
(Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
(Conversion.DonorReg='Yes') AND (Conversion.ApproachMand=2) OR
(Conversion.ApproachSV=2) AND (Year(ReferralDate)=?) AND
(Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
(Conversion.DonorReg='Yes, form on file') OR (Year(ReferralDate)=?) AND
(Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
(Conversion.DonorReg='Yes, form on file') AND (Conversion.ApproachFV=2) OR
(Year(ReferralDate)=?) AND (Month(ReferralDate)=?) AND
(Conversion.ApproachedBy=?) AND (Conversion.DonorReg='Yes, form on file')
AND (Conversion.ApproachSkin=2) OR (Year(ReferralDate)=?) AND
(Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
(Conversion.DonorReg='Yes, form on file') AND (Conversion.ApproachUB=2) OR
(Year(ReferralDate)=?) AND (Month(ReferralDate)=?) AND
(Conversion.ApproachedBy=?) AND (Conversion.DonorReg='Yes, form on file')
AND (Conversion.ApproachLB=2) OR (Year(ReferralDate)=?) AND
(Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
(Conversion.DonorReg='Yes, form on file') AND (Conversion.ApproachMand=2)



"Nick Hodge" <nick_hodgeTAKETHISOUT[ at ]zen.co.uk.ANDTHIS> wrote in message
news:63EA0E03-0BA3-4A71-9ECF-BE6327899DEB[ at ]microsoft.com...
[Quoted Text]
> Don
>
> Check here
>
> http://www.nickhodge.co.uk/gui/datamenu/dataexamples/externaldataexamples.htm
>
> (Under using parameters.......)
>
> --
> HTH
> Nick Hodge
> Microsoft MVP - Excel
> Southampton, England
> nick_hodgeTAKETHISOUT[ at ]zen.co.ukANDTHIS
> web: www.nickhodge.co.uk
> blog (non-tech): www.nickhodge.co.uk/blog/
> "Don S" <NoSpamForMe[ at ]either.com> wrote in message
> news:u9qd$l%23lHHA.960[ at ]TK2MSFTNGP03.phx.gbl...
>> Hello all,
>> I am querying data from a SQL server and would like the query to
>> reference data in the spreadsheet. I'm not sure how to compose the SQL
>> query. Here is what I have so far which, of course, does not work. I
>> get syntax errors.
>>
>> Select Count(ReferralNumber)
>> From Conversion
>> Where YEAR(ReferralDate) = 'Sheet1'!$B$1
>>
>


Re: Excel SQL syntax question
"Nick Hodge" <nick_hodgeTAKETHISOUT[ at ]zen.co.uk.ANDTHIS> 5/21/2007 10:25:03 PM
Don

Wow, even if you hand-type the query rather then building it in the grid?

--
HTH
Nick Hodge
Microsoft MVP - Excel
Southampton, England
nick_hodgeTAKETHISOUT[ at ]zen.co.ukANDTHIS
web: www.nickhodge.co.uk
blog (non-tech): www.nickhodge.co.uk/blog/
"Don S" <NoSpamForMe[ at ]either.com> wrote in message
news:uWGORu8mHHA.3888[ at ]TK2MSFTNGP03.phx.gbl...
[Quoted Text]
> Thanks a million Nick!
> Do you know how to get MS Query editor to stop reformatting my queries. I
> have a 3-parameter query. It gets reformatted so that I have to enter the
> value 12+ times for EACH parameter. It changes this:
>
> elect Count(*)
> From Conversion
> Where Year(ReferralDate) = ?
> and Month(ReferralDate) = ?
> and Approachedby = ?
> and (ApproachSV = 2
> or ApproachFV = 2
> or ApproachSkin = 2
> or ApproachUB = 2
> or ApproachLB = 2
> or ApproachMand = 2)
> AND (DonorReg = 'Yes' or DonorReg = 'Yes, form on file')
>
> . . . to this:
>
> SELECT Count(*)
> FROM Compstat.dbo.Conversion Conversion
> WHERE (Conversion.ApproachSV=2) AND (Year(ReferralDate)=?) AND
> (Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
> (Conversion.DonorReg='Yes') OR (Year(ReferralDate)=?) AND
> (Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
> (Conversion.DonorReg='Yes') AND (Conversion.ApproachFV=2) OR
> (Year(ReferralDate)=?) AND (Month(ReferralDate)=?) AND
> (Conversion.ApproachedBy=?) AND (Conversion.DonorReg='Yes') AND
> (Conversion.ApproachSkin=2) OR (Year(ReferralDate)=?) AND
> (Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
> (Conversion.DonorReg='Yes') AND (Conversion.ApproachUB=2) OR
> (Year(ReferralDate)=?) AND (Month(ReferralDate)=?) AND
> (Conversion.ApproachedBy=?) AND (Conversion.DonorReg='Yes') AND
> (Conversion.ApproachLB=2) OR (Year(ReferralDate)=?) AND
> (Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
> (Conversion.DonorReg='Yes') AND (Conversion.ApproachMand=2) OR
> (Conversion.ApproachSV=2) AND (Year(ReferralDate)=?) AND
> (Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
> (Conversion.DonorReg='Yes, form on file') OR (Year(ReferralDate)=?) AND
> (Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
> (Conversion.DonorReg='Yes, form on file') AND (Conversion.ApproachFV=2) OR
> (Year(ReferralDate)=?) AND (Month(ReferralDate)=?) AND
> (Conversion.ApproachedBy=?) AND (Conversion.DonorReg='Yes, form on file')
> AND (Conversion.ApproachSkin=2) OR (Year(ReferralDate)=?) AND
> (Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
> (Conversion.DonorReg='Yes, form on file') AND (Conversion.ApproachUB=2) OR
> (Year(ReferralDate)=?) AND (Month(ReferralDate)=?) AND
> (Conversion.ApproachedBy=?) AND (Conversion.DonorReg='Yes, form on file')
> AND (Conversion.ApproachLB=2) OR (Year(ReferralDate)=?) AND
> (Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
> (Conversion.DonorReg='Yes, form on file') AND (Conversion.ApproachMand=2)
>
>
>
> "Nick Hodge" <nick_hodgeTAKETHISOUT[ at ]zen.co.uk.ANDTHIS> wrote in message
> news:63EA0E03-0BA3-4A71-9ECF-BE6327899DEB[ at ]microsoft.com...
>> Don
>>
>> Check here
>>
>> http://www.nickhodge.co.uk/gui/datamenu/dataexamples/externaldataexamples.htm
>>
>> (Under using parameters.......)
>>
>> --
>> HTH
>> Nick Hodge
>> Microsoft MVP - Excel
>> Southampton, England
>> nick_hodgeTAKETHISOUT[ at ]zen.co.ukANDTHIS
>> web: www.nickhodge.co.uk
>> blog (non-tech): www.nickhodge.co.uk/blog/
>> "Don S" <NoSpamForMe[ at ]either.com> wrote in message
>> news:u9qd$l%23lHHA.960[ at ]TK2MSFTNGP03.phx.gbl...
>>> Hello all,
>>> I am querying data from a SQL server and would like the query to
>>> reference data in the spreadsheet. I'm not sure how to compose the SQL
>>> query. Here is what I have so far which, of course, does not work. I
>>> get syntax errors.
>>>
>>> Select Count(ReferralNumber)
>>> From Conversion
>>> Where YEAR(ReferralDate) = 'Sheet1'!$B$1
>>>
>>
>
>

Re: Excel SQL syntax question
"Don S" <NoSpamForMe[ at ]either.com> 5/22/2007 2:55:56 PM
Yes. I seldom use the grid anymore. I'd REALLY love to turn this "feature"
off.

Don.

"Nick Hodge" <nick_hodgeTAKETHISOUT[ at ]zen.co.uk.ANDTHIS> wrote in message
news:6ACEE03F-D175-404E-A61C-ADDD73CA8FB7[ at ]microsoft.com...
[Quoted Text]
> Don
>
> Wow, even if you hand-type the query rather then building it in the grid?
>
> --
> HTH
> Nick Hodge
> Microsoft MVP - Excel
> Southampton, England
> nick_hodgeTAKETHISOUT[ at ]zen.co.ukANDTHIS
> web: www.nickhodge.co.uk
> blog (non-tech): www.nickhodge.co.uk/blog/
> "Don S" <NoSpamForMe[ at ]either.com> wrote in message
> news:uWGORu8mHHA.3888[ at ]TK2MSFTNGP03.phx.gbl...
>> Thanks a million Nick!
>> Do you know how to get MS Query editor to stop reformatting my queries.
>> I have a 3-parameter query. It gets reformatted so that I have to enter
>> the value 12+ times for EACH parameter. It changes this:
>>
>> elect Count(*)
>> From Conversion
>> Where Year(ReferralDate) = ?
>> and Month(ReferralDate) = ?
>> and Approachedby = ?
>> and (ApproachSV = 2
>> or ApproachFV = 2
>> or ApproachSkin = 2
>> or ApproachUB = 2
>> or ApproachLB = 2
>> or ApproachMand = 2)
>> AND (DonorReg = 'Yes' or DonorReg = 'Yes, form on file')
>>
>> . . . to this:
>>
>> SELECT Count(*)
>> FROM Compstat.dbo.Conversion Conversion
>> WHERE (Conversion.ApproachSV=2) AND (Year(ReferralDate)=?) AND
>> (Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
>> (Conversion.DonorReg='Yes') OR (Year(ReferralDate)=?) AND
>> (Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
>> (Conversion.DonorReg='Yes') AND (Conversion.ApproachFV=2) OR
>> (Year(ReferralDate)=?) AND (Month(ReferralDate)=?) AND
>> (Conversion.ApproachedBy=?) AND (Conversion.DonorReg='Yes') AND
>> (Conversion.ApproachSkin=2) OR (Year(ReferralDate)=?) AND
>> (Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
>> (Conversion.DonorReg='Yes') AND (Conversion.ApproachUB=2) OR
>> (Year(ReferralDate)=?) AND (Month(ReferralDate)=?) AND
>> (Conversion.ApproachedBy=?) AND (Conversion.DonorReg='Yes') AND
>> (Conversion.ApproachLB=2) OR (Year(ReferralDate)=?) AND
>> (Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
>> (Conversion.DonorReg='Yes') AND (Conversion.ApproachMand=2) OR
>> (Conversion.ApproachSV=2) AND (Year(ReferralDate)=?) AND
>> (Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
>> (Conversion.DonorReg='Yes, form on file') OR (Year(ReferralDate)=?) AND
>> (Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
>> (Conversion.DonorReg='Yes, form on file') AND (Conversion.ApproachFV=2)
>> OR (Year(ReferralDate)=?) AND (Month(ReferralDate)=?) AND
>> (Conversion.ApproachedBy=?) AND (Conversion.DonorReg='Yes, form on file')
>> AND (Conversion.ApproachSkin=2) OR (Year(ReferralDate)=?) AND
>> (Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
>> (Conversion.DonorReg='Yes, form on file') AND (Conversion.ApproachUB=2)
>> OR (Year(ReferralDate)=?) AND (Month(ReferralDate)=?) AND
>> (Conversion.ApproachedBy=?) AND (Conversion.DonorReg='Yes, form on file')
>> AND (Conversion.ApproachLB=2) OR (Year(ReferralDate)=?) AND
>> (Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
>> (Conversion.DonorReg='Yes, form on file') AND (Conversion.ApproachMand=2)
>>
>>
>>
>> "Nick Hodge" <nick_hodgeTAKETHISOUT[ at ]zen.co.uk.ANDTHIS> wrote in message
>> news:63EA0E03-0BA3-4A71-9ECF-BE6327899DEB[ at ]microsoft.com...
>>> Don
>>>
>>> Check here
>>>
>>> http://www.nickhodge.co.uk/gui/datamenu/dataexamples/externaldataexamples.htm
>>>
>>> (Under using parameters.......)
>>>
>>> --
>>> HTH
>>> Nick Hodge
>>> Microsoft MVP - Excel
>>> Southampton, England
>>> nick_hodgeTAKETHISOUT[ at ]zen.co.ukANDTHIS
>>> web: www.nickhodge.co.uk
>>> blog (non-tech): www.nickhodge.co.uk/blog/
>>> "Don S" <NoSpamForMe[ at ]either.com> wrote in message
>>> news:u9qd$l%23lHHA.960[ at ]TK2MSFTNGP03.phx.gbl...
>>>> Hello all,
>>>> I am querying data from a SQL server and would like the query to
>>>> reference data in the spreadsheet. I'm not sure how to compose the SQL
>>>> query. Here is what I have so far which, of course, does not work. I
>>>> get syntax errors.
>>>>
>>>> Select Count(ReferralNumber)
>>>> From Conversion
>>>> Where YEAR(ReferralDate) = 'Sheet1'!$B$1
>>>>
>>>
>>
>>
>


Re: Excel SQL syntax question
"Nick Hodge" <nick_hodgeTAKETHISOUT[ at ]zen.co.uk.ANDTHIS> 5/22/2007 7:37:39 PM
Don

Does re-arranging the ANDs and ORs to run in seq make any odds?

--
HTH
Nick Hodge
Microsoft MVP - Excel
Southampton, England
nick_hodgeTAKETHISOUT[ at ]zen.co.ukANDTHIS
web: www.nickhodge.co.uk
blog (non-tech): www.nickhodge.co.uk/blog/
"Don S" <NoSpamForMe[ at ]either.com> wrote in message
news:OHkJQFInHHA.1240[ at ]TK2MSFTNGP04.phx.gbl...
[Quoted Text]
> Yes. I seldom use the grid anymore. I'd REALLY love to turn this
> "feature" off.
>
> Don.
>
> "Nick Hodge" <nick_hodgeTAKETHISOUT[ at ]zen.co.uk.ANDTHIS> wrote in message
> news:6ACEE03F-D175-404E-A61C-ADDD73CA8FB7[ at ]microsoft.com...
>> Don
>>
>> Wow, even if you hand-type the query rather then building it in the grid?
>>
>> --
>> HTH
>> Nick Hodge
>> Microsoft MVP - Excel
>> Southampton, England
>> nick_hodgeTAKETHISOUT[ at ]zen.co.ukANDTHIS
>> web: www.nickhodge.co.uk
>> blog (non-tech): www.nickhodge.co.uk/blog/
>> "Don S" <NoSpamForMe[ at ]either.com> wrote in message
>> news:uWGORu8mHHA.3888[ at ]TK2MSFTNGP03.phx.gbl...
>>> Thanks a million Nick!
>>> Do you know how to get MS Query editor to stop reformatting my queries.
>>> I have a 3-parameter query. It gets reformatted so that I have to enter
>>> the value 12+ times for EACH parameter. It changes this:
>>>
>>> elect Count(*)
>>> From Conversion
>>> Where Year(ReferralDate) = ?
>>> and Month(ReferralDate) = ?
>>> and Approachedby = ?
>>> and (ApproachSV = 2
>>> or ApproachFV = 2
>>> or ApproachSkin = 2
>>> or ApproachUB = 2
>>> or ApproachLB = 2
>>> or ApproachMand = 2)
>>> AND (DonorReg = 'Yes' or DonorReg = 'Yes, form on file')
>>>
>>> . . . to this:
>>>
>>> SELECT Count(*)
>>> FROM Compstat.dbo.Conversion Conversion
>>> WHERE (Conversion.ApproachSV=2) AND (Year(ReferralDate)=?) AND
>>> (Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
>>> (Conversion.DonorReg='Yes') OR (Year(ReferralDate)=?) AND
>>> (Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
>>> (Conversion.DonorReg='Yes') AND (Conversion.ApproachFV=2) OR
>>> (Year(ReferralDate)=?) AND (Month(ReferralDate)=?) AND
>>> (Conversion.ApproachedBy=?) AND (Conversion.DonorReg='Yes') AND
>>> (Conversion.ApproachSkin=2) OR (Year(ReferralDate)=?) AND
>>> (Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
>>> (Conversion.DonorReg='Yes') AND (Conversion.ApproachUB=2) OR
>>> (Year(ReferralDate)=?) AND (Month(ReferralDate)=?) AND
>>> (Conversion.ApproachedBy=?) AND (Conversion.DonorReg='Yes') AND
>>> (Conversion.ApproachLB=2) OR (Year(ReferralDate)=?) AND
>>> (Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
>>> (Conversion.DonorReg='Yes') AND (Conversion.ApproachMand=2) OR
>>> (Conversion.ApproachSV=2) AND (Year(ReferralDate)=?) AND
>>> (Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
>>> (Conversion.DonorReg='Yes, form on file') OR (Year(ReferralDate)=?) AND
>>> (Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
>>> (Conversion.DonorReg='Yes, form on file') AND (Conversion.ApproachFV=2)
>>> OR (Year(ReferralDate)=?) AND (Month(ReferralDate)=?) AND
>>> (Conversion.ApproachedBy=?) AND (Conversion.DonorReg='Yes, form on
>>> file') AND (Conversion.ApproachSkin=2) OR (Year(ReferralDate)=?) AND
>>> (Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
>>> (Conversion.DonorReg='Yes, form on file') AND (Conversion.ApproachUB=2)
>>> OR (Year(ReferralDate)=?) AND (Month(ReferralDate)=?) AND
>>> (Conversion.ApproachedBy=?) AND (Conversion.DonorReg='Yes, form on
>>> file') AND (Conversion.ApproachLB=2) OR (Year(ReferralDate)=?) AND
>>> (Month(ReferralDate)=?) AND (Conversion.ApproachedBy=?) AND
>>> (Conversion.DonorReg='Yes, form on file') AND
>>> (Conversion.ApproachMand=2)
>>>
>>>
>>>
>>> "Nick Hodge" <nick_hodgeTAKETHISOUT[ at ]zen.co.uk.ANDTHIS> wrote in message
>>> news:63EA0E03-0BA3-4A71-9ECF-BE6327899DEB[ at ]microsoft.com...
>>>> Don
>>>>
>>>> Check here
>>>>
>>>> http://www.nickhodge.co.uk/gui/datamenu/dataexamples/externaldataexamples.htm
>>>>
>>>> (Under using parameters.......)
>>>>
>>>> --
>>>> HTH
>>>> Nick Hodge
>>>> Microsoft MVP - Excel
>>>> Southampton, England
>>>> nick_hodgeTAKETHISOUT[ at ]zen.co.ukANDTHIS
>>>> web: www.nickhodge.co.uk
>>>> blog (non-tech): www.nickhodge.co.uk/blog/
>>>> "Don S" <NoSpamForMe[ at ]either.com> wrote in message
>>>> news:u9qd$l%23lHHA.960[ at ]TK2MSFTNGP03.phx.gbl...
>>>>> Hello all,
>>>>> I am querying data from a SQL server and would like the query to
>>>>> reference data in the spreadsheet. I'm not sure how to compose the
>>>>> SQL query. Here is what I have so far which, of course, does not
>>>>> work. I get syntax errors.
>>>>>
>>>>> Select Count(ReferralNumber)
>>>>> From Conversion
>>>>> Where YEAR(ReferralDate) = 'Sheet1'!$B$1
>>>>>
>>>>
>>>
>>>
>>
>
>

Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net