Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 495
  • Last Modified:

SQL Query multiplication

In SQL, I am updating a Table : PlacedOrderDetails by mutiplying two columns and entering the mutiplied values to the Qty Column.

[Ship Qty] which is nvarchar(255)  
Mutiply with
[Size] which is nvarchar(255)

Resulted value goes to [Qty] which is float.


update PlacedOrderDetails          
set Qty = [Ship Qty] * (cast(substring(Size, 1, patindex('%[^0-9]%', Size + '/') - 1) as float) *   case when charindex('X', Size, 1) > 0 then 
             cast(substring(substring(Size, charindex('X', Size) + 1, len(Size)), 1, patindex('%[^0-9]%', substring(Size, charindex('X', Size) + 1, len(Size)) + '/') - 1) as float)             
             else  1 end )    
           WHERE Qty IS NULL

Open in new window


This is the Table value

Ship Qty            Size                      Qty
2                   100 EA                  200
1                   14.7 GM                   14

Issue with my query is, decimal value of size column treated as round number and decimal part is ignored.

How to fix to treat whole number and decimal numbers equally?
0
chokka
Asked:
chokka
  • 7
  • 2
  • 2
  • +1
4 Solutions
 
hpdvs2Commented:
your regex should include the decimals:  

^(([0-9]*)|(([0-9]*).([0-9]*)))$
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
For reasons you are experiencing now, it is a poor idea to include the numeric size (100) and unit of measure (EA) in the same field.   Especially when you consider that a user can easily make a typo such that T-SQL can't uniformly split the numeric 14.7's from the char GM's.

Recommend you split these into two separate columns in your table.

Then the math would be easy.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
don't use varchar as data type for numerical data.
instead, use "int" or "decimal"
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
chokkaAuthor Commented:
Both @Jimhorn and @angelIII  -  I will not be able to modify existing Table which has thousands of records.

@Jimhorn, I can create a new table like temp_PlacedOrderdetails and create two different columns as Size and SizeUnit. But again, I need to take a lengthy permission as because database belongs to a Thirdparty software provider.

Anyways for me to fix it without changing the datatype or table schema
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
> I will not be able to modify existing Table which has thousands of records.
even if the table is containing records, you can modify the data types ...
you may add a new column with the proper data type, update that column from the existing one, drop the old column, and rename the new column

though you can work out your queries without changing data types, you will just end up with many more problems ...

"size" column should be 2 columns, one containing the "unit" type, the other the actual size value. ...
0
 
chokkaAuthor Commented:
Table : PlacedOrderDetails  , is loaded from a CSV file which has values SizeUnit (ie. 100 mg or 15 ml ) populated.

If i go with your decision, i have to create a new table such as temp_PlacedOrderDetails which export data from PlacedOrderDetails and split the value of Size Column to two different columns such as Size and Unit
0
 
chokkaAuthor Commented:
@ hpdvs2

Where should i implement your regex expression.

Because, i haven't seen $ sign on my query ???
0
 
chokkaAuthor Commented:
regex format is not working , it throws error saying Substring conversion error !!

I am sure, we can achieve this through Patindex and Charindex. But having problem on formating the decimal.

Something like i am expecting

Case when ( if size column is decimal ) do this

else do this
0
 
hpdvs2Commented:
I believe it supposed to be this, but I don't use regex a lot.  The key is that you get the decimal place as well.  

patindex('^(([0-9]*)|(([0-9]*).([0-9]*)))$' 
patindex('/^\d+\.?\d*$/'

Open in new window


One of those two should work.  Otherwise just search for
regex get numeric with decimal
0
 
chokkaAuthor Commented:
Thank you, I am searching online for the Patindex format. But as per your suggestion, i am getting errors ..


update PlacedOrderDetails          
set Qty = [Ship Qty] * (cast(substring(Size, 1, patindex('^(([0-9]*)|(([0-9]*).([0-9]*)))$', Size + '/') - 1) as float) *   case when charindex('X', Size, 1) > 0 then 
             cast(substring(substring(Size, charindex('X', Size) + 1, len(Size)), 1, patindex('^(([0-9]*)|(([0-9]*).([0-9]*)))$', substring(Size, charindex('X', Size) + 1, len(Size)) + '/') - 1) as float)             
             else  1 end )    
           WHERE Qty IS NULL

Open in new window



Error :

Invalid length parameter passed to the LEFT or SUBSTRING function.
The statement has been terminated.
0
 
chokkaAuthor Commented:
I changed the regex from '%[^0-9]%' to '%[A-Z]%'

Issue is fixed. An expert @Jared_s solved the issue in this posting :

http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/Q_27978053.html



update PlacedOrderDetails          
set Qty = [Ship Qty] * (cast(substring(Size, 1, patindex('%[A-Z]%', Size + '/') - 1) as float) *   case when charindex('X', Size, 1) > 0 then 
             cast(substring(substring(Size, charindex('X', Size) + 1, len(Size)), 1, patindex('%[A-Z]%', substring(Size, charindex('X', Size) + 1, len(Size)) + '/') - 1) as float)             
             else  1 end )    
           WHERE Qty IS NULL

Open in new window

0
 
chokkaAuthor Commented:
Thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now