Solved

How to sum / values in part and underline through SQL

Posted on 2014-03-21
5
244 Views
Last Modified: 2014-03-21
Dear Experts,
I have two fileds, which in need to sum

1. 100/98
2. 10/9

need to show result with under line

110/107

Please help
0
Comment
Question by:Mehram
[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
  • 3
5 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39944957
there is nothing built-in to do it. you will need to find the / to extract the 2 numbers of each fraction, add them and them concat back a string
0
 

Author Comment

by:Mehram
ID: 39944960
can you help to build this
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39944998
your fields are stored as varchar(x)?
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 39945013
try this:

DECLARE @v1 VARCHAR(10) = '100/98'
DECLARE @v2 VARCHAR(10) = '10/9'

DECLARE @v1_1 INT
DECLARE @v1_2 INT
DECLARE @v2_1 INT
DECLARE @v2_2 INT

DECLARE @posv1 int = CHARINDEX('/', @v1)
DECLARE @posv2 int = CHARINDEX('/', @v2)

DECLARE @result VARCHAR(10)

IF (@posv1 = 0) OR (@posv2 = 0)
BEGIN
      SELECT 'invalid values'
END
ELSE
BEGIN
      SELECT @v1, @posv1, @v2, @posv2

      SET @v1_1 = CAST(SUBSTRING(@v1, 1, @posv1-1) AS INT)
      SET @v1_2 = CAST(SUBSTRING(@v1, @posv1+1, 99) AS INT)

      SET @v2_1 = CAST(SUBSTRING(@v2, 1, @posv2-1) AS INT)
      SET @v2_2 = CAST(SUBSTRING(@v2, @posv2+1, 99) AS INT)

      SET @result = CAST(@v1_1+@v2_1 AS VARCHAR) + '/' + CAST(@v1_2+@v2_2 AS VARCHAR)

      SELECT @v1_1, @v1_2, @v2_1, @v2_2, @result
END
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39945031
>need to show result with under line
For starters, SQL Server is a database, and does not graphically render such as underscore.

If you need to perform math on two columns, I highly recommend running some code similar to Eric's above that searches for the /, writes the two numbers into two new columns, then you can SUM() normally.

Otherwise, Eric's T-SQL above is the correct answer (nice job btw).
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 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