April 28, 2005 at 11:25 am
Hi there,
I'm a newbie and need some expert advice- I'm pretty sure this is a simple problem...
I want to use the REPLACE function in the select statement, which also has a MAX Function in the select statement but it complains when i try to assign an alias to it
For example,
SELECT REPLACE(d.agtpostcode, CHAR(32), null) as pcode, c.agtname as name, MAX(d.agtrefno) as maxcode
FROM EntaLive.dbo.agent as c ,EntaLive.dbo.AgentAddress as d
WHERE c.agtrefno = d.agtrefno
GROUP BY pcode, name
this is part of a subquery, but having tried to run just this bit it gives the same complaint- that pcode and name are invalid column names! I think it's more to do with the naming, rather than the fact that I'm using a replace function..
Any advice would be very much appreciated!!
Thanks,
Alex
April 28, 2005 at 11:32 am
Your problem comes from the fact that you cannot use alias in the group by clause, this is allowed only in the order by clause.
SELECT REPLACE(d.agtpostcode, CHAR(32), '') as pcode, c.agtname as name, MAX(d.agtrefno) as maxcode
FROM EntaLive.dbo.agent as c ,EntaLive.dbo.AgentAddress as d
WHERE c.agtrefno = d.agtrefno
GROUP BY REPLACE(d.agtpostcode, CHAR(32), ''), c.agtname
I replaced null by'' because you'd get nulls instead of varchars in that column when a replace would occur.
April 28, 2005 at 11:41 am
Hiya, that's brilliant- thanks for the reply. My next question relates to the null part.
I'm using a linked server- so that patr of the statement actualyl looks like..
SELECT * FROM OPENQUERY(BARBSLIVE, 'SELECT REPLACE(d.agtpostcode, CHAR(32), null) as pcode, c.agtname as name, MAX(d.agtrefno) as maxcode
FROM EntaLive.dbo.agent as c ,EntaLive.dbo.AgentAddress as d
WHERE c.agtrefno = d.agtrefno
GROUP BY REPLACE(d.agtpostcode, CHAR(32), null) , c.agtname')
Now, if I us '' instead of null, it will escape out- ive tried using \ but that didnt seem to work either (hence why i tried using null).
What can i do to use '' to replace the spaces, without escapignfrom this query?
Thanks again for the reply- you've been most helpful!
Alex
April 28, 2005 at 11:49 am
You need to double each apostrophe in the query.
SELECT * FROM OPENQUERY(BARBSLIVE, 'SELECT REPLACE(d.agtpostcode, CHAR(32), '''') as pcode, c.agtname as name, MAX(d.agtrefno) as maxcode
FROM EntaLive.dbo.agent as c ,EntaLive.dbo.AgentAddress as d
WHERE c.agtrefno = d.agtrefno
GROUP BY REPLACE(d.agtpostcode, CHAR(32), '''') , c.agtname')
April 28, 2005 at 11:51 am
Fantastic!
I shall be setting up a shrine in your honour this evening 😉
Alex
April 28, 2005 at 11:52 am
Nah...
For the whole month... so it makes more sens .
April 28, 2005 at 8:18 pm
Remi in honor of your help on this post and others.. why just 2 days (the month is almost over). Perhaps this month and next month.
It could be more but why give you a big head.\
April 29, 2005 at 6:40 am
I just like to help... I don't need no shrine.
April 29, 2005 at 7:28 am
Be careful! Last time I said something similar, David Burrows called me a "sycophantic fool from Germany"
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
April 29, 2005 at 7:30 am
Well he knows you .
April 29, 2005 at 7:42 am
Yes, we have shared here more than one cyberspace beer and laughter.
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
April 29, 2005 at 7:45 am
God!!! I just realized how much of bunch of Geeks we are....
April 29, 2005 at 7:53 am
...yes, and it's Friday and here in my place just about the right time to go home.
Enjoy your weekend!
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply