Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
lols
Posting Yak Master
174 Posts |
Posted - 2009-04-30 : 09:10:43
|
| I am running the query:SELECT DISTINCT CONVERT(VARCHAR(12), MyDate, 106) as MyDate FROM MyTable WHERE MyDate >= DATEADD(month,-1,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)) and MyDate < dateadd(month,datediff(month,-1,getdate()),0) ORDER BY MyDate DescI get the result as:31 Mar 200930 Mar 200930 Apr 200929 Mar 200929 Apr 200928 Mar 200928 Apr 200927 Mar 200927 Apr 200926 Mar 200926 Apr 200925 Mar 200925 Apr 200924 Mar 200924 Apr 200923 Mar 200923 Apr 200922 Mar 200922 Apr 200921 Mar 200921 Apr 200920 Mar 200920 Apr 200919 Mar 200919 Apr 200918 Mar 200918 Apr 200917 Mar 200917 Apr 200916 Mar 200916 Apr 200915 Mar 200915 Apr 200914 Mar 200914 Apr 200913 Mar 200913 Apr 200912 Mar 200912 Apr 200911 Mar 200911 Apr 200910 Mar 200910 Apr 200909 Mar 200909 Apr 200908 Mar 200908 Apr 200907 Mar 200907 Apr 200906 Mar 200906 Apr 200905 Mar 200905 Apr 200904 Mar 200904 Apr 200903 Mar 200903 Apr 200902 Mar 200902 Apr 200901 Mar 200901 Apr 2009However I want the result where first dates of April and then dates of March should be listed rather than being mixed. How to do it? |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-04-30 : 09:14:21
|
| SELECT DISTINCT CONVERT(VARCHAR(12), MyDate, 106) as MyDate FROM MyTable WHERE MyDate >= DATEADD(month,-1,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)) and MyDate < dateadd(month,datediff(month,-1,getdate()),0) ORDER BY cast(MyDate as datetime) DescMadhivananFailing to plan is Planning to fail |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-04-30 : 09:17:06
|
| datatype for MyDate is varchar ? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-04-30 : 09:24:58
|
quote: Originally posted by sakets_2000 datatype for MyDate is varchar ?
No. Op convert it in the SELECT statement as varcharMadhivananFailing to plan is Planning to fail |
 |
|
|
lols
Posting Yak Master
174 Posts |
Posted - 2009-04-30 : 09:30:01
|
| Thanks. When I run your query, I get errorORDER BY items must appear in the select list if SELECT DISTINCT is specified. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-04-30 : 09:38:31
|
| SELECT * FROM(SELECT DISTINCT CONVERT(VARCHAR(12), MyDate, 106) as MyDate FROM MyTable WHERE MyDate >= DATEADD(month,-1,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)) and MyDate < dateadd(month,datediff(month,-1,getdate()),0) ) AS tORDER BY CAST(MyDate as datetime) DescMadhivananFailing to plan is Planning to fail |
 |
|
|
lols
Posting Yak Master
174 Posts |
Posted - 2009-04-30 : 11:16:07
|
| Thanks so much. I get the desired output but now it is taking very long time to get records. Can I generate the desired output without using MyTable. I want to list down last 2 months dates in a similar format30 Apr 200929 Apr 2009 ...and so on |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-05-04 : 01:44:54
|
| select dateadd(month,datediff(month,0,getdate())-2,0)+number from master..spt_valueswhere type='p' and dateadd(month,datediff(month,0,getdate())-2,0)+number<dateadd(month,datediff(month,0,getdate()),0)order by 1MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|