Home:ALL Converter>Update two conditions only if both conditions are met

Update two conditions only if both conditions are met

Ask Time:2018-02-13T15:52:21         Author:D.Trump123

Json Formatter

I would like to update a query if two conditions are met. What's happening in my code is, the code is updated if either one of them are met. Not if both are met at the same time.

    var cmd = new SqlCommand(@"UPDATE [Truck Table] 
                                  SET [TruckID] = @TruckID, 
                                      [PlateNumber] = @PlateNumber 
                                WHERE [Condition] <> 'Good' AND 
                                      [MaximumVolumeLoad] <= 10", Con)

The code updates a cell even if the condition is good, but the maximum volume load is less than or equal to 10. What I want is, it only updates the Cell if the condition is not Good and the Maxvolumeload is <10.

Author:D.Trump123,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/48761902/update-two-conditions-only-if-both-conditions-are-met
Indunil Withana :

There's no issue, only the small mistake at greater than or equal at second where clause. Make is [MaximumVolumeLoad]<10 if you want less than 10 values. ",
2018-02-13T08:22:17
Prabhat G :

Code looks OK, it's mostly because of parenthesis :\n\nvar cmd = new SqlCommand(@\"UPDATE [Truck Table] \n SET [TruckID] = @TruckID, \n [PlateNumber] = @PlateNumber \n WHERE ([Condition] <> 'Good' AND \n [MaximumVolumeLoad]<10)\", Con)\n",
2018-02-13T07:58:00
husky :

your script looks good, try debugging it, check the sql variable; try the script in SSMS\n\nvar sql = \"UPDATE [Truck Table] SET [TruckID] = @TruckID, [PlateNumber] = @PlateNumber WHERE [Condition] <> 'Good' AND [MaximumVolumeLoad]<=10\";\nvar cmd = new SqlCommand(sql, Con)\n",
2018-02-13T08:04:05
Vishav Premlall :

var cmd = new SqlCommand(\"UPDATE [Truck Table] SET [TruckID] = @TruckID, [PlateNumber] = @PlateNumber WHERE [Condition] <> 'Good' AND [MaximumVolumeLoad]<10\", Con)\n\n\nYou only needed to remove the equal sign from the second condition in the where clause. Done above for u.",
2018-02-13T08:13:03
Serkan Arslan :

Your code seems ok if it is case-insensitive. \n\n var cmd = new SqlCommand(@\"UPDATE [Truck Table] \n SET [TruckID] = @TruckID, \n [PlateNumber] = @PlateNumber \n WHERE UPPER([Condition]) <> 'GOOD' \n AND [MaximumVolumeLoad]<=10\", Con) \n",
2018-02-13T08:12:22
yy