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
 General SQL Server Forums
 New to SQL Server Programming
 LTRIM & RTIM

Author  Topic 

dalvarado
Starting Member

4 Posts

Posted - 2009-08-18 : 19:45:25
I have a field that is set up in the following manner: '001196-12345'. Value varies per row. This is just an example.

I need to create another field in the table that only has the '1196' in it.

How do I just trim the leading 0s and everything after the - and the - itself?

Thanks in advance for your help.

robvolk
Most Valuable Yak

15732 Posts

Posted - 2009-08-18 : 19:59:54
One way to do it:
SELECT cast(cast(substring(myColumn, 1, charindex('-', myColumn)-1) as int) as varchar) FROM myTable
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-08-19 : 03:35:19
or

select substring(myColumn, 1, charindex('-', myColumn)-1)*1 FROM myTable



Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

dalvarado
Starting Member

4 Posts

Posted - 2009-08-24 : 16:17:02
ok i know what mycolumn and mytable are but what is cast & substring?
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-08-24 : 16:20:14
cast:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
substring:
http://msdn.microsoft.com/en-us/library/ms187748.aspx


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-08-24 : 16:21:23
charindex:
http://msdn.microsoft.com/en-us/library/ms186323.aspx


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -