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 662237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:21:22+00:00 2026-05-13T23:21:22+00:00

I am getting an error when I run this query because the data type

  • 0

I am getting an error when I run this query because the data type money can’t be implicitly converted to a varchar. However, I am using an if statemnt to make sure the data type is not money before I try the conversion. Clearly, the conversion is being executed anyways. Anyone know why?

table: BBH_NEW col: rebate2
datatype: money

if 'money'= 'money'
begin
   if (select max([rebate2]) from [BBH_NEW]) = 0 
   and (select min([rebate2]) from [BBH_NEW]) = 0
     print ' rebate2 '
     print ' 1 '
end   

if 'money'!= 'money'
begin
   IF NOT EXISTS (SELECT top 1 * FROM [BBH_NEW] WHERE [rebate2] IS NOT NULL and
   len([rebate2]) > 0 ) 
   BEGIN 
      print ' rebate2 ' 
   end
end

Error:
Msg 257, Level 16, State 3, Line 11
Implicit conversion from data type money to varchar is not allowed. Use the CONVERT function to run this query.


yes this code was generated. If it helps, this is the code which was used to produce it:

select @temp = 
    data_type FROM information_schema.columns
WHERE table_schema = 'dbo'
AND table_name = @tblname 
AND column_name = @col

SELECT @hold = 
    'if '''+@temp+'''= ''money'' 
begin
   if (select max(['+@col+']) from ['+@tblname+']) = 0 
       and (select min(['+@col+']) from ['+@tblname+']) = 0
   print '' '+@col+' money''
end 

    if '''+@temp+'''!= ''money'' 
begin
   IF NOT EXISTS (SELECT max([' + @col + ']) FROM ['+ @tblname + '] 
       WHERE len( [' + @col + ']) > 0 ) 
       BEGIN 
          print '' ' + @col + ' '' 
       end 
end'
  • 1 1 Answer
  • 1 View
  • 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-13T23:21:23+00:00Added an answer on May 13, 2026 at 11:21 pm

    As I understand it the column BBH_NEW.rebate2 is of type money when you get the error. In T-SQL you can’t have a query that doesn’t compile, and that is what you are encountering. Even though the query in the always-false if block won’t run, it doesn’t compile because the data types don’t match.

    First, a quick solution for you – use CONVERT or CAST to explicitly change the data type.

    if 'money'!= 'money'
    begin
       IF NOT EXISTS (SELECT top 1 * FROM [BBH_NEW] WHERE [rebate2] IS NOT NULL and
       len(CONVERT(VARCHAR(8000), [rebate2])) > 0 ) 
       BEGIN 
          print ' rebate2 ' 
       end
    end
    

    But, there has to be a better way to do whatever you are doing… When does that SQL get generated? If it is at runtime, can you just not generate the part that won’t run? Maybe something like this?

    SELECT @hold = CASE WHEN @temp = 'money' THEN 
        'if (select max(['+@col+']) from ['+@tblname+']) = 0 
           and (select min(['+@col+']) from ['+@tblname+']) = 0
       print '' '+@col+' money'''
    ELSE
        'IF NOT EXISTS (SELECT max([' + @col + ']) FROM ['+ @tblname + '] 
           WHERE len( [' + @col + ']) > 0 ) 
           BEGIN 
              print '' ' + @col + ' '' 
           end'
    END
    

    or maybe change the generation to this…

    SELECT @temp = DATA_TYPE 
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = 'dbo'
    AND TABLE_NAME = @tblname 
    AND COLUMN_NAME = @col
    
    IF(@temp = 'money')
        SELECT @hold = 'IF(EXISTS(
        SELECT 1 
        FROM ['+@tblname+'] 
        HAVING MAX(['+@col+']) = 0 
        AND MIN(['+@col+']) = 0))
    BEGIN
        PRINT '' '+@col+' ''
    END';
    ELSE
        SELECT @hold = 'IF(NOT EXISTS(
        SELECT * 
        FROM ['+@tblname+'] 
        WHERE ['+@col+'] IS NOT NULL
        AND LEN(['+@col+']) > 0))
    BEGIN
        PRINT '' '+@col+' ''
    END';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 380k
  • Answers 380k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Late binding is a very different concept to interpretation. Strictly… May 14, 2026 at 9:58 pm
  • Editorial Team
    Editorial Team added an answer By omimtting -des3 you won't be prompted for a passphrase… May 14, 2026 at 9:58 pm
  • Editorial Team
    Editorial Team added an answer You should make a [ThreadStatic] static Current property, then write… May 14, 2026 at 9:58 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.