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)
 Number Manipulation

Author  Topic 

dotnetnewbie001
Starting Member

2 Posts

Posted - 2010-10-21 : 12:54:56
Hi All,

I'm very new to SQL Server 2008, and need a bit of help manipulating numbers.

I've got a int column holding numbers up to 11 digits. I need to do the following manipulation and use it in a SQL query. Basically, the first digit is transfered to the 3rd position on the right side.

12345678
23456178

or

1234
2134

The values are variable, there are even records with only a value of 1.

Thanks in advance.

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2010-10-21 : 15:26:45
This works okay. You have to turn the number into a string so you can manipulate it like one.

DECLARE @in int
DECLARE @out varchar(11)
SET @in = 123456789
SET @out = convert(varchar(11),@in)

SELECT CASE WHEN LEN(@OUT) > 2
THEN substring(@out,2,len(@out)-3)+left(@out,1)+right(@out,2)
ELSE @out
END

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

dotnetnewbie001
Starting Member

2 Posts

Posted - 2010-10-22 : 05:06:31
Thanks very much
Go to Top of Page
   

- Advertisement -