Skip to main content

Swapping a column value of a table in a single query and not using temp table variable

There are some cases when your guess is right but for the lack of your confidence you have to leave some questions or facts.This doesn't mean that in such cases you need to deliver what you are guessing is good enough to show that you are confident rather not answering is sometime cuts better for those who respect themselves in such a way that "Talk less but always right".But If you are hungry for marks and there is no negative marking then proceed :)

Q:Given a table of following structure and data you have to swap('M' to 'F' and 'F' to 'M') column value for column 'SEX' in a single query and you cannot use temp table.

ID | SEX | Salary
------------------------
1 M   45345.00
2 F   75345.00
3 M   64644.00
4 F   5555.00
5 M   34434.00
6 M   84434.00


The query is so simple

UPDATE employee SET sex= CASE sex WHEN 'M' THEN 'F'  WHEN 'F' THEN 'M' END


That's it !


  

Comments

Popular posts from this blog

Cannot access a disposed object. A common cause of this error is disposing a context that was

  Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'PropertyContext'.  public class CustomerHub : Hub     {         public readonly ICustomerService _customerService;         private readonly IServiceScopeFactory _serviceScopeFactory;         public CustomerHub(ICustomerService service, IServiceScopeFactory serviceScopeFactory)         {             _customerService = service;             _serviceScopeFactory = serviceSco...

Limit Upload File Type Extensions ASP.NET MVC 5

  //-----------------------------------------------------------------------    // <copyright file="AllowExtensionsAttribute.cs" company="None">    //     Copyright (c) Allow to distribute this code and utilize this code for personal or commercial purpose.    // </copyright>    // <author>Asma Khalid</author>    //-----------------------------------------------------------------------       namespace  ImgExtLimit.Helper_Code.Common   {        using  System;        using  System.Collections.Generic;        using  System.ComponentModel.DataAnnotations;        using  System.Linq;      ...