Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 if else statement

Author  Topic 

JJ297
Aged Yak Warrior

940 Posts

Posted - 2010-03-17 : 11:30:56
If a person doesn't enter a date in the date field this goes into the DB 1/1/1900 12:00:00 AM

How can I get the word NULL to go into the DB when a date isn't entered into the date field?

I just need it for the these two highlighted below:

@DteToFO datetime,
@DteBack datetime,

Thanks!

ALTER procedure [dbo].[UpdateClaimant]
@Claim char(6),
@PIC char(2),
@FinalLaf varchar(2),
@DteIn datetime,
@Remarks varchar(255),
@BICAdj varchar(3),
@NoBIC varchar(255),
@IA int,
@FD int,
@DteToFO datetime,
@DteBack datetime,

@Results int,
@Amt money


AS update TestData

set
[Claim] = @Claim,
[PIC] = @PIC,
[FinalLaf] = @FinalLaf,
[DteIn] = @DteIn,
[Remarks] = @Remarks,
[BICAdj] = @BICAdj,
[NoBIC] = @NoBIC,
[IA] = @IA,
[FD] = @FD,
[DteToFO] = @DteToFO,
[DteBack] = @DteBack,
[Results] = @Results,
[Amt] = @Amt

Where claim=@claim and pic=@pic

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-17 : 11:35:01
pass NULL value explicitly for date parameters

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2010-03-17 : 11:39:43
Thank you so much for helping me. I'm working on a project that's due today. Thanks that worked!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-17 : 11:42:18
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2010-03-17 : 11:42:36
Back again

I added this to the stored procedure:
[DteToFO] = NULL,
[DteBack] = NULL,

So when I went back to add a date it didn't add it. What am I doing wrong?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-17 : 11:43:49
can you show the insert code?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2010-03-17 : 11:46:31
I tried this but it didn't work:

if dtetofo = "" then
[DteToFO] = NULL,
else
[DteToFO] = @DteToFO,


ALTER procedure [dbo].[UpdateClaimant]
@Claim char(6),
@PIC char(2),
@FinalLaf varchar(2),
@DteIn datetime,
@Remarks varchar(255),
@BICAdj varchar(3),
@NoBIC varchar(255),
@IA int,
@FD int,
@DteToFO datetime,
@DteBack datetime,
@Results int,
@Amt money


AS update TestData

set
[Claim] = @Claim,
[PIC] = @PIC,
[FinalLaf] = @FinalLaf,
[DteIn] = @DteIn,
[Remarks] = @Remarks,
[BICAdj] = @BICAdj,
[NoBIC] = @NoBIC,
[IA] = @IA,
[FD] = @FD,
[DteToFO] = @DteToFO,
[DteBack] = @DteBack,
[Results] = @Results,
[Amt] = @Amt

Where claim=@claim and pic=@pic
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-17 : 11:50:55
is that if ..else inside sql? or is that front end code?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2010-03-17 : 11:53:25
Yes it's in sql. I tried this but Incorrect syntax near the keyword 'CASE'.

ALTER procedure [dbo].[UpdateClaimant]
@Claim char(6),
@PIC char(2),
@FinalLaf varchar(2),
@DteIn datetime,
@Remarks varchar(255),
@BICAdj varchar(3),
@NoBIC varchar(255),
@IA int,
@FD int,
@DteToFO datetime,
@DteBack datetime,
@Results int,
@Amt money


AS update TestData

set
[Claim] = @Claim,
[PIC] = @PIC,
[FinalLaf] = @FinalLaf,
[DteIn] = @DteIn,
[Remarks] = @Remarks,
[BICAdj] = @BICAdj,
[NoBIC] = @NoBIC,
[IA] = @IA,
[FD] = @FD,
CASE
WHEN (dtetofo)= Null THEN Null
ELSE
[DteToFO] = @DteToFO end,
[DteBack] = @DteBack,
[Results] = @Results,
[Amt] = @Amt

Where claim=@claim and pic=@pic
Go to Top of Page

JJ297
Aged Yak Warrior

940 Posts

Posted - 2010-03-17 : 12:19:30
Figured it out...I added it to the front end:


 If DteToFOtxt.Text = "" Then
.Parameters.AddWithValue("@DteToFO", DBNull.Value)
Else
.Parameters.AddWithValue("@DteToFO", DteToFOtxt.Text)
End If

If DteFromtxt.Text = "" Then
.Parameters.AddWithValue("@DteBack", DBNull.Value)
Else
.Parameters.AddWithValue("@DteBack", DteFromtxt.Text)
End If


Go to Top of Page
   

- Advertisement -