Author |
Topic |
cplusplus
Aged Yak Warrior
567 Posts |
Posted - 2010-08-18 : 16:34:16
|
I have the following sp: order by revisiondate desc is not working properly.ALTER PROCEDURE [dbo].[USP_GetDMGridRevisions](@DMid int,@glanguage nvarchar(20))ASDECLARE @ErrorCode intSET NOCOUNT ONSELECT @ErrorCode = @@ErrorIF @ErrorCode = 0BEGIN SELECT RV.RevID, dbo.UDF_DMRevDocExists(RV.RevID) RevDocID, rtrim(RV.RevNo) as RevNo, Case --This is revision status by language When @glanguage='English' Then rtrim(isnull(PV.EnglishText,'')) When @glanguage='Spanish' Then rtrim(isnull(PV.SpanishText,'')) When @glanguage='Catalan' Then rtrim(isnull(PV.CatalanText,'')) End RevStatusText, RV.RevStatus, CONVERT(varchar(10),RV.RevisionDate,101) as RevisionDate, CONVERT(varchar(10),RV.IssuedDate,101) as IssuedDate, CONVERT(varchar(10),RV.LoggedDate,101) as LoggedDate, PV.PValue from TAB_ccsNetDMRevision RV, TAB_ccsNetPicklistValue PV Where RV.dmid=@DMid and RV.RevStatus = PV.Pickid and PV.FieldLabelKey='ddlRevstatus' and RV.Deleted=0 ORDER BY RV.RevisionDate DESC SELECT @ErrorCode = @@ErrorENDSET NOCOUNT OFFRETURN @ErrorCodewhen i execute the above sp, i am getting 2 rowsshowing revision dates as follows: year 2010 row should appear top but it is showing as second row. am i doing something wrong.Revision date:12/10/200908/12/2010Thank you very much for the helpful info. |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2010-08-18 : 17:15:18
|
if it is do ORDER BY CONVERT(varchar(10),RV.RevisionDate,101) DESCbut really what tkizer is saying is on the moneyIf you don't have the passion to help people, you have no passion |
|
|
cplusplus
Aged Yak Warrior
567 Posts |
|
cplusplus
Aged Yak Warrior
567 Posts |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
Kristen
Test
22859 Posts |
Posted - 2010-08-18 : 17:38:39
|
long shot:Change to:CONVERT(varchar(10),RV.RevisionDate,101) as RevisionDatexxxxx,to avoid ambiguity on RevisionDate name (notwithstanding that you do appear to have unambiguously named it with the table alias as a prefix anyway ... I did say it was a longshot!) |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-08-18 : 17:40:31
|
"and using order by revisiondate desc, it is using the converted varchar date data"I disagree, although that was the premise of my comment above.Your ORDER by isORDER BY RV.RevisionDate DESCwhich should use the value from the RV table alias and not the value from the named column in the SELECT clause. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
Kristen
Test
22859 Posts |
Posted - 2010-08-18 : 17:42:24
|
Yeah, I definitely agree that there is room for misunderstanding! so just from a code maintenance perspctive I would avoid having the same name for a column in the table, and a column in the Select that was "manipulated".Plus, as you will know , I also agree with "format it in the Application / Front end"But notwithstanding any of that I reckon this puppy SHOULD work as written! |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-08-19 : 05:37:59
|
quote: Originally posted by Kristen "and using order by revisiondate desc, it is using the converted varchar date data"I disagree, although that was the premise of my comment above.Your ORDER by isORDER BY RV.RevisionDate DESCwhich should use the value from the RV table alias and not the value from the named column in the SELECT clause.
This is a behavioral change in versions starting from 2005. For versions prior to 2005, the ordering done by the actual alias nameMadhivananFailing to plan is Planning to fail |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-08-19 : 11:08:25
|
I assumed SQL 2005 as that is this conference but, yeah, if we are back in SQL 2000 then all bets are off as to which data object will be used when an ambiguous ORDER BY name is used That was pretty much our only, but (for us) relatively major, fix when upgrading from SQL 2000. We had lots of instances of ORDER BY columns used in the SELECT in a way that upset SQL2005+ |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-08-19 : 11:17:20
|
quote: Originally posted by Kristen I assumed SQL 2005 as that is this conference but, yeah, if we are back in SQL 2000 then all bets are off as to which data object will be used when an ambiguous ORDER BY name is used That was pretty much our only, but (for us) relatively major, fix when upgrading from SQL 2000. We had lots of instances of ORDER BY columns used in the SELECT in a way that upset SQL2005+
My guess is OP uses version 2000 and posted a question at 2005 forumMadhivananFailing to plan is Planning to fail |
|
|
|