Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
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
madhivanan
Premature Yak Congratulator
22864 Posts
Posted - 2009-08-19 : 03:35:19
orselect substring(myColumn, 1, charindex('-', myColumn)-1)*1 FROM myTable MadhivananFailing to plan is Planning to fail
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?