Solved

Inconsistent datatypes: expected CHAR got NUMBER

Posted on 2014-09-24
2
314 Views
Last Modified: 2014-09-24
Why am I getting the following error when I run this query? "inconsistent datatypes: expected CHAR got NUMBER"
It seems to be triggered by the YEAR_OF_STUDY case statement.

SELECT rep_start_year,
         university_code,
         YEAR_OF_STUDY,
         COUNT(*) AS YEAR_OF_STUDY_count

    FROM (SELECT rep_start_year,
                 CASE
                     WHEN YEAR_OF_STUDY in (5,6,7,8,9)  THEN 'Other'
                     ELSE
                         YEAR_OF_STUDY
                 END
                     YEAR_OF_STUDY,

                 (CASE
                      WHEN uni.university_code = 'DOMC' THEN 'CARL'
                      WHEN uni.university_code = 'NOSM-LAKE' THEN 'NOSM'
                      WHEN uni.university_code = 'NOSM-LAUR' THEN 'NOSM'
                      WHEN uni.university_code = 'LAUR-ALGM' THEN 'ALGM'
                      WHEN uni.university_code = 'LAUR-HRST' THEN 'HRST'
                      WHEN uni.university_code = 'DOMC' THEN 'CARL'
                      ELSE uni.university_code
                  END)
                     AS university_code
            FROM edcs_unistat.ustat_enrolment_data enrol
            
                     
                 INNER JOIN edcs_unistat.uni_institution_type uniinst
                     ON enrol.uni_institution_type_id = uniinst.uni_institution_type_id
                     
                 INNER JOIN edcs_core.university uni ON uniinst.university_id = uni.university_id
                 
                 INNER JOIN edcs_unistat.reg_status_type regstatus
                     ON enrol.reg_status_type_id = regstatus.reg_status_type_id
                     
                 INNER JOIN edcs_unistat.fee_cat_type feetyp
                     ON enrol.fee_cat_type_id = feetyp.fee_cat_type_id
                     
           WHERE enrol.rep_start_year IN ('2012', '2013')
           AND enrol.period IN ('FAL')
           AND regstatus.reg_status_type_code IN ('12','13','14','15','16','18')
           AND feetyp.fee_cat_type_code IN ('3', '4')
           )
             
GROUP BY rep_start_year, university_code, YEAR_OF_STUDY
ORDER BY rep_start_year, university_code, YEAR_OF_STUDY

Open in new window

0
Comment
Question by:mariita
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 250 total points
ID: 40342005
The return values for a CASE statement have to be of the same data type.

If year_of_study is a number then:
...
CASE
                     WHEN YEAR_OF_STUDY in (5,6,7,8,9)  THEN 'Other'
                     ELSE
                         to_char(YEAR_OF_STUDY)
                 END
                     YEAR_OF_STUDY,
...

Same for the other CASE statement.
0
 
LVL 24

Assisted Solution

by:Phillip Burton
Phillip Burton earned 250 total points
ID: 40342009
It's lines 8 and 10.

If the YEAR_OF_STUDY, which appears to be a number, is 5, 6, 7, 8 or 9, you return a string. If it isn't, you return a number. That's the mismatch.

Maybe line 10 should be

CAST(YEAR_OF_STUDY as varchar(4))

Open in new window

0

Featured Post

Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to take different types of Oracle backups using RMAN.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
Suggested Courses

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question