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 2005 Forums
 Transact-SQL (2005)
 moving - from end of #

Author  Topic 

duhaas
Constraint Violating Yak Guru

310 Posts

Posted - 2011-12-16 : 16:10:33
have a varchar(50) field that is really a decimal once converted except the numbers that are negative reflect the - at the end of the number:

36651.070-
101269.900-
84625.320-
288898.320-
125958.000-
58213.230-
2174.150-
648.970-
7285.990-
707.710-
55345.240-
29324.450-
252423.370-
990403.840-


To to move the - to the beginning of the field if one exists.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-12-16 : 16:19:43
select '-' + replace(c1, '-', '')
from yourtable
where c1 like '%-'

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

duhaas
Constraint Violating Yak Guru

310 Posts

Posted - 2011-12-16 : 16:43:12
quote:
Originally posted by tkizer

select '-' + replace(c1, '-', '')
from yourtable
where c1 like '%-'

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog



Thanks Tara for the suggestion, I should of mentioned there are also other numbers in this table that dont have the '-' meaning I wouldn't want to place that in front of every record returned. My apologies for not being entirely clear
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-12-16 : 16:43:45
That's why I have the where clause on there.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

duhaas
Constraint Violating Yak Guru

310 Posts

Posted - 2011-12-16 : 16:55:39
quote:
Originally posted by tkizer

That's why I have the where clause on there.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog



What if i wanted both sets of results returned regardless of - at the end. basically:

select number from table

results:

1
2
3
-3
-4
3
4
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-12-16 : 17:00:55
select c1 = case when c1 like '%-' then '-' + replace(c1, '-', '') else c1 end
from yourtable


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

duhaas
Constraint Violating Yak Guru

310 Posts

Posted - 2011-12-16 : 17:03:56
quote:
Originally posted by tkizer

select c1 = case when c1 like '%-' then '-' + replace(c1, '-', '') else c1 end
from yourtable


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog



Thanks much tara, enjoy the weekend
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-12-16 : 17:10:42
You're welcome, you too.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -