 |
| 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
|
|
|
|

December 1st, 2003, 12:39 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

December 2nd, 2003, 01:48 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 77
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Maybe it will work if you replace 'THEN 0' with 'THEN 0.0'.
defiant
|
|

December 2nd, 2003, 02:23 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

December 2nd, 2003, 03:00 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

December 2nd, 2003, 03:29 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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. 
|
|

December 3rd, 2003, 02:00 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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
|
|

December 3rd, 2003, 02:12 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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
|
|

April 2nd, 2004, 11:46 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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. :)
|
|

April 3rd, 2004, 05:18 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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
|
|

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