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
 Help with Multiple Case Statements

Author  Topic 

dave1816
Starting Member

9 Posts

Posted - 2013-04-15 : 05:32:10
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
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-04-15 : 05:51:32
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
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-04-15 : 05:56:57
-- 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
Go to Top of Page

dave1816
Starting Member

9 Posts

Posted - 2013-04-15 : 11:51:30
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
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-04-16 : 00:57:43
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
   

- Advertisement -