How to use a CASE statement to replace values within a SELECT statement, under certain conditions, using SQL Server 2008 ?

I have a table named tblRange and create a query to geneate the following output:

SELECT MthTitl As [Date Range], DFrC as [From], DToC As [To], DollTitl As [Dollar Range],
CountOfCustomerNumber As [Num Of Accts], SumOfAcctValue As [Acct Value], MthF As [# Months],
DFr As [From Date ] FROM dbo.tblRange
ORDER By DFrC DESC, DollTitl DESC

result:

Date Range                   From              To                    Dollar Range    Num Of Accts    Acct Value  
--------------------------       -------------        --------------       -------------------  --------------------    ---------------
LESS THAN 6 MOS       2014-10-01    2015-03-01    WITH $100K                948           678648.46
LESS THAN 6 MOS       2014-10-01    2015-03-01    WITH $  50K                662           456456.53      
6 MOS TO 1 YEAR        2014-04-01    2014-09-01    WITH $ 100K               416           234245.54
6 MOS TO 1 YEAR        2014-04-01    2014-09-01    WITH $   25K               473           145457.76
1 TO 2 YEARS               2013-04-01    2014-03-01    WITH $ 100K               598           854754.65          
1 TO 2 YEARS               2013-04-01    2014-03-01    WITH $   50K              1084          746953.64
1 TO 2 YEARS               2013-04-01    2014-03-01    WITH $   10K                861          357735.22
2 TO 3 YEARS               2012-04-01    2013-03-01    WITH $100K                598           432864.35
2 TO 3 YEARS               2012-04-01    2013-03-01    WITH $  50K                342           865676.54
3 PLUS YEARS              1931-12-01    2012-03-01    WITH $  50K                113           253743.64
3 PLUS YEARS              1931-12-01    2012-03-01    WITH $  10K                475           463435.56
3 PLUS YEARS              1931-12-01    2012-03-01    WITH $    1K                735           356765.64
OTHER                          NULL              NULL               WITH $  50K                375           575654.46
OTHER                          NULL              NULL               WITH $  10K                153           453644.68

I use the following Select statement with CASE to create subtotals.

How would you modify this SELECT with CASE statement to replace the value '1931-12-01' with the value 'Earliest' in the DFrC field?

I tried  "CASE WHEN DFrC='1931-12-01' THEN 'EARLIEST' ELSE DFrc END AS [From]," unsuccessfully

SELECT
     CASE WHEN is_total_only = 0 THEN [Date Range] ELSE '' END AS [Date Range],
     CASE WHEN is_total_only = 0 THEN CONVERT(varchar(10), [From], 120) ELSE '' END AS [From],
     CASE WHEN is_total_only = 0 THEN CONVERT(varchar(10), [To], 120) ELSE '' END AS [To],
     CASE WHEN is_total_only = 0 THEN [Dollar Range] ELSE '' END AS [Dollar Range],
     [Num Of Accts],
     [Acct Value]
 FROM (
     SELECT
        0 AS is_total_only,
         MthTitl As [Date Range],
         --CASE WHEN DFrC='1931-12-01' THEN 'EARLIEST' ELSE DFrc END AS [From],
         DFrC as [From],
         DToC As [To], DollTitl As [Dollar Range],
        CountOfCustomerNumber As [Num Of Accts], SumOfAcctValue As [Acct Value]
     FROM tblRange tr
     UNION ALL
     SELECT
         1 AS is_total_only,
         MthTitl AS [Date Range],
         MIN(DFrC) AS [From],
         --CASE WHEN MIN DFrC='1931-12-01' THEN 'EARLIEST' ELSE DFrc END AS [From],
         CAST('19000101' AS datetime) AS [To], '' AS [Dollar Range],
         SUM(CountOfCustomerNumber) AS CountOfCustomerNumber, SUM(SumOfAcctValue) AS SumOfAcctValue
     FROM tblRange
     GROUP BY MthTitl
 ) AS derived
 ORDER By derived.[From] DESC, is_total_only, derived.[Dollar Range]
zimmer9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Data DudeCommented:
You'll have to cast the column as varchar to support a value of 'EARLIEST'.
The below worked in my SSMS 2012
CREATE TABLE #tmp (DFrC date) 

INSERT INTO #tmp (DfrC) 
VALUES ('2010-01-01'), ('1989-12-01'), ('1931-12-01'), ('2015-03-05')

SELECT 
  CASE WHEN DfrC = '1931-12-01' THEN 'UNKNOWN' ELSE CAST(DfrC as varchar(10)) END
FROM #tmp

Open in new window

Scott PletcherSenior DBACommented:
SELECT
      CASE WHEN is_total_only = 0 THEN [Date Range] ELSE '' END AS [Date Range],
      CASE WHEN is_total_only = 0 THEN
          CASE WHEN [From] = '19311201' THEN 'EARLIEST' ELSE CONVERT(varchar(10), [From], 120) END
          ELSE '' END AS [From],

      CASE WHEN is_total_only = 0 THEN CONVERT(varchar(10), [To], 120) ELSE '' END AS [To],
...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.