Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6532429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:00:02+00:00 2026-05-25T10:00:02+00:00

I am trying to use CASE to group table contents that include characters and

  • 0

I am trying to use CASE to group table contents that include characters and NULL and Blank entries using the following:

SELECT  COUNT(*) AS Clients,
   CASE WHEN t.Diagnosedin = 'Unknown' OR t.Diagnosedin ='' THEN 'Unknown' 
   ELSE CASE WHEN CAST((CAST(DATENAME(Year,GETDATE()) AS INT)- t.Diagnosedin)AS varchar(5)) =1 THEN 1
   ELSE CASE WHEN CAST((CAST(DATENAME(Year,GETDATE()) AS INT)- t.Diagnosedin)AS varchar(5)) <=2 THEN 2
   ELSE CASE WHEN CAST((CAST(DATENAME(Year,GETDATE()) AS INT)- t.Diagnosedin)AS varchar(5)) <=5 THEN 5
   ELSE CASE WHEN CAST((CAST(DATENAME(Year,GETDATE()) AS INT)- t.Diagnosedin)AS varchar(5)) <=10 THEN 10 
   ELSE CASE WHEN CAST((CAST(DATENAME(Year,GETDATE()) AS INT)- t.Diagnosedin)AS varchar(5))>=10 THEN '+10 '
   END
   END
   END
   END
   END
   END AS Years

I get the following error: Conversion failed when converting the varchar value 'Unknown' to data type int., I am using MS SQL 2008.
Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:00:03+00:00Added an answer on May 25, 2026 at 10:00 am

    What’s happening is that you’re mixing literals of different datatypes in the column Years. (i.e. int 5 and varchar Unknown will be in the same resultset). You need to code those literal values as the same datatype.

    Try this:

    • cast your hardcoded integers to strings/varchar. I’ve used the STR() function, and used the LRTIM() to ensure that the leading spaces aren’t kept.
    • use the same CASE
    SELECT COUNT(*) AS Clients,
       CASE WHEN t.Diagnosedin = 'Unknown' OR t.Diagnosedin ='' THEN 'Unknown' 
        WHEN CAST((CAST(DATENAME(Year,GETDATE()) AS INT)- t.Diagnosedin)AS varchar(5)) =1 THEN LTRIM(STR(1))
        WHEN CAST((CAST(DATENAME(Year,GETDATE()) AS INT)- t.Diagnosedin)AS varchar(5)) <=2 THEN LTRIM(STR(2))
        WHEN CAST((CAST(DATENAME(Year,GETDATE()) AS INT)- t.Diagnosedin)AS varchar(5)) <=5 THEN LTRIM(STR(5))
        WHEN CAST((CAST(DATENAME(Year,GETDATE()) AS INT)- t.Diagnosedin)AS varchar(5)) <=10 THEN LTRIM(STR(10)) 
        WHEN CAST((CAST(DATENAME(Year,GETDATE()) AS INT)- t.Diagnosedin)AS varchar(5))>=10 THEN '+10 '
    END AS Years
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We've the following SQL that we use to calculate total costs. SELECT DailyProduction.CustomerId as
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
Related (for the no-association-object use case): SQLAlchemy Many-to-Many Relationship on a Single Table Building
Trying to get a simple COUNT from a table that takes a couple of
I'm trying to use group by with rollup clause within sql server 2005 but
I'm trying to use ibatis with HSSQL to create a table in an in
I am trying a query like this in MYSQL select Sum(case when WindowsXP =
Using VS2010's find dialog, I'm using the following regex: (?'outer'^div\#.+) input$ . I'm trying
I am trying to create a dynamic table using php but needed to get
I'm trying to use a method group in a lambda expression, like this: public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.