I am looking for solution to get a character based string extracted from another string.
I need only first 4 "characters only" from another string.
The restriction here is that "another" string may contain spaces, special characters, numbers etc and may be less than 4 characters.
For example - I should get
"NAGP" if source string is "Nagpur District"
"ILLF" if source string is "Ill Fated"
"RAJU" if source string is "RA123 *JU23"
"MAC" if source string is "MAC"
Any help is greatly appreciated.
Thanks for sharing your time and wisdom.
vks :
^([a-zA-Z])[^a-zA-Z\\n]*([a-zA-Z])?[^a-zA-Z\\n]*([a-zA-Z])?[^a-zA-Z\\n]*([a-zA-Z])?\n\n\nYou can try this.Grab the captures or groups.See demo.\n\nhttp://regex101.com/r/rQ6mK9/42",
2014-10-22T11:12:16
Satyajit :
You can use the answer in the question and add substring method to get your value of desired length\nHow to strip all non-alphabetic characters from string in SQL Server?\n\ni.e.\n\nCreate Function [dbo].[RemoveNonAlphaCharacters](@Temp VarChar(1000))\nReturns VarChar(1000)\nAS\nBegin\n\n Declare @KeepValues as varchar(50)\n Set @KeepValues = '%[^a-z]%'\n While PatIndex(@KeepValues, @Temp) > 0\n Set @Temp = Stuff(@Temp, PatIndex(@KeepValues, @Temp), 1, '')\n\n Return @Temp\nEnd\n\n\nuse it like\n\nSelect SUBSTRING(dbo.RemoveNonAlphaCharacters('abc1234def5678ghi90jkl'), 1, 4);\n\n\nHere SUBSTRING is used to get string of length 4 from the returned value.",
2014-10-22T11:20:51