Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Query multiplication

Posted on 2012-12-26
12
Medium Priority
?
493 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 2
  • 2
  • +1
12 Comments
 
LVL 8

Assisted Solution

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

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

Assisted Solution

by:Jim Horn
Jim Horn earned 375 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 375 total points
ID: 38721565
don't use varchar as data type for numerical data.
instead, use "int" or "decimal"
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

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 750 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

661 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