| Author |
Topic |
|
gerrif55
Starting Member
2 Posts |
Posted - 2004-09-06 : 02:44:46
|
| How can I easily search for an uppercase character in a string, to be able to separate two or more words eg. separate: "ClientRegisteredName" into: "Client" and "Registered" and "Name"Thanks |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2004-09-06 : 04:56:53
|
| select substring(column1,1,6) as client, substring(column1,7,10) as registered, substring(column1,17,4) as name from table1would get you a SOLUTION IN THIS PARTICUALR INSTANCE....but that would depend on the lengths of each word being 5,10 and 4 respectively.if that's not the case....and you are delimiting/identifying the start of each word by the presence of an uppercase character....then 1stly you need to have a 'case sensitive' enabled table/databaseand then you need a repetitive search for characters in the required ascii range...ie...asc(a) <> asc(A)...1 search for each word. |
 |
|
|
jen
Master Smack Fu Yak Hacker
4110 Posts |
Posted - 2004-09-06 : 05:04:52
|
| use a range:if ascii(letter) between 65 and 90 then it's an uppercasehere's an example from BOL------------ExamplesThis example, which assumes an ASCII character set, returns the ASCII value and char character for each character in the string "Du monde entier."SET TEXTSIZE 0SET NOCOUNT ON-- Create the variables for the current character string position -- and for the character string.DECLARE @position int, @string char(15)-- Initialize the variables.SET @position = 1SET @string = 'Du monde entier'WHILE @position <= DATALENGTH(@string) BEGIN SELECT ASCII(SUBSTRING(@string, @position, 1)), CHAR(ASCII(SUBSTRING(@string, @position, 1))) SET @position = @position + 1 ENDSET NOCOUNT OFFGO------------ |
 |
|
|
kselvia
Aged Yak Warrior
526 Posts |
Posted - 2004-09-06 : 05:08:43
|
You could do thisdeclare @var varchar(255)select @var = 'ClientRegisteredName'select Ltrim ( Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( replace (@var COLLATE Latin1_General_BIN ,'A', ' A') ,'B', ' B') ,'C', ' C') ,'D', ' D') ,'E', ' E') ,'F', ' F') ,'G', ' G') ,'H', ' H') ,'I', ' I') ,'J', ' J') ,'K', ' K') ,'L', ' L') ,'M', ' M') ,'N', ' N') ,'O', ' O') ,'P', ' P') ,'Q', ' Q') ,'R', ' R') ,'S', ' S') ,'T', ' T') ,'U', ' U') ,'V', ' V') ,'W', ' W') ,'X', ' X') ,'Y', ' Y') ,'Z', ' Z') ) --KenI want to die in my sleep like my grandfather, not screaming in terror like his passengers. |
 |
|
|
gerrif55
Starting Member
2 Posts |
Posted - 2004-09-06 : 23:53:14
|
Thanks Jen, much appreciated...Here is a solution that separates 'ClientRegisteredName'into 'Client' 'Registered' and 'Name'. declare @word varchar(20)select @word = 'ClientRegisteredName'DECLARE @WordTwoPos int, @WordThreePos intSET @WordTwoPos = 2WHILE @WordTwoPos <= DATALENGTH(@word)BEGIN if ascii(SUBSTRING(@word, @WordTwoPos, 1)) between 65 and 90 break else SET @WordTwoPos = @WordTwoPos + 1ENDSet @WordThreePos = @WordTwoPos + 1WHILE @WordThreePos <= DATALENGTH(@word)BEGIN if ascii(SUBSTRING(@word, @WordThreePos, 1)) between 65 and 90 break else SET @WordThreePos = @WordThreePos + 1ENDselect substring(@word, 1, @WordTwoPos -1) as Word1, substring(@word, @WordTwoPos, @WordThreePos - @WordTwoPos) as Word2, substring (@word, @WordThreePos, len(@word)-@WordThreePos+1) as Word3 |
 |
|
|
|
|
|