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)
 equivalent of substring syntax for numeric string

Author  Topic 

dnf999
Constraint Violating Yak Guru

253 Posts

Posted - 2007-01-08 : 07:07:28
Hi

I have a numeric field and I need to to identify certain digits within the field.

i.e. field1
1234567

I need to pick out the first, second and last digits and add them up. I know that if the field was varchar i could simply use the substring syntax to obtain these, but as the field is numeric I was wondering if there is an equivalent method to get this...??

Thanks in advance!!

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-01-08 : 07:31:23
select field1 / power(10, len(field1) - 1) + field1 / power(10, len(field1) - 2) % 10 + field1 % 10


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-01-08 : 07:32:02
Or simply CAST the integer field as VARCHAR first.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -