Tuesday, March 27, 2012

conversion sql from oracle to SQL Server

Hi Guys, I have this statement that I am converting from Oracle to SQL. Help pls:-) PP_PRICEPOINT_ID is a decimal. What is the appropriate usage..

Oracle
----
update pricepoint set pp_type = decode(substr(pp_pricepoint_id,1,1),7,0,2),
pp_qtybreakindex =substr(pp_pricepoint_id,3,1) where pp_type is null and pp_qtybreakIndex is null;

Here is its SQL
------
UPDATE pricepoint
SET pp_type =
CASE SUBSTRING(pp_pricepoint_id, 1, 1)
WHEN 7 THEN 0
ELSE 2
END,
pp_qtybreakindex = SUBSTRING(pp_pricepoint_id, 3, 1)
WHERE pp_type is null
AND pp_qtybreakIndex is null
------
I am getting the error
The data type decimal is invalid for the substring function. Allowed types are: char/varchar, nchar/nvarchar, and binary/varbinary.UPDATE pricepoint
SET pp_type = CASE SUBSTRING(CONVERT(VARCHAR(15),pp_pricepoint_id), 1, 1)
WHEN 7 THEN 0 ELSE 2
END
-- What's with this?
-- , pp_qtybreakindex = SUBSTRING(pp_pricepoint_id, 3, 1)
WHERE pp_type is null
AND pp_qtybreakIndex is null|||Try converting your pp_pricepoint variable to varchar before using the substring function

Change :

CASE SUBSTRING(pp_pricepoint_id, 1, 1)

For :

CASE SUBSTRING(Cast(pp_pricepoint_id As VarChar) , 1, 1)

Pls Note i didnt check ur substring use for the sintax.

Hope it can help|||Thanks Brett, this worked..
UPDATE pricepoint
SET pp_type = CASE SUBSTRING(CONVERT(VARCHAR(15),pp_pricepoint_id), 1, 1)
WHEN 7 THEN 0 ELSE 2
END
, pp_qtybreakindex = SUBSTRING(CONVERT(VARCHAR(15),pp_pricepoint_id), 3, 1)
WHERE pp_type is null
AND pp_qtybreakIndex is null

I am updating 2 values here..sqlsql

No comments:

Post a Comment