| Author |
Topic |
|
Gary Costigan
Yak Posting Veteran
95 Posts |
Posted - 2003-09-26 : 13:27:33
|
| I recently received some "outstanding" help on a large query I've been working on, and was hopeing to get some additional help on a couple of problems that have me stumped.1: In the query I have the following code.WHEN losttime.CTwentyOne.C23DiscussionDate >= CTwentyOne.AbsenceStopDate THEN 'Completed'ELSE CONVERT(varchar(20),DATEDIFF(dd,CTwentyOne.AbsenceStopDate, getdate())) END AS DaysOverDue,Is there a way of formatting the return values so that when I do the following.ORDER BY losttime.CTwentyOne.DaysOverDue DESC,The results are in descending order? Right now the numbers are in a descending order but out of sink. I'm thinking I just need to format the value as (000), but can't seem to figure out how to do it.2. My other question is that I have the following code.DATENAME (MONTH,losttime.CTwentyOne.AbsenceStartDate)AS AbsenceMonth,Which returns the name of the month. Is there a way of having it return the name for just the current month Ie: September. And the last three months as well. Ie: August, July, June. Anything between January - May of 2003 I would like it to just return the year Ie: 2003, and if the absence happened in 2002 to just show Ie: 2002.Thanks for all your help now and in the past!Regards.GC |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2003-09-26 : 13:33:02
|
| What do you mean by out of sync? Could you show us an example?Tara |
 |
|
|
Gary Costigan
Yak Posting Veteran
95 Posts |
Posted - 2003-09-26 : 13:43:22
|
| Tara,This is what I'm getting.April 90June 90June 90June 90June 90September 9September 9September 9September 9September 9July 80July 80July 80September 8September 8September 8September 8September 8August 26August 26January 259January 259January 257January 254January 252January 251January 251January 250September 25September 25September 25September 25September 20September 20September 20September 2September 2September 2September 2September 2I think my problem would be solved if I could format the column to three digits. Ie: 002 or 020. so that the descend order would be 285020002etc...Thanks again.Gary |
 |
|
|
drymchaser
Aged Yak Warrior
552 Posts |
Posted - 2003-09-26 : 14:07:39
|
| For No. 2DATENAME(MONTH, getdate()), DATENAME(MONTH, (DATEADD(month, -1, GETDATE())), DATENAME(MONTH, (DATEADD(month, -2, GETDATE())), DATENAME(MONTH, (DATEADD(month, -3, GETDATE())) |
 |
|
|
drymchaser
Aged Yak Warrior
552 Posts |
Posted - 2003-09-26 : 14:14:02
|
| To add leading zeroes:right(('000' + your field), 3) --- with 3 being digits needed in outcomeresults would resemble:003033333 |
 |
|
|
Gary Costigan
Yak Posting Veteran
95 Posts |
Posted - 2003-09-26 : 14:31:39
|
| Drymchaser,I'm still stumped. Where does the "Right" fall into this line of code?WHEN losttime.CTwentyOne.C23DiscussionDate >= CTwentyOne.AbsenceStopDate THEN 'Completed'ELSE CONVERT(varchar(20),DATEDIFF(dd,CTwentyOne.AbsenceStopDate, getdate())) END AS DaysOverDue,I've got all my SQL books open and the only examples show it at the start of the SELECT. Help!!GC |
 |
|
|
mohdowais
Sheikh of Yak Knowledge
1456 Posts |
Posted - 2003-09-26 : 14:46:43
|
(Question no 1) The results you are getting are correct, since you are sorting them as varchar(20), i.e. by the total ascii values of the characters in the string. You could cast DaysOverDue to an INT and get it sorted right but I think you also have a value of 'Completed' for some of the rows that would throw a type mismatch error. So you need to check the datatype and sort accordingly:SELECT blah, blah, blahFROM table1ORDER BY CASE WHEN IsNumeric(DaysOverDue) THEN DaysOverDueELSE 0 END DESC --Use 0 to see all rows with "completed" firstOwais Make it idiot proof and someone will make a better idiot |
 |
|
|
drymchaser
Aged Yak Warrior
552 Posts |
Posted - 2003-09-26 : 15:11:26
|
| Gary,Tell me what "20" comes from in this resultSeptember 20 |
 |
|
|
Gary Costigan
Yak Posting Veteran
95 Posts |
Posted - 2003-09-26 : 15:39:58
|
| Drymchaser,My example wasn't the greatest as I was showing the results from two different fields.The 'DaysOverDue' field is just a calculation between two dates. I get the results I need, I would just like to add some leading zeros so that I can get the query to display in a descending order.Gary |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2003-09-26 : 17:12:43
|
| Gary --Use the DateDiff() function, it can return the # of days between two dates. And it returns a numeric value, so you can sort by it.- Jeff |
 |
|
|
Gary Costigan
Yak Posting Veteran
95 Posts |
Posted - 2003-09-26 : 17:49:14
|
| Jeff,Here's the code I am using which includes DATEDIFF, I'm getting the results I need, I just need them formatted to 3 digits so that they sort in descending order properely.WHEN losttime.CTwentyOne.C23DiscussionDate >= CTwentyOne.AbsenceStopDateTHEN 'Completed'ELSE CONVERT(varchar(20),DATEDIFF(dd,CTwentyOne.AbsenceStopDate, getdate())) END AS DaysOverDue,Right now the sort comes back as.93929190989 to 80 then879 to 70 then7....etc.When we get down to the 3's then it's365 to 300 then39 to 30 then 3. This is the same for the 2's and 1's.I just need it descend in order.365to001GC |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2003-09-26 : 18:07:18
|
| WHY are you converting the result to a VARCHAR ?! the result of DateDiff() is a perfectly good number that you can return + sort on. Does this make sense?You need two columns: 1 called "days over due" and 1 called "Status" or something like that. You should not try to mix words and numbers in the same column. Or, just return a days over due #, and in your PRESENTATION LAYER (HTML, Crystal, Access, Excel) take care of formatting and display the words "completed" instead.PLease think about this and try it out and tell me if it makes sense.- Jeff |
 |
|
|
mohdowais
Sheikh of Yak Knowledge
1456 Posts |
Posted - 2003-09-27 : 06:07:00
|
Gary, did you miss my post in the middle of this page? Owais Make it idiot proof and someone will make a better idiot |
 |
|
|
Gary Costigan
Yak Posting Veteran
95 Posts |
Posted - 2003-09-29 : 14:04:31
|
| Jeff,I read what you had to say on Friday before I left the office and thought about what you had said over the weekend.From the query I have now with the help of the SQL Team Forum members, I am getting the results I need. I just wanted to "tweak" it a bit more. The conversion in the CASE Statement to VARCHAR does give us the result we need of either text "Completed" or a number.I've played around with the query and found that I can get the result I need by descending the AbsenceStopDate in "Ascending Order".In another part of the query I was using the "CONVERT" function to change the date fields to CHAR and the Style to 101 to give me a small date of MM/DD/YYYY in all of the date fields. I've looked through the SQL books I have and can't find anything that shows how to get the Date fields to just a small date "MM/DD/YY"If I convert them then I can't get them to sort properly as well, because of the CHAR format which is the same problem as the DaysOverDue field.Any suggestions on the dates to change them to just a small format?Thanks again for your time on this.GC |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-09-29 : 14:07:49
|
| How about:SELECT CONVERT(varchar(8),GetDate(),1)Brett8-)SELECT @@POST FROM Brain ORDER BY NewId()That's correct! It's an AlphaNumeric! |
 |
|
|
Gary Costigan
Yak Posting Veteran
95 Posts |
Posted - 2003-09-29 : 14:27:19
|
| Brett,I tried but still get the same problem. Converting the date to a VARCHAR will not let me sort the field in a Descending Order by date.If I leave it to just the default date format I can sort things fine.In the date field I just want to get rid of the Time area. It's all 00:00:00 which I don't need.Thanks.GC |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2003-09-29 : 15:22:13
|
| Gary --Formatting should be done at the PRESENTATION LAYER not at the database layer. Just return the RAW DATA and format the data as nicely as you like in your PRESENTATION.how are you displaying the results of this query to users? Web page? Report? Excel?There is a REASON why SQL doesn't have lots of formatting functions -- because it shouldn't. If you leave the data unformatted while in SQL, you will find you have no trouble sorting and grouping and doing all the things you need to do.- Jeff |
 |
|
|
Gary Costigan
Yak Posting Veteran
95 Posts |
Posted - 2003-09-29 : 15:36:03
|
| Jeff,The little light just turned on. I hear what you are saying. I was too busy formatting my query in the SQL Query Analyzer and not thinking of the "BIG" picture.I am hoping to have the query result display on a web page and also in an Excel report.Thanks for the friendly nudge in the right direction. If I run into any problems I'll get back with you.Thanks again.GC |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-09-29 : 15:42:41
|
Gary,What does SELECTing a column and ORDERing it have to do with each other?I agree with Jeff, but isn't this simple?USE NorthwindGO SELECT CONVERT(varchar(8),OrderDate,1) FROM OrdersORDER BY OrderDateGO No?Brett8-)SELECT @@POST FROM Brain ORDER BY NewId()That's correct! It's an AlphaNumeric! |
 |
|
|
Gary Costigan
Yak Posting Veteran
95 Posts |
Posted - 2003-09-29 : 16:19:18
|
| Brett,I keep copies of my queries as I build in steps so that if something blows I have a backup that works. I've tried both queries, one where I just leave the date fields alone and SQL defaults as 10/05/2002 0:00, and the other where I have CONVERTED the date field as CONVERT (Char (10), losttime.CTwentyOne.AbsenceStartDate, 101)as AbsenceStartDate.I sort both by: ORDER BY losttime.CTwentyOne.AbsenceStopDate DESCThe one query using the convert returns this.Absence Code Absence Start Date Absencen Stop Date Absence Month C23 Due Days Over Due Days C23 Completed UA 12/31/2002 12/31/2002 December YES 272 NeededSKU 12/18/2002 12/31/2002 December YES 272 NeededSK 12/26/2002 12/31/2002 December YES 272 NeededSK 12/23/2002 12/30/2002 December YES 273 NeededSKU 12/29/2002 12/30/2002 December YES 273 NeededSKQ 12/28/2002 12/28/2002 December YES 275 NeededSK 12/22/2002 12/23/2002 December YES 280 NeededRL 12/23/2002 12/23/2002 December YES 280 NeededSK 12/20/2002 12/20/2002 December YES 283 NeededRL 12/18/2002 12/18/2002 December YES 285 NeededSK 12/18/2002 12/18/2002 December YES 285 NeededSK 12/10/2002 12/17/2002 December YES 286 NeededSK 12/14/2002 12/16/2002 December YES 287 NeededSK 12/13/2002 12/16/2002 December YES 287 NeededSK 12/13/2002 12/14/2002 December YES 289 NeededSK 12/10/2002 12/12/2002 December YES 291 NeededSK 12/07/2002 12/09/2002 December YES 294 NeededRL 12/09/2002 12/09/2002 December YES 294 NeededSK 12/02/2002 12/08/2002 December YES 295 NeededSK 12/06/2002 12/06/2002 December YES 297 NeededSK 12/03/2002 12/06/2002 December YES 297 NeededRL 12/06/2002 12/06/2002 December YES 297 NeededSK 12/05/2002 12/05/2002 December YES 298 NeededRL 12/03/2002 12/03/2002 December YES 300 NeededUA 12/02/2002 12/02/2002 December YES 301 NeededSK 12/02/2002 12/02/2002 December YES 301 NeededSK 11/30/2002 12/01/2002 November YES 302 NeededSK 11/27/2002 11/27/2002 November YES 306 NeededSKU 11/26/2002 11/27/2002 November YES 306 NeededRL 11/26/2002 11/26/2002 November YES 307 NeededUA 11/23/2002 11/23/2002 November YES 310 NeededUA 11/20/2002 11/20/2002 November YES 313 NeededSK 11/18/2002 11/19/2002 November YES 314 NeededNC 11/16/2002 11/16/2002 November YES 317 NeededSK 11/12/2002 11/12/2002 November YES 321 NeededRL 11/06/2002 11/06/2002 November YES 327 NeededSKU 11/01/2002 11/01/2002 November YES 332 NeededSKL 10/01/2002 10/30/2002 October YES 334 NeededSK 10/28/2002 10/29/2002 October YES 335 NeededSK 10/21/2002 10/29/2002 October YES 335 NeededSK 10/20/2002 10/21/2002 October YES 343 NeededNC 10/19/2002 10/19/2002 October YES 345 NeededRL 10/08/2002 10/08/2002 October YES 356 NeededSK 10/05/2002 10/05/2002 October YES 359 NeededSK 09/23/2003 09/25/2003 September YES 4 NeededSK 09/25/2003 09/25/2003 September YES 4 NeededSK 09/23/2003 09/25/2003 September YES 4 NeededSK 09/25/2003 09/25/2003 September YES 4 NeededSK 09/24/2003 09/25/2003 September YES 4 NeededSKU 09/18/2003 09/25/2003 September YES 4 NeededSKU 09/16/2003 09/25/2003 September YES 4 NeededSK 09/25/2003 09/25/2003 September YES 4 NeededSK 09/24/2003 09/25/2003 September YES 4 NeededSK 09/25/2003 09/25/2003 September YES 4 NeededSK 09/24/2003 09/25/2003 September YES 4 NeededSK 09/23/2003 09/25/2003 September YES 4 NeededSKU 09/25/2003 09/25/2003 September YES 4 NeededSKL 09/08/2003 09/25/2003 September YES 4 NeededSK 09/24/2003 09/25/2003 September YES 4 NeededSK 09/25/2003 09/25/2003 September YES 4 NeededSK 09/25/2003 09/25/2003 September YES 4 NeededSK 09/23/2003 09/25/2003 September YES 4 NeededSK 09/25/2003 09/25/2003 September YES 4 NeededSK 09/25/2003 09/25/2003 September YES 4 NeededSKL 09/02/2003 09/25/2003 September YES 4 NeededSKL 09/07/2003 09/25/2003 September YES 4 NeededSKU 09/19/2003 09/25/2003 September YES 4 NeededSK 09/24/2003 09/25/2003 September YES 4 NeededSK 09/25/2003 09/25/2003 September YES 4 NeededSKL 06/30/2003 09/25/2003 June YES 4 NeededSKL 07/30/2003 09/25/2003 July YES 4 NeededSK 09/18/2003 09/25/2003 September YES 4 NeededSK 09/25/2003 09/25/2003 September YES 4 NeededUA 09/25/2003 09/25/2003 September YES 4 NeededSKP 08/21/2003 09/25/2003 August YES 4 NeededSKL 07/15/2003 09/25/2003 July YES 4 NeededSK 09/17/2003 09/25/2003 September YES 4 NeededSKL 08/14/2003 09/25/2003 August YES 4 NeededSKL 07/23/2003 09/25/2003 July YES 4 NeededSK 09/14/2003 09/25/2003 September YES 4 NeededSKL 09/07/2003 09/25/2003 September YES 4 NeededSKL 04/17/2003 09/25/2003 April YES 4 NeededSKL 07/23/2003 09/25/2003 July YES 4 NeededSKL 05/05/2003 09/25/2003 May YES 4 NeededSK 09/17/2003 09/25/2003 September YES 4 NeededSKL 07/31/2003 09/25/2003 July YES 4 NeededSKL 09/11/2003 09/25/2003 September YES 4 NeededSKU 09/25/2003 09/25/2003 September YES 4 NeededSK 09/23/2003 09/25/2003 September YES 4 NeededThe one without the convert returns this:Absence Code Absence Start Date Absencen Stop Date Absence Month C23 Due Days Over Due Days C23 Completed SK 10/05/2002 0:00 10/05/2002 0:00 2002 YES 359 NeededRL 10/08/2002 0:00 10/08/2002 0:00 2002 YES 356 NeededNC 10/19/2002 0:00 10/19/2002 0:00 2002 YES 345 NeededSK 10/20/2002 0:00 10/21/2002 0:00 2002 YES 343 NeededSK 10/28/2002 0:00 10/29/2002 0:00 2002 YES 335 NeededSK 10/21/2002 0:00 10/29/2002 0:00 2002 YES 335 NeededSKL 10/01/2002 0:00 10/30/2002 0:00 2002 YES 334 NeededSKU 11/01/2002 0:00 11/01/2002 0:00 2002 YES 332 NeededRL 11/06/2002 0:00 11/06/2002 0:00 2002 YES 327 NeededSK 11/12/2002 0:00 11/12/2002 0:00 2002 YES 321 NeededNC 11/16/2002 0:00 11/16/2002 0:00 2002 YES 317 NeededSK 11/18/2002 0:00 11/19/2002 0:00 2002 YES 314 NeededUA 11/20/2002 0:00 11/20/2002 0:00 2002 YES 313 NeededUA 11/23/2002 0:00 11/23/2002 0:00 2002 YES 310 NeededRL 11/26/2002 0:00 11/26/2002 0:00 2002 YES 307 NeededSK 11/27/2002 0:00 11/27/2002 0:00 2002 YES 306 NeededSKU 11/26/2002 0:00 11/27/2002 0:00 2002 YES 306 NeededSK 11/30/2002 0:00 12/01/2002 0:00 2002 YES 302 NeededUA 12/02/2002 0:00 12/02/2002 0:00 2002 YES 301 NeededSK 12/02/2002 0:00 12/02/2002 0:00 2002 YES 301 NeededRL 12/03/2002 0:00 12/03/2002 0:00 2002 YES 300 NeededSK 12/05/2002 0:00 12/05/2002 0:00 2002 YES 298 NeededSK 12/06/2002 0:00 12/06/2002 0:00 2002 YES 297 NeededSK 12/03/2002 0:00 12/06/2002 0:00 2002 YES 297 NeededRL 12/06/2002 0:00 12/06/2002 0:00 2002 YES 297 NeededSK 12/02/2002 0:00 12/08/2002 0:00 2002 YES 295 NeededSK 12/07/2002 0:00 12/09/2002 0:00 2002 YES 294 NeededRL 12/09/2002 0:00 12/09/2002 0:00 2002 YES 294 NeededSK 12/10/2002 0:00 12/12/2002 0:00 2002 YES 291 NeededSK 12/13/2002 0:00 12/14/2002 0:00 2002 YES 289 NeededSK 12/14/2002 0:00 12/16/2002 0:00 2002 YES 287 NeededSK 12/13/2002 0:00 12/16/2002 0:00 2002 YES 287 NeededSK 12/10/2002 0:00 12/17/2002 0:00 2002 YES 286 NeededRL 12/18/2002 0:00 12/18/2002 0:00 2002 YES 285 NeededSK 12/18/2002 0:00 12/18/2002 0:00 2002 YES 285 NeededSK 12/20/2002 0:00 12/20/2002 0:00 2002 YES 283 NeededSK 12/22/2002 0:00 12/23/2002 0:00 2002 YES 280 NeededRL 12/23/2002 0:00 12/23/2002 0:00 2002 YES 280 NeededSKQ 12/28/2002 0:00 12/28/2002 0:00 2002 YES 275 NeededSK 12/23/2002 0:00 12/30/2002 0:00 2002 YES 273 NeededSKU 12/29/2002 0:00 12/30/2002 0:00 2002 YES 273 NeededUA 12/31/2002 0:00 12/31/2002 0:00 2002 YES 272 NeededSKU 12/18/2002 0:00 12/31/2002 0:00 2002 YES 272 NeededSK 12/26/2002 0:00 12/31/2002 0:00 2002 YES 272 NeededSK 12/31/2002 0:00 01/01/2003 0:00 2002 YES 271 NeededSK 12/31/2002 0:00 01/02/2003 0:00 2002 YES 270 NeededSK 01/03/2003 0:00 01/03/2003 0:00 2003 YES 269 NeededNC 01/03/2003 0:00 01/03/2003 0:00 2003 YES 269 NeededUA 01/03/2003 0:00 01/03/2003 0:00 2003 YES 269 NeededRL 01/04/2003 0:00 01/04/2003 0:00 2003 YES 268 NeededSK 01/07/2003 0:00 01/07/2003 0:00 2003 YES 265 NeededSK 01/05/2003 0:00 01/09/2003 0:00 2003 YES 263 NeededSK 01/10/2003 0:00 01/10/2003 0:00 2003 YES 262 NeededSK 01/06/2003 0:00 01/10/2003 0:00 2003 YES 262 NeededSK 01/12/2003 0:00 01/12/2003 0:00 2003 YES 260 NeededSK 01/06/2003 0:00 01/15/2003 0:00 2003 YES 257 NeededUA 01/14/2003 0:00 01/17/2003 0:00 2003 YES 255 NeededSK 01/16/2003 0:00 01/18/2003 0:00 2003 YES 254 NeededSK 01/16/2003 0:00 01/18/2003 0:00 2003 YES 254 NeededRL 01/19/2003 0:00 01/19/2003 0:00 2003 YES 253 NeededSK 01/20/2003 0:00 01/20/2003 0:00 2003 YES 252 NeededSK 01/11/2003 0:00 01/21/2003 0:00 2003 YES 251 NeededSK 01/16/2003 0:00 01/24/2003 0:00 2003 YES 248 NeededSK 01/20/2003 0:00 01/24/2003 0:00 2003 YES 248 NeededNC 01/24/2003 0:00 01/24/2003 0:00 2003 YES 248 NeededRL 01/25/2003 0:00 01/25/2003 0:00 2003 YES 247 NeededRL 01/26/2003 0:00 01/26/2003 0:00 2003 YES 246 NeededSK 01/26/2003 0:00 01/27/2003 0:00 2003 YES 245 NeededSK 01/28/2003 0:00 01/28/2003 0:00 2003 YES 244 NeededSK 01/29/2003 0:00 01/29/2003 0:00 2003 YES 243 NeededSK 01/21/2003 0:00 01/29/2003 0:00 2003 YES 243 NeededSKL 01/16/2003 0:00 01/30/2003 0:00 2003 YES 242 NeededSK 01/22/2003 0:00 01/31/2003 0:00 2003 YES 241 NeededSK 01/28/2003 0:00 01/31/2003 0:00 2003 YES 241 NeededSK 01/28/2003 0:00 01/31/2003 0:00 2003 YES 241 NeededSK 01/31/2003 0:00 02/01/2003 0:00 2003 YES 240 NeededSKP 01/15/2003 0:00 02/02/2003 0:00 2003 YES 239 NeededSKL 01/05/2003 0:00 02/03/2003 0:00 2003 YES 238 NeededSK 01/31/2003 0:00 02/03/2003 0:00 2003 YES 238 NeededSK 02/03/2003 0:00 02/04/2003 0:00 2003 YES 237 NeededSK 02/02/2003 0:00 02/05/2003 0:00 2003 YES 236 NeededRL 02/08/2003 0:00 02/08/2003 0:00 2003 YES 233 NeededSK 02/07/2003 0:00 02/12/2003 0:00 2003 YES 229 NeededAs you can see the query without using the convert has the 'Absence Stop Date' descending correctly which also reflects in the 'Days Over Due' field as well.I think Jeff is correct is saying that I need to format the date field in the presentation layer, ie: Web or Excel. Thanks for all your help.GC |
 |
|
|
|