AleAng
AleAng

Reputation: 71

Why is REPLACE not working in my SQL query?

I am creating a stored procedure. Currently queries are being stored in variables. For example:

@sample = 'SELECT * FROM WS_CoolApp_ASample'
@sample += 'Where DATASOURCE.FName = @finder AND DATASOURCE.Age = 23'

I want to remove that 'DATASOURCE.' from the query if a condition is true. So far I've tried:

IF (true)
    REPLACE(@sample, "DATASOURCE.", "")

Why isn't this working?

Upvotes: 0

Views: 1918

Answers (4)

Marinus
Marinus

Reputation: 167

DECLARE @sample nvarchar(max)
SET @sample = 'SELECT * FROM WS_CoolApp_ASample '
SET @sample += 'Where DATASOURCE.FName = @finder AND DATASOURCE.Age = 23'
if(@bit_var = 1)
  SET @sample = REPLACE(@sample,'DATASOURCE.','')

Upvotes: 0

Vikram
Vikram

Reputation: 197

DECLARE @sample nvarchar(max)
SET @sample = 'SELECT * FROM WS_CoolApp_ASample'
SET @sample += 'Where DATASOURCE.FName = @finder AND DATASOURCE.Age = 23'

SELECT REPLACE(@sample,'DATASOURCE.','')

I hope this works.

Upvotes: 0

th1rdey3
th1rdey3

Reputation: 4358

<!-- language: lang-sql -->
DECLARE @sample as nvarchar(500)

set @sample = 'SELECT * FROM WS_CoolApp_ASample';
set @sample += 'Where DATASOURCE.FName = @finder AND DATASOURCE.Age = 23';

-- assign the replaced string back to @sample
set @sample = REPLACE(@sample, 'DATASOURCE.', '')

PRINT @sample

-- prints -> SELECT * FROM WS_CoolApp_ASampleWhere FName = @finder AND Age = 23

Upvotes: 0

Mike D.
Mike D.

Reputation: 4104

You aren't assigning the output of REPLACE() to anything.

@sample = REPLACE(@sample, 'DATASOURCE.', '')

Upvotes: 3

Related Questions