I have a table in SQL that looks like the below:
table is like the below:
Partno b5 b6 b7 A 3*38 4*38 5*38 B 4*1100 8*1100 15*1100
Column b5,b6,b7 is currently nvarchar. Can I check how to convert this column to int with the value of the multiplication? Eg if column b5=3*38 I would like to return as 114 etc.
Thank you very much.
With the following assumption, you can use split the query and do multiplication.
a*b
in the columnsYou can use following query:
Select partno,
PARSENAME(REPLACE(b5,'*','.'),2) * PARSENAME(REPLACE(b5,'*','.'),1) as b5_res,
PARSENAME(REPLACE(b6,'*','.'),2) * PARSENAME(REPLACE(b6,'*','.'),1) as b6_res,
PARSENAME(REPLACE(b7,'*','.'),2) * PARSENAME(REPLACE(b7,'*','.'),1) as b7_res
From your_table t
Given the structure of the data, you could use simple string operations:
select t.*,
multiple * try_convert(int, left(b4, charindex('*', b4) - 1)),
multiple * try_convert(int, left(b5, charindex('*', b5) - 1)),
multiple * try_convert(int, left(b6, charindex('*', b6) - 1))
from t;
Here is a db<>fiddle.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.