Solved

SQL Query multiplication

Posted on 2012-12-26
12
487 Views
Last Modified: 2012-12-27
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
Comment
Question by:chokka
  • 7
  • 2
  • 2
  • +1
12 Comments
 
LVL 8

Assisted Solution

by:hpdvs2
hpdvs2 earned 250 total points
ID: 38721546
your regex should include the decimals:  

^(([0-9]*)|(([0-9]*).([0-9]*)))$
0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 125 total points
ID: 38721554
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
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 125 total points
ID: 38721565
don't use varchar as data type for numerical data.
instead, use "int" or "decimal"
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Comment

by:chokka
ID: 38721734
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38721755
> 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
 

Author Comment

by:chokka
ID: 38721765
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
 

Author Comment

by:chokka
ID: 38721783
@ hpdvs2

Where should i implement your regex expression.

Because, i haven't seen $ sign on my query ???
0
 

Author Comment

by:chokka
ID: 38721895
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
 
LVL 8

Accepted Solution

by:
hpdvs2 earned 250 total points
ID: 38721916
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
 

Author Comment

by:chokka
ID: 38721931
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
 

Author Comment

by:chokka
ID: 38724891
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
 

Author Closing Comment

by:chokka
ID: 38724894
Thanks
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question