| Author |
Topic |
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-02-16 : 08:16:35
|
| In my table date is set up as Char.How do I sort it by date as it is being sorted by string when I do order by filedate. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2012-02-16 : 08:28:03
|
ORDER BY CAST(Col1 AS DATE) N 56°04'39.26"E 12°55'05.63" |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2012-02-16 : 08:28:41
|
| You need to convert the char to date. (EDIT) Peso is too fast for me today!http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspxHow to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-02-16 : 08:37:20
|
| Thanks I had to add datetime to it because I got this error with just date:I'm getting Type date is not a defined system typeOrder by cast(filedate as datetime) |
 |
|
|
rajan.nithin
Starting Member
42 Posts |
Posted - 2012-02-16 : 08:39:34
|
In what format is the date stored ?is it like 2012/02/03?quote: Originally posted by JJ297 In my table date is set up as Char.How do I sort it by date as it is being sorted by string when I do order by filedate.
|
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-02-16 : 08:43:28
|
| It's stored as 11/02/2009 |
 |
|
|
rajan.nithin
Starting Member
42 Posts |
Posted - 2012-02-16 : 08:53:24
|
You could also try:ORDER BY CONVERT(date,filedate)quote: Originally posted by JJ297 It's stored as 11/02/2009
|
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-02-16 : 09:04:41
|
| Thanks that worked but I failed to tell you I have another column that needs to have the oldest to the earliest date sorted on as well.How do I add another field so when I click on that field next it sorts. The field is called ddsreceiptORDER BY CONVERT(datetime,filedate) How would I add it here? |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-02-16 : 09:06:00
|
Here's the entire query:Select p.DOC, p.Reg, n.RegionAcronym, FO, CLMSSN,Age = Datediff(day,max(filedate), getdate()),FileDate = max(FileDate),o.mailingaddressstate as DDS,o.ddsofficecode as DDSCode,Min(ddsrcpt) AS DDSReceipt, DDSAge = Datediff(day,min(ddsrcpt), getdate()), Title = case when min(p.Title) <> max(p.Title) then 'Concurr' else min(p.Title) endFrom (select *, row_number() over (Partition by ddsrcpt order by ddsrcpt) as rownum from pendingdds) pjoin natdocfile n on n.reg = p.regjoin offices.dbo.doorsinfo oon o.officecode = p.fowhere p.doc in ('S0B')Group By p.filedate, FO, p.Reg,n.regionacronym, p.DOC, CLMSSN, o.ddsofficecode,o.mailingaddressstateORDER BY CONVERT(datetime,filedate) |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2012-02-16 : 09:16:00
|
Step 1, scream at the DBO until the fields are changed to DATETIME fields. Step 2, order by those fields. http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspxHow to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-02-16 : 09:18:36
|
| Okay thanks. Does my group by have anything to do with the order by? Do I need to place the group by in a particular order? |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-02-16 : 11:04:19
|
I'm sorry I don't understand what you mean by datetime2When I come into the page the first time I want to sort by age first with the oldest age at the top.FO CLMS AGE FileDate Code DDSRecedipt278 125698 570 07/26/2010 S23 09/08/2011195 569874 524 09/10/2010 S23 01/06/2012278 659874 444 07/26/2010 S23 11/07/2011 when I click on FileDate I want it to sort like this:FO CLMS AGE FileDate Code DDSReceipt278 125698 570 07/26/2010 S23 09/08/2011278 659874 444 07/26/2010 S23 11/07/2011195 569874 524 09/10/2010 S23 01/06/2012 when I click on DDSReceipt I want it to sort like this:FO CLMS AGE FileDate Code DDSReceipt278 125698 570 07/26/2010 S23 09/08/2011278 659874 444 07/26/2010 S23 11/07/2011195 569874 524 09/10/2010 S23 01/06/2012 How would I write the group by and order by for this query:Select p.DOC, p.Reg, n.RegionAcronym, FO, CLMSSN,Age = Datediff(day,max(filedate), getdate()),FileDate = max(FileDate),o.mailingaddressstate as DDS,o.ddsofficecode as DDSCode,Min(ddsrcpt) AS DDSReceipt, DDSAge = Datediff(day,min(ddsrcpt), getdate()), Title = case when min(p.Title) <> max(p.Title) then 'Concurr' else min(p.Title) endFrom (select *, row_number() over (Partition by ddsrcpt order by ddsrcpt) as rownum from pendingdds) pjoin natdocfile n on n.reg = p.regjoin offices.dbo.doorsinfo oon o.officecode = p.fowhere p.doc in ('S0B')Group By p.filedate, FO, p.Reg,n.regionacronym, p.DOC, CLMSSN, o.ddsofficecode,o.mailingaddressstateORDER BY CONVERT(datetime,filedate) |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-02-16 : 11:50:49
|
when I don't add group by and run the stored procedure I get this error message:Column 'p.doc' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.So I added it:Select p.DOC, p.Reg, n.RegionAcronym, FO, CLMSSN,Age = Datediff(day,max(filedate), getdate()),FileDate = max(FileDate),o.mailingaddressstate as DDS,o.ddsofficecode as DDSCode,Min(ddsrcpt) AS DDSReceipt, DDSAge = Datediff(day,min(ddsrcpt), getdate()), Title = case when min(p.Title) <> max(p.Title) then 'Concurr' else min(p.Title) endFrom (select *, row_number() over (Partition by ddsrcpt order by ddsrcpt) as rownum from pendingdds) pjoin natdocfile n on n.reg = p.regjoin offices.dbo.doorsinfo oon o.officecode = p.fowhere p.doc in ('S0B')Group By p.filedate, p.ddsrcpt, p.doc , p.reg, n.regionacronym, p.fo, p.clmssn, MailingAddressState, o.ddsofficecodeorder by doc, age descORDER BY age desc, filedate, ddsreceipt What I mean by click on is this stored procedure is used in a Gridview with sortable columns. So when I sort on those columns I want them to be in desc order. Do I do that in .net? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2012-02-17 : 03:40:23
|
ORDER BY CONVERT(datetime, filedate, 101) N 56°04'39.26"E 12°55'05.63" |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2012-02-17 : 07:57:37
|
| Click on = front end. Your datagrid should have a column header event you can handle, and most likely a sort property.http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspxHow to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2012-02-17 : 13:12:36
|
| Thanks for the replies I changed the field to Datetime which was much easier to handle! |
 |
|
|
|