7

I'm doing some work in MS Access and I need to append a prefix to a bunch of fields, I know SQL but it doesn't quite seem to work the same in Access

Basically I need this translated to a command that will work in access:

UPDATE myTable
SET [My Column] = CONCAT ("Prefix ", [My Column]) 
WHERE [Different Column]='someValue';

I've searched up and down and can't seem to find a simple translation.

HansUp
  • 95,961
  • 11
  • 77
  • 135
salty
  • 594
  • 4
  • 6
  • 18

4 Answers4

16

There are two concatenation operators available in Access: +; and &. They differ in how they deal with Null.

"foo" + Null returns Null

"foo" & Null returns "foo"

So if you want to update Null [My Column] fields to contain "Prefix " afterwards, use ...

SET [My Column] = "Prefix " & [My Column]

But if you prefer to leave it as Null, you could use the + operator instead ...

SET [My Column] = "Prefix " + [My Column]

However, in the second case, you could revise the WHERE clause to ignore rows where [My Column] contains Null.

WHERE [Different Column]='someValue' AND [My Column] Is Not Null
HansUp
  • 95,961
  • 11
  • 77
  • 135
  • Ah, I had not realized there was a difference in the two, thanks HansUp, this is good to know as I do not want to Null out all my values! – salty Dec 05 '13 at 15:57
13
UPDATE myTable
SET [My Column] = "Prefix " & [My Column] 
WHERE [Different Column]='someValue';

As far as I am aware there is no CONCAT

Fred
  • 5,663
  • 4
  • 45
  • 74
2

You can use the & operator:

UPDATE myTable
    SET [My Column] = "Prefix " & [My Column]
    WHERE [Different Column]='someValue';
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
2

Since there is no Concat function in MS-ACCESS, you can simply combine both strings with + operator:

  UPDATE myTable
 SET [My Column] = "Prefix " + [My Column]
 WHERE [Different Column]='someValue';
Kiril Rusev
  • 745
  • 3
  • 9
  • 2
    For string concatination you should use & as it explicitly a string operation. The plus sign could give unexpected results – Fred Dec 05 '13 at 15:42