0

I'm trying to do an insert query like follow:

Insert Into table1 (column1, column2)
Values (#value1#, #value2#)

But let's say, I want to default column2 to 2/19/2013 when the parameter #value2# is null. How would I achieve that?

I'm using Ibatis framework, and the insert statement is in a XML file, which is used in a db file that is called from the controller.

1
  • you want query in sql, right? what is data type for column2? Commented Feb 19, 2013 at 20:44

2 Answers 2

2

This may be what you're looking for:

ISNULL(#value2#,'2/19/2013')

Sign up to request clarification or add additional context in comments.

1 Comment

probably want to use GETDATE() in place of string literal: '2/19/2013', since today is 2/19/2013
2

If you can't change insert statements which are coming from an XML file, you might consider creating a trigger

CREATE TRIGGER table1_Insert ON table1
INSTEAD OF INSERT
AS
INSERT INTO table1 (column1, column2)
    SELECT column1, ISNULL(column2,'2/19/2013') FROM INSERTED;

Then if try to insert a NULL

INSERT INTO table1 VALUES (1, NULL);

You'll have

| COLUMN1 |   COLUMN2 |
-----------------------
|       1 | 2/19/2013 |

Here is sqlfiddle

2 Comments

probably want to use GETDATE() in place of string literal: '2/19/2013', since today is 2/19/2013
Absolutely if the intent is to put the current date and not some base date or something else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.