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 |
vijay1234
Starting Member
48 Posts |
Posted - 2014-03-16 : 02:38:30
|
Hello All,I have created an user defined function with one input parameter. The input parameter is a string (varchar) which uses spaces,comma & '.' at any index level ( starting, ending or middle ). As per the business req i have put some conditions and are below.1)String starting with '-','.',space (' '), characters should be replaced with NULL ('') which means no space should occur and the rest portion of the string should start from the first index. (Ex:_KaranPratap -->KaranPratap )2) String using specific words at any stage, used wild card ( like operator) should result as 'Audio' as ouput.3) String using specific words at any stage, used wild card ( like operator) should result as 'Unknown' as ouput.4) String starting with '.' should appear as 'Unknown' as ouput.5) If at all the string is not having any spaces, then the complete string is considered as first part and the same will be resulted as ouput.6) If there multiple spaces in the string then the firt substring (first space before), will be resulted as the ouput.6) If there is any email string with '@' then the pre portion of '@' is to be resulted. (Ex:karanpratap@aol.com --> karanpratap ), if not then the complete string will be considered as first part and will be resulted.The function is used in one of the stored procedures where EPN, the input parameter is being called joining with a view.The results are below as per the above conditionsPN EPNUser123 User123 user0041UnKnown .JH.123.BNM_SDX.2345678UnKnown 123test test@aol.comKaran Karan Pratap Singh ---> (Same should come )Karan€™s Karan€™s iPhoneHere is the function createdCREATE FUNCTION [dbo].[fn_xyz](@EPN VARCHAR(200))RETURNS VARCHAR(200)ASBEGINDECLARE @PN VARCHAR(200)DECLARE @FirstPart VARCHAR(200)-- Condtions -- IF LEFT(@EPN,1) = '-' SET @EPN = REPLACE(@EPN,'-','')IF LEFT(@EPN,1) = '.' SET @EPN = REPLACE(@EPN,'.','')IF LEFT(@EPN,1) = ' ' SET @EPN = SUBSTRING(@EPN,2,LEN(@EPN)-1) IF @EPN LIKE '%ABS78%' OR @EPN LIKE '%DFS%' BEGIN SET @PN='Audio' ENDELSE IF @EPN LIKE '%ABS_DFS%' BEGIN SET @PN='UnKnown' ENDELSE IF ISNUMERIC(REPLACE(@EPN,'.','')) = 1 BEGIN SET @PN='UnKnown' ENDELSE BEGIN--If there is no space in the string then consider full string as first partIF CHARINDEX(' ',@EPN) = 0 SET @FirstPart=@EPNELSE--Extract data before first space as First part SET @FirstPart=LEFT(@EPN,CHARINDEX(' ',@EPN))IF @FirstPart LIKE '%@%' --Extract data before @ in first part as PN SET @PN = LEFT(@FirstPart,CHARINDEX('@',@FirstPart)-1) ELSE--If there is no @ in first part consider first part as PN SET @PN = @FirstPart ENDset @PN= case when isnumeric(@PN) = 1 then 'UnKnown' else @PN endRETURN @PNENDWhat i am looking for is for a logic where the result should be displayed using some split function.Desired ResultFor Ex: Karan Pratap Singh --> Result: Karan Pratap SinghKaran User@aol.com --> Result: karanKaran€™s --> Result: Karan€™s iPhone |
|
|
|
|
|
|