Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Convert string to decimal

Author  Topic 

cvipin
Yak Posting Veteran

51 Posts

Posted - 2013-04-08 : 14:47:47
Is there a quick way to achieve following in select statement:

00000000403.50- --> -403.50
00000000403.50+ --> 403.50

Thanks
Vipin

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-04-08 : 14:59:12
Could possibly be done more cleverly than the query below, but if you always have a + or - at the end, this should work:
DECLARE @X VARCHAR(32) = '00000000403.50-';
SELECT CAST(STUFF(@X,PATINDEX('%[+-]',@X),1,'') AS DECIMAL(19,2))
*CASE WHEN @X LIKE '%-' THEN -1 ELSE 1 END
Go to Top of Page
   

- Advertisement -