January 17, 2009 at 9:32 am
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 😀
January 17, 2009 at 10:41 am
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
January 18, 2009 at 2:10 am
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 🙂
January 18, 2009 at 2:38 am
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
January 18, 2009 at 3:17 am
currently its numeric(9, 3).
January 18, 2009 at 11:15 am
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
January 24, 2009 at 10:49 am
thanks! it worked
July 7, 2010 at 5:57 am
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
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply