views:

70

answers:

4

So that when you insert 'abc',it'll be converted to 'ABC' automatically.

I'm using MySQL,is it possible?

+6  A: 

You could write an INSERT trigger that does this for you.

Something like:

CREATE TRIGGER Capitalize BEFORE INSERT ON MyTable
SET NEW.MyColumn = UPPER(NEW.MyColumn)
Wim Hollebrandse
But trigger is not available until mysql 5.1,right?
From 5.0 onwards as far as I can see.
Wim Hollebrandse
Is it a typo,NEW should beg MyTable?
No, that's a built-in reference to the newly to be inserted row. Only available in triggers.
Wim Hollebrandse
A: 

What about using Upper function while executing query ? lets say You are using a SP to insert the values ... cast the value to upper case using UPPER function and then insert ?

Amit
I guess regardless of *how* it gets inserted, the OP wants to make sure it is fully capitalized; a trigger would ensure this behaviour.
Wim Hollebrandse
A: 

I think your question is already well answered, but as a side note, this doesn't feel right to me. The data should be capitalized on a higher layer of your application, when the input is validated, or when the output takes place.

Pekka
It depends what level of control the OP has over this. There could potentially be quite a number of applications (even some that OP may not have control over) that insert into this database.But if it's one app, then this kind of logic indeed is better suited to some BL layer. Having said that, it wouldn't quite answer the OP's question.
Wim Hollebrandse
A: 

this really should be done before it is passed to the DB

DBunting