Replace Statement?

  • Thread starter Thread starter Jason
  • Start date Start date
J

Jason

In SQL Server the following statement works:

SELECT * FROM [MyTable] WHERE Replace([FieldName], '1', '2') = 'Whatever'

However, it does not work in Access (2003); instead it generates a "Data
Type mismatch" error. Does the 'Replace' statement not work in Access? I
tried switching the apostrophes to quotation marks but this didn't help.

Thanks.
 
Probably one or more records contain a Null value in that field. Try
something like ...

SELECT Table1.Test_Text
FROM Table1
WHERE (((Replace([Test_Text] & ""," ",""))="onetwo"));

Note the empty string appended to the value of the field, to coerce the
result to a string.

Alternatively, you could use the NZ function if you prefer ...
Replace(NZ([TestText], "") ... etc.
 
Thanks. However, it appears that the "Replace" command just doesn't work
when it's in the Where clause. It works fine if I say "Select
Replace(FieldName, 'A', 'B') From MyTable" but doesn't work if I say

"Select Replace(FieldName, 'A', 'B') From MyTable Where Replace(FieldName,
'A', 'B') Is Not Null".

I will keep playing around with it though.

Brendan Reynolds said:
Probably one or more records contain a Null value in that field. Try
something like ...

SELECT Table1.Test_Text
FROM Table1
WHERE (((Replace([Test_Text] & ""," ",""))="onetwo"));

Note the empty string appended to the value of the field, to coerce the
result to a string.

Alternatively, you could use the NZ function if you prefer ...
Replace(NZ([TestText], "") ... etc.

--
Brendan Reynolds (MVP)


Jason said:
In SQL Server the following statement works:

SELECT * FROM [MyTable] WHERE Replace([FieldName], '1', '2') = 'Whatever'

However, it does not work in Access (2003); instead it generates a "Data
Type mismatch" error. Does the 'Replace' statement not work in Access? I
tried switching the apostrophes to quotation marks but this didn't help.

Thanks.
 
I tested my example before I posted it. It works for me. Have you actually
tried what I recommended? Your most recent example is not doing what I
advised, and would be subject to exactly the same problem I described - it
will fail if the field contains any Null values.

In addition, note that the return value of the Replace function is also a
string, and therefore can never be Null.

In summary, you need to coerce any Null values to strings before passing the
value to the Replace function. Attempting to pass Null values and then check
the return value of the function for Null, as your most recently posted
example is doing, is not going to work.

--
Brendan Reynolds (MVP)


Jason said:
Thanks. However, it appears that the "Replace" command just doesn't work
when it's in the Where clause. It works fine if I say "Select
Replace(FieldName, 'A', 'B') From MyTable" but doesn't work if I say

"Select Replace(FieldName, 'A', 'B') From MyTable Where Replace(FieldName,
'A', 'B') Is Not Null".

I will keep playing around with it though.

Brendan Reynolds said:
Probably one or more records contain a Null value in that field. Try
something like ...

SELECT Table1.Test_Text
FROM Table1
WHERE (((Replace([Test_Text] & ""," ",""))="onetwo"));

Note the empty string appended to the value of the field, to coerce the
result to a string.

Alternatively, you could use the NZ function if you prefer ...
Replace(NZ([TestText], "") ... etc.

--
Brendan Reynolds (MVP)


Jason said:
In SQL Server the following statement works:

SELECT * FROM [MyTable] WHERE Replace([FieldName], '1', '2') =
'Whatever'

However, it does not work in Access (2003); instead it generates a "Data
Type mismatch" error. Does the 'Replace' statement not work in Access? I
tried switching the apostrophes to quotation marks but this didn't help.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top