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.
Author |
Topic |
Pete_N
Posting Yak Master
181 Posts |
Posted - 2012-11-30 : 12:26:47
|
I have a table which holds a reference number, however past oprartor have not made the references up correctly. Part of the reference is an internal id number for a client. What I am trying to do is get the following Sample data132116234CL21781701343198390CL217817166841312CL2178170180485687CL21781701813994454CL217817217020364981625CL217817039315843CL21781700613500875CL2178170CC100747CL217748CC101273CL217748CC102782CL217748CC103652CL217748If there is a CL in the reference the id is the next 6 digits. If no CL then the id is the first 6 digitswhat I want to get out is just the distinct licence numbersso I should end up with217817217020217748code so far that returns all records SELECT CASE WHEN CHARINDEX('CL', reference, 1) > 0 THEN SUBSTRING(reference , CHARINDEX('CL', reference, 1)+ 2, 6) ELSE LEFT(reference,6 ) END as 'ClientID' FROM #ADDACSBODY |
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2012-11-30 : 12:47:05
|
I think you just nee a DISTINCTSELECT DISTINCTCASE WHEN CHARINDEX('CL', reference, 1) > 0 THEN SUBSTRING(reference , CHARINDEX('CL', reference, 1)+ 2, 6) ELSE LEFT(reference,6 )END as 'ClientID'FROM #ADDACSBODYJimEveryday I learn something that somebody else already knew |
|
|
Pete_N
Posting Yak Master
181 Posts |
Posted - 2012-11-30 : 12:48:23
|
Cheers Jim,Works a treat Pete |
|
|
|
|
|
|
|