Solved

SQL Query multiplication

Posted on 2012-12-26
12
488 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 143

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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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 143

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

830 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