September 14, 2010 at 12:18 am
How do I convert a negative varchar value (50.10-), the minus sign is at the end of the value, to a negative numeric value (-50.10)
thanks
September 14, 2010 at 12:42 am
The simplest way is you use SUBSTRING fuction.
My example:
DECLARE @NegativeNumeric varchar(100)
SET @NegativeNumeric='50.10-'
SELECT (-1)*CAST(SUBSTRING(@NegativeNumeric,1,CHARINDEX('-',@NegativeNumeric)-1) AS numeric(18,2))
September 14, 2010 at 12:46 am
thanks, will try it
September 14, 2010 at 12:52 am
My two cents:
SET NOCOUNT ON
DECLARE @NegativeNumeric VARCHAR(100)
SELECT @NegativeNumeric='50.10-'
SELECT @NegativeNumeric = -1 * CAST ( LEFT (@NegativeNumeric , (LEN(@NegativeNumeric)-1)) AS DECIMAL (10,2))
SELECT @NegativeNumeric AS [ConvertedToNegativeNumber]
September 14, 2010 at 1:25 am
For what its worth ... Hopefully it adds some flexibility
DECLARE @l_CharVal VARCHAR(20)
SET @l_CharVal = ' 50.12- '
SELECT
CASE
WHEN CHARINDEX('-', LTRIM(RTRIM(@l_CharVal))) = LEN(LTRIM(@l_CharVal))
THEN CONVERT(DECIMAL(5, 2), LEFT(LTRIM(RTRIM(@l_CharVal)), LEN(LTRIM(@l_CharVal)) - 1)) * -1 -- We know this has a trailing "-"
ELSE CONVERT(DECIMAL(5, 2), LTRIM(RTRIM(@l_CharVal)))
END
September 14, 2010 at 1:26 am
Another dig:
SELECT @NegativeNumeric = -1 * CAST ( REPLACE(@NegativeNumeric ,'-','') AS DECIMAL (10,2))
SELECT @NegativeNumeric AS [ConvertedToNegativeNumber]
September 14, 2010 at 1:29 am
LTRIM & RTRIM from the above post forced me for this:
SET NOCOUNT ON
DECLARE @NegativeNumeric VARCHAR(100)
SELECT @NegativeNumeric='50.10-'
SELECT @NegativeNumeric = -1 * CAST ( REPLACE(LTRIM(RTRIM(@NegativeNumeric)) ,'-','') AS DECIMAL (10,2))
SELECT @NegativeNumeric AS [ConvertedToNegativeNumber]
SELECT @NegativeNumeric=' 454850.10000- '
SELECT @NegativeNumeric = -1 * CAST ( REPLACE(LTRIM(RTRIM(@NegativeNumeric)) ,'-','') AS DECIMAL (10,2))
SELECT @NegativeNumeric AS [ConvertedToNegativeNumber]
~Edit: Included trailing and leading spaces
September 14, 2010 at 1:32 am
Completely generic - no CAST/CONVERTS...
DECLARE @MyTable TABLE (MyNumericVal VARCHAR(100))
INSERT INTO @MyTable VALUES ('50.10-'),
('-50.10'),
('50.10'),
('abc')
SELECT CASE WHEN CHARINDEX('-',MyNumericVal) > 0 THEN SUBSTRING(MyNumericVal,CHARINDEX('-',MyNumericVal),LEN(MyNumericVal)) + SUBSTRING(MyNumericVal,0,CHARINDEX('-',MyNumericVal))
ELSE MyNumericVal
END
FROM @MyTable
Thanks & Regards,
Nakul Vachhrajani.
http://nakulvachhrajani.com
Follow me on
Twitter: @sqltwins
September 14, 2010 at 1:36 am
Nakul Vachhrajani (9/14/2010)
Completely generic - no CAST/CONVERTS...
DECLARE @MyTable TABLE (MyNumericVal VARCHAR(100))
INSERT INTO @MyTable VALUES ('50.10-'),
('-50.10'),
('50.10'),
('abc')
SELECT CASE WHEN CHARINDEX('-',MyNumericVal) > 0 THEN SUBSTRING(MyNumericVal,CHARINDEX('-',MyNumericVal),LEN(MyNumericVal)) + SUBSTRING(MyNumericVal,0,CHARINDEX('-',MyNumericVal))
ELSE MyNumericVal
END
FROM @MyTable
With all due respect to ur wonderful code, the OP's request was to convert it to a numeric value. His quote:
the minus sign is at the end of the value, to a negative numeric value (-50.10)
So IMHO, we WILL need CASTs and CONVERTs...
September 14, 2010 at 1:38 am
Nakul Vachhrajani (9/14/2010)
Completely generic - no CAST/CONVERTS...
DECLARE @MyTable TABLE (MyNumericVal VARCHAR(100))
INSERT INTO @MyTable VALUES ('50.10-'),
('-50.10'),
('50.10'),
('abc'),
(' 50.10- ')
SELECT CASE WHEN CHARINDEX('-',MyNumericVal) > 0 THEN SUBSTRING(MyNumericVal,CHARINDEX('-',MyNumericVal),LEN(MyNumericVal)) + SUBSTRING(MyNumericVal,0,CHARINDEX('-',MyNumericVal))
ELSE MyNumericVal
END
FROM @MyTable
Forgot the LTRIM and this returns a Character!:-P
lol - Ten Centuries beat me to it!
I hope we gave the OP some ideas!
September 14, 2010 at 1:51 am
Thanks SSC-Enthusiastic
I forgot to mention that some values do not have a negative at the end, but your code worked.
Thanks again for all the replies.
September 14, 2010 at 2:21 am
Would this do ?
DECLARE @MyTable TABLE (MyNumericVal VARCHAR(100))
INSERT INTO @MyTable VALUES ('50.10-'),
('-50.10'),
('50.10'),
(' 50.10- ')
SELECT CASE
WHEN CHARINDEX('-',MyNumericVal) > 0
THEN -1 * CAST ( REPLACE(LTRIM(RTRIM(MyNumericVal)) ,'-','') AS DECIMAL (10,2))
ELSE
MyNumericVal
END AS Converted
FROM @MyTable
September 14, 2010 at 3:19 am
Thanks SSC-Enthusiastic
this worked for me, I forgot to mentioned that some values be negative, but this works
DECLARE @l_CharVal VARCHAR(20)
SET @l_CharVal = ' 50.12- '
SELECT
CASE
WHEN CHARINDEX('-', LTRIM(RTRIM(@l_CharVal))) = LEN(LTRIM(@l_CharVal))
THEN CONVERT(DECIMAL(5, 2), LEFT(LTRIM(RTRIM(@l_CharVal)), LEN(LTRIM(@l_CharVal)) - 1)) * -1 -- We know this has a trailing "-"
ELSE CONVERT(DECIMAL(5, 2), LTRIM(RTRIM(@l_CharVal)))
END
from aatest
September 14, 2010 at 7:32 am
coenie-353912 (9/14/2010)
Thanks SSC-Enthusiasticthis worked for me, I forgot to mentioned that some values be negative, but this works
DECLARE @l_CharVal VARCHAR(20)
SET @l_CharVal = ' 50.12- '
SELECT
CASE
WHEN CHARINDEX('-', LTRIM(RTRIM(@l_CharVal))) = LEN(LTRIM(@l_CharVal))
THEN CONVERT(DECIMAL(5, 2), LEFT(LTRIM(RTRIM(@l_CharVal)), LEN(LTRIM(@l_CharVal)) - 1)) * -1 -- We know this has a trailing "-"
ELSE CONVERT(DECIMAL(5, 2), LTRIM(RTRIM(@l_CharVal)))
END
from aatest
You might want to take a gander at the simplicity of ColdCoffee's code.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 14, 2010 at 7:59 am
Jeff Moden (9/14/2010)
You might want to take a gander at the simplicity of ColdCoffee's code.
I was telling the OP to have a look at that from the start Jeff, he wasn't interested :-P.. pro'lly he dislikes any form of Coffee :hehe:
Viewing 15 posts - 1 through 15 (of 16 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy