1

I have an sql function and at the end RETURN @_result;.

My question is that if I put this

REPLACE(@_result, '&', 'eseses')
REPLACE(@_result, '-', 'vagyvagy')

before RETURN @_result; that is OK?

@_result returns a long string and in that string I want to replace & to eseses and - to vagyvagy.

1
  • Think you need to specify your database manager software - Is this question about TransactSQL from SQLServer? You definitely need to retag this question to include the database type and development environment Commented Feb 3, 2010 at 15:10

4 Answers 4

2

You need to put the following:

SET @_result = REPLACE(@_result, '&', 'eseses')
SET @_result = REPLACE(@_result, '-', 'vagyvagy')
Sign up to request clarification or add additional context in comments.

1 Comment

Looks like we thought of the same thing at the same time. :-)
2

Yes, the idea is ok but your code isn't syntactically sound. You'd need to do:

SET @_result = REPLACE(@_result, '&', 'eseses')
SET @_result = REPLACE(@_result, '-', 'vagyvagy')

RETURN @_result

You may know this but I wanted to ensure that you're clear on that in case you are trying it and it's not working. (Some DB implementations may allow REPLACE w/o the set, but I know of none.)

Comments

0

yes that is okay in SQL Server

Comments

0

You need the SET before the REPLACE in SQL 2008.

When I run the following

DECLARE @_result as varchar(30)

SET @_result = 'Hello-There & How are you?'

REPLACE(@_result, '&', 'eseses')
REPLACE(@_result, '-', 'vagyvagy')

SELECT @_result

I get the following error

Msg 102, Level 15, State 1, Line 5
Incorrect syntax near 'REPLACE'.

By updating it to the following, the error went away and everything is set correctly

DECLARE @_result as varchar(30)

SET @_result = 'Hello-There & How are you?'

SET @_result = REPLACE(@_result, '&', 'eseses')
SET @_result = REPLACE(@_result, '-', 'vagyvagy')

SELECT @_result

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.