+ Reply to Thread
Results 1 to 8 of 8

Thread: IF..THEN with SQL UPDATE?

  1. #1
    Ian W is offline VB.NET Forum Enthusiast Ian W is on a distinguished programming path ahead
    .NET Framework
    .NET 3.0
    Join Date
    Feb 2010
    Age
    28
    Posts
    31
    Reputation
    8

    Default IF..THEN with SQL UPDATE?

    I am trying to write an update query but I can't figure out how to do something.

    Is it possible to have a update query look at a field and if it is Null update it but if not null skip it?

    Basically have a date field that I want to insert todays date into unless it already has a date.

  2. #2
    r3plica's Avatar
    r3plica is offline VB.NET Forum Enthusiast r3plica done a little coding in his/her time r3plica done a little coding in his/her time r3plica done a little coding in his/her time
    .NET Framework
    .NET 3.5
    Join Date
    Mar 2010
    Age
    30
    Posts
    81
    Reputation
    63

    Default

    This should work

    Code:
      CREATE PROC dbo.UpdateDate
    	@ID INT
      AS
      BEGIN
    	  DECLARE @Date VARCHAR(100)
    	  
    	  SELECT @Date = CAST([Date] AS VARCHAR(100))
    	  FROM tTable
    	  WHERE ID = @ID
    	  
    	  IF @Date = ''
    	  BEGIN
    		UPDATE tTable
    		SET [Date] = GETDATE()
    		WHERE ID = @ID
    	  END
      END
      GO
    Last edited by r3plica; 03-19-2010 at 10:30 AM.
    If I solve your issue or you are happy with my response, please provide me with reputation by clicking the reputation link at the top right of my posts - thanks

  3. #3
    rcombs4 is offline VB.NET Forum Genius rcombs4 puts e.f. hutton to shame rcombs4 puts e.f. hutton to shame rcombs4 puts e.f. hutton to shame rcombs4 puts e.f. hutton to shame rcombs4 puts e.f. hutton to shame rcombs4 puts e.f. hutton to shame
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Aug 2008
    Posts
    167
    Reputation
    158

    Default

    You can also just add a WHERE clause in your update query.

    Code:
    UPDATE MyTable
    SET MyField = GetDate()
    WHERE MyField IS NULL
    --Insert Clever Signature--

  4. #4
    Ian W is offline VB.NET Forum Enthusiast Ian W is on a distinguished programming path ahead
    .NET Framework
    .NET 3.0
    Join Date
    Feb 2010
    Age
    28
    Posts
    31
    Reputation
    8

    Default

    I already have a WHERE clause in it though ?

    Here is my current statement.

    Code:
    "UPDATE Serial_Data_Table SET SDT_PSS_IX = 4 WHERE SDT_Serial = '" & sn & "'"
    So using the above example I want to update SDT_Date if the field is currently Null.

  5. #5
    InertiaM is offline VB.NET Forum Idol InertiaM puts e.f. hutton to shame InertiaM puts e.f. hutton to shame InertiaM puts e.f. hutton to shame InertiaM puts e.f. hutton to shame InertiaM puts e.f. hutton to shame InertiaM puts e.f. hutton to shame
    .NET Framework
    .NET 2.0
    Join Date
    Nov 2007
    Location
    Kent, UK
    Age
    39
    Posts
    563
    Reputation
    175

    Default

    I *believe* this will work.

    Code:
    UPDATE
      Serial_Data_Table
    SET
      Serial_Data_Table.SDT_PSS_IX = 4,
      Serial_Data_Table.SDT_Date = CASE
        WHEN Serial_Data_Table.SDT_Date IS NULL THEN @NEW_SDT_DATE /* NEW VALUE */
        ELSE Serial_Data_Table.SDT_Date END /* NO CHANGE */ 
    WHERE
      Serial_Data_Table.SDT_Serial = @SDT_Serial
    Always parameterize your queries - read more here

  6. #6
    r3plica's Avatar
    r3plica is offline VB.NET Forum Enthusiast r3plica done a little coding in his/her time r3plica done a little coding in his/her time r3plica done a little coding in his/her time
    .NET Framework
    .NET 3.5
    Join Date
    Mar 2010
    Age
    30
    Posts
    81
    Reputation
    63

    Default

    try

    Code:
    UPDATE Serial_Data_Table
    SET 
    	SDT_PSS_IX = 4,
    	SDT_Date = 
    	CASE 
    		WHEN CAST(SDT_Date AS VARCHAR(100)) = '' THEN GETDATE()
    		ELSE NULL
    	END
    WHERE SDT_Serial = @SN
    I have used a parameter for your serial, you can use it the way you had if you like.
    If I solve your issue or you are happy with my response, please provide me with reputation by clicking the reputation link at the top right of my posts - thanks

  7. #7
    r3plica's Avatar
    r3plica is offline VB.NET Forum Enthusiast r3plica done a little coding in his/her time r3plica done a little coding in his/her time r3plica done a little coding in his/her time
    .NET Framework
    .NET 3.5
    Join Date
    Mar 2010
    Age
    30
    Posts
    81
    Reputation
    63

    Default

    i find casting as a varchar and checking for an empty string for some reason works better than checking for NULLs.
    That could just be me though.
    Either solution posted will work (or should)
    If I solve your issue or you are happy with my response, please provide me with reputation by clicking the reputation link at the top right of my posts - thanks

  8. #8
    Ian W is offline VB.NET Forum Enthusiast Ian W is on a distinguished programming path ahead
    .NET Framework
    .NET 3.0
    Join Date
    Feb 2010
    Age
    28
    Posts
    31
    Reputation
    8

    Default

    Many thanks for the replys.

    I think I may have posted this in the wrong section though..

    I'm using an MS Access backend and executing my queries using oledbcommand.ExecuteNonQuery.

    I can't figure out how to get the examples that have been posted working.

    Sorry, i'm new to .net...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts