Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


A little help needed with simple Custom Function


A little help needed with simple Custom Function

Author
Message
RedBirdOBX
RedBirdOBX
SSC-Enthusiastic
SSC-Enthusiastic (100 reputation)SSC-Enthusiastic (100 reputation)SSC-Enthusiastic (100 reputation)SSC-Enthusiastic (100 reputation)SSC-Enthusiastic (100 reputation)SSC-Enthusiastic (100 reputation)SSC-Enthusiastic (100 reputation)SSC-Enthusiastic (100 reputation)

Group: General Forum Members
Points: 100 Visits: 243
This should be a simple task but I think I'm tripping over the syntax. I have data from a database that is fed out onto a web page (aspx). Due to reasons out of my control, the data comes to me as "dirty". 99% but not 100% of the data comes with "junk" characters appended to the end of the string. So, full name might look like "Doe, John, H *^" where it should look like "Doe, John, H".

I could do this one the front end and rewrite some of the web page's logic but I'm trying to handle this on the back end. It would seem I could write a simple Function to "strip" out junk chars I see fit to remove. So, in my query, I could do something like:


SELECT dbo.MyCustomFunction(FieldName), Field2, Field3 FROM......etc...


Then, if I've written the function correct, it'll just strip out the bad chars. I've done this many times before with VB.

The Custom Function on SQL Server is where I'm having trouble. Here's what I have so far:


CREATE FUNCTION fncStripBadChars

(@strInputString varchar(150))


RETURNS varchar(150)

AS

BEGIN

@strInputString = Replace((@strInputString), ", * *", "")
RETURN @strInputString

END

GO


It's the BOLDED line above is where I'm having a little trouble. I'm getting an incorrect Syntax error.

Any tips or pointers? See what I'm doing wrong?

I should also mention that the ",* *" is one of the few strings I'm trying to replace with an empty string.
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8582 Visits: 18157
The problem is you're using double quotes instead of single quotes.
However, you should really avoid doing a simple replace with a UDF as it will degrade performance in a horrible way.
Why can't you ise the following? How dirty is the data you're receiving? With more details we could offer better help.

SELECT REPLACE(FieldName, ', * *', ''),
Field2,
Field3
FROM SomeTable




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16667 Visits: 17030
You are also missing a SET or SELECT on the line with your replace.


set @strInputString = Replace((@strInputString), ', * *', '')



I agree with Luis that doing this in a scalar function is performance timebomb. With more details we can help you put together a solution will not only work but also do it FAST!!!

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8582 Visits: 18157
Maybe you could use something like this. The CTE is just to have some sample data to test with.

WITH SampleData(String) AS(
SELECT 'Doe, John, H *^' UNION ALL
SELECT 'Doe, John, H, **' UNION ALL
SELECT 'Doe, John, H *¨#$'
)
SELECT LEFT( String, LEN(String) - PATINDEX( '%[A-Za-z]%', REVERSE(String)) + 1)
FROM SampleData




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
RedBirdOBX
RedBirdOBX
SSC-Enthusiastic
SSC-Enthusiastic (100 reputation)SSC-Enthusiastic (100 reputation)SSC-Enthusiastic (100 reputation)SSC-Enthusiastic (100 reputation)SSC-Enthusiastic (100 reputation)SSC-Enthusiastic (100 reputation)SSC-Enthusiastic (100 reputation)SSC-Enthusiastic (100 reputation)

Group: General Forum Members
Points: 100 Visits: 243
Good point. It's about 4000 records that would call this function.

The data is pretty dirty. I'm pulling from a mainframe and retrieving First Name, Middle Name and Last Name. 19 out 20 times, First and Middle Name are all crammed into Last Name. But not always. Since the mainframe requires an entry on First and Middle Name, the user (not us) will just type crap into the fields.

In any event, let me experiment with doing it directly in the SELECT Statement.

Thanks guys - you guys are great.
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4283 Visits: 6431
You can also use a pattern splitting FUNCTION like below:


WITH SampleData(String) AS(
SELECT 'Doe, John, H *^' UNION ALL
SELECT 'Doe, John, H, **' UNION ALL
SELECT 'Doe, John, H *¨#$')
SELECT String=Item
FROM SampleData
CROSS APPLY dbo.PatternSplitCM(String, '[A-Za-z ,]')
WHERE [Matched]=1




That FUNCTION can be found in the 4th article in my signature links.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search