Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Error Converting Data type varchar to numeric


Error Converting Data type varchar to numeric

Author
Message
codewatcher11
codewatcher11
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 36
hi guys, im a newbie in this forum and im glad being a part of this programmer's den.
anyways, im new in to sql,i know a few,but there are some things i dont know,
and i know a great help is waiting me.

i just wanna know guys how can i improve my sql statement,

SELECT     Orders.CustomerCode, Orders.Description, Orders.IsDelivered, Orders.OrderDate, Orders.OrderNo, Orders.RequisitionNo, Orders.Username, 
OrderedItems.DeliveredQty, CASE WHEN DeliveredQty = ItemQty THEN 'Delivered' WHEN DeliveredQty < ItemQty THEN (DeliveredQty)
+ ' items delivered.' ELSE (DeliveredQty) + ' items delivered' + (DeliveredQty - ItemQty) + ' consignment' END AS Remarks
FROM Orders INNER JOIN
OrderedItems ON Orders.OrderNo = OrderedItems.OrderNo
WHERE (Orders.Username = @Username)



i need to tell the query builder that the Fields ending with 'Qty' are numeric,
thanks in advance BigGrin
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47263 Visits: 44392
Actually, SQL knows they're numeric. The error comes from trying to concatenate a numeric value into a string. It's trying to convert 'delivered' into numeric, which isn't going to work.

SELECT Orders.CustomerCode, 
Orders.Description,
Orders.IsDelivered,
Orders.OrderDate,
Orders.OrderNo,
Orders.RequisitionNo,
Orders.Username,
OrderedItems.DeliveredQty,
CASE
WHEN DeliveredQty = ItemQty THEN 'Delivered'
WHEN DeliveredQty < ItemQty THEN CAST(DeliveredQty AS VARCHAR(10)) + ' items delivered.'
ELSE CAST(DeliveredQty AS VARCHAR(10)) + ' items delivered' + CAST(DeliveredQty - ItemQty AS VARCHAR(10)) + ' consignment'
END AS Remarks
FROM Orders
INNER JOIN OrderedItems ON Orders.OrderNo = OrderedItems.OrderNo
WHERE (Orders.Username = @Username)




Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


codewatcher11
codewatcher11
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 36
GilaMonster (1/17/2009)
Actually, SQL knows they're numeric. The error comes from trying to concatenate a numeric value into a string. It's trying to convert 'delivered' into numeric, which isn't going to work.

SELECT Orders.CustomerCode, 
Orders.Description,
Orders.IsDelivered,
Orders.OrderDate,
Orders.OrderNo,
Orders.RequisitionNo,
Orders.Username,
OrderedItems.DeliveredQty,
CASE
WHEN DeliveredQty = ItemQty THEN 'Delivered'
WHEN DeliveredQty < ItemQty THEN CAST(DeliveredQty AS VARCHAR(10)) + ' items delivered.'
ELSE CAST(DeliveredQty AS VARCHAR(10)) + ' items delivered' + CAST(DeliveredQty - ItemQty AS VARCHAR(10)) + ' consignment'
END AS Remarks
FROM Orders
INNER JOIN OrderedItems ON Orders.OrderNo = OrderedItems.OrderNo
WHERE (Orders.Username = @Username)



hi gilamonster!
thanks for the help, it worked!

one more question,
how can i makE the fields ending with 'Qty' into two decimal places (0.00) ?
thanks Smile
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47263 Visits: 44392
What type are they at the moment?


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


codewatcher11
codewatcher11
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 36
currently its numeric(9, 3).
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47263 Visits: 44392
Cast them to numeric(9,2) before casting to varchar. It will cause the value to be rounded and then displayed with 2 decimals.

eg: replace CAST(DeliveredQty AS VARCHAR(10)) with CAST(CAST(DeliveredQty AS NUMERIC(9,2)) AS VARCHAR(10))


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


codewatcher11
codewatcher11
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 36
thanks! it worked
shailesh-360831
shailesh-360831
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 105
I also incurred same error msg "Error converting data type nvarchar to numeric", solved converting non-numeric column to numeric

Problem: TableA.EmpID = TableB.EmployeeID

Here TableA.EmpID is varchar datatype and in other end TableB.EmployeeID was in Decimal datatype. I converted TableB.EmployeeID to varchar datatype.

Solution: TableA.EmpID = Cast (TableB.EmployeeID as Varchar)




Shailesh
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search