SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help with Multiple Case Statements
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

dave1816
Starting Member

United Kingdom
9 Posts

Posted - 04/15/2013 :  05:32:10  Show Profile  Reply with Quote
I’m trying to figure out How I would write a case statement for the following:-

Values: 1a, 01a, 01b,1b,01c,1c ,10c etc...

I need to basically to remove the Characters/lettering from any number value with a letter in i.e. 10c = 10

any clues?


I have figured out using this example: SUBSTRING(inv_ms_num, 1, 1) I can trim a letter out but have no idea how to check/find the letter to use the SUBSTRING code. I'm assuming I need a case statement which says if 1st, 2nd or 3rd Characters has a letter then remove it....

Dave

D.ARNOLD

bandi
Flowing Fount of Yak Knowledge

India
2206 Posts

Posted - 04/15/2013 :  05:51:32  Show Profile  Reply with Quote
Check this link...

http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/sql-server-extracting-data-again


EDIT: If you have fixed format alpha numeric values, let us know.. we can provide smarter ways
--
Chandu

Edited by - bandi on 04/15/2013 06:01:02
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2206 Posts

Posted - 04/15/2013 :  05:56:57  Show Profile  Reply with Quote
-- Check the below script
SELECT X AS A, N AS B
INTO TXNS
FROM (VALUES(1,'1a'),(2,'fwsa38#'), (3, '01a'),(4, '01b'),(5, '1b'),(6, '01c'),( 7, '1c') ,( 8, '10c')
	)a(X,N);

SELECT *
FROM TXNS

;WITH Tally(N) AS (SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) 
                  FROM(VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0))a(N))
SELECT A,B,stripped 
FROM TXNS b
CROSS APPLY (SELECT 
				(SELECT CASE 
					WHEN PATINDEX('[0-9]',SUBSTRING(B,N,1)) = 1 
					THEN SUBSTRING(B,N,1)
                    ELSE '' END
                  FROM Tally
                  WHERE N <= LEN(B)
                  FOR XML PATH(''), TYPE
                  ).value('.', 'VARCHAR(10)') 
            ) c(stripped)

DROP TABLE TXNS


--
Chandu

Edited by - bandi on 04/15/2013 05:59:13
Go to Top of Page

dave1816
Starting Member

United Kingdom
9 Posts

Posted - 04/15/2013 :  11:51:30  Show Profile  Reply with Quote
Thanks Chandu!

first solution worked great! just got to find out why it won't work in crystal now very strange

regards

D.ARNOLD
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2206 Posts

Posted - 04/16/2013 :  00:57:43  Show Profile  Reply with Quote
quote:
Originally posted by dave1816

Thanks Chandu!
first solution worked great! just got to find out why it won't work in crystal now very strange


Welcome

--
Chandu
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000