Solved

SQL Query multiplication

Posted on 2012-12-26
12
482 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
Comment Utility
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
Comment Utility
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
Comment Utility
don't use varchar as data type for numerical data.
instead, use "int" or "decimal"
0
 

Author Comment

by:chokka
Comment Utility
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]
Comment Utility
> 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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:chokka
Comment Utility
@ hpdvs2

Where should i implement your regex expression.

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

Author Comment

by:chokka
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now