Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 1st, 2003, 12:39 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default Error using CONVERT

I have the following T-SQL in SQL2000 that is giving me an error in the CONVERT line:
Code:
SELECT [T_SetupSheetHistoryParameterDetail].WOID,
      [T_SetupSheetHistoryParameterDetail].WOIDSub,
      [T_SetupSheetHistoryParameterDetail].QuoteID,
      [T_SetupSheetHistoryParameterDetail].ECN,
      [T_SetupSheetHistoryParameterDetail].Revision,


 CONVERT(float,
    CASE
        WHEN [ParameterValue] LIKE '%[A-Z]%'
            THEN 0
        WHEN  [ParameterValue] IS NULL
            THEN 0
        ELSE
              [ParameterValue]
    END
  ) AS LineSpedInInchesPerMin  

FROM [T_SetupSheetHistoryParameterDetail]
WHERE [T_SetupSheetHistoryParameterDetail].ParameterID=9;
When I run this, the first [ParameterValue] is '34.2' and it generates the following error:
Code:
 
Syntax error converting the nvarchar value '34.2' to a column of data type int.
The [ParameterValue] can contain text as well as numbers or be blank, so it is a nvarchar 50.
__________________
Mitch
 
Old December 2nd, 2003, 01:48 PM
Authorized User
 
Join Date: Jun 2003
Posts: 77
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Maybe it will work if you replace 'THEN 0' with 'THEN 0.0'.

defiant
 
Old December 2nd, 2003, 02:23 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default

We are getting closser I think. :)

When I change THEN 0 to THEN 0.0 I now get:

Arithmetic overflow error converting numeric to data type numeric.

Which does not make any sense to me.
 
Old December 2nd, 2003, 03:00 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Even if I take out the Convert I still get an error!


 (
    CASE
        WHEN [ParameterValue] LIKE '%[A-Z]%'
            THEN 0.0
        WHEN [ParameterValue] = NULL
            THEN 0.0
         ELSE
              [ParameterValue]
    END
  ) AS LineSpedInInchesPerMin

I get:

Arithmetic overflow error converting numeric to data type numeric.
 
Old December 2nd, 2003, 03:29 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I got it to work! :D
But I had to change the 0.0 to '0' like this:

(
    CASE
        WHEN [ParameterValue] LIKE '%[A-Z]%'
            THEN '0'
        WHEN [ParameterValue] = NULL
            THEN '0'
         ELSE
              [ParameterValue]
    END
) AS LineSpedInInchesPerMin
I don't understand why the zeros have to be text.
 
Old December 3rd, 2003, 02:00 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

It is simple, as you have mentioned earlier in your first post. Since you have removed the Convert() from your sql statement now, you can't return int in first 2 cases and nvarchar in the 3rd case. So '0' is a meaningful one.

Quote:
quote:The [ParameterValue] can contain text as well as numbers or be blank, so it is a nvarchar 50.
Cheers,

-Vijay G
 
Old December 3rd, 2003, 02:12 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Now you can try this too.

convert(float,
    CASE
        WHEN [ParameterValue] LIKE '%[A-Z]%'
            THEN '0'
        WHEN [ParameterValue] = NULL
            THEN '0'
         ELSE
              [ParameterValue]
    END
) AS LineSpedInInchesPerMin

Cheers,


-Vijay G
 
Old April 2nd, 2004, 11:46 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry about getting back to this one so late, I got side tracked and forgot about it until I actually came back to do it again and thought this problem seemed familiar.

To reply to Vijay:

Putting CONVERT at the begining still caused an error, I thought it would as well.

This is what finally worked:

Code:
SELECT [T_SetupSheetHistoryParameterDetail].WOID,
      [T_SetupSheetHistoryParameterDetail].WOIDSub,
      [T_SetupSheetHistoryParameterDetail].QuoteID,
      [T_SetupSheetHistoryParameterDetail].ECN,
      [T_SetupSheetHistoryParameterDetail].Revision,
(
    CASE
        WHEN [ParameterValue] LIKE '%[A-Z]%'
            THEN '0'
        WHEN  [ParameterValue] = NULL
            THEN '0'
         ELSE
              [ParameterValue] 
    END 
) AS LineSpedInInchesPerMin  


FROM [T_SetupSheetHistoryParameterDetail]
WHERE [T_SetupSheetHistoryParameterDetail].ParameterID=9
As with before, it works. :)
 
Old April 3rd, 2004, 05:18 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Yes, the reason behind it is, there weren't similarity in the datatypes used in your THEN statements.

In your first and foremost post,

 CONVERT(float,
    CASE
        WHEN [ParameterValue] LIKE '%[A-Z]%'
            THEN 0 -- Int Type
        WHEN [ParameterValue] IS NULL
            THEN 0 -- Int Type
        ELSE
              [ParameterValue] -- NVARCHAR Type
    END
  ) AS LineSpedInInchesPerMin

We need to maintain similarity in return datatypes for all the 3 cases. Then it doesn't matter using convert() or not using Convert() there, as LineSpedInInchesPerMin cannot be an Int type in one row and a nvarchar type in another.

Cheers,

-Vijay G
 
Old April 5th, 2004, 02:36 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Vijay





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help to convert c++ to c# happy_face C# 2 November 9th, 2007 10:56 PM
Learning VB 2k5. Cant convert to string error. Michael Eaton Visual Basic 2005 Basics 3 November 16th, 2005 04:28 PM
convert ngang SQL Server 2000 7 November 21st, 2003 06:03 PM
Error Data When Convert Excel to Access rpitoyo Pro VB Databases 0 August 5th, 2003 12:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.