SQL麻烦!需要IF / CASE帮助

时间:2018-09-28 11:39:18

标签: sql sql-server select

所以。在表中重新排列列格式以导入到某些软件中。 之前的数据库中有一个性别列,其中包含M,F或U。新软件不会将U用作有效值,因此我需要一种在保持M和F值不变的情况下将其空白的方法。

SELECT Id AS Customerrefid,
       SUBSTRING(Name, CHARINDEX(' ', "Name") + 1, LEN(Name) - (CHARINDEX(' ', "Name") - 1)) AS Surname,
       LEFT("Name", CHARINDEX(' ', "Name")) AS Forename,
       '0' AS Barcode,
       "Address" AS "Address",
       Postcode AS Postcode,
       'UK' AS Countrycode,
       Dob AS Dateofbirth,
       Gender AS Gender,
       ' ' AS "Password",
       Comment AS Notes,
       ' ' AS "Skintype",
       Homephone AS Hometel,
       ' ' AS "Worktel",
       Mobilephone AS Mobiletel,
       Email AS Emailaddress,
       'FALSE' AS Allowemail,
       'FALSE' AS Allowsms,
       'FALSE' AS Agreementsigned,
       ' ' AS Pin
FROM Customer;

select * from customer 

2 个答案:

答案 0 :(得分:2)

您可以使用(case when gender in ('M', 'F') then gender else '' end) as gender 表达式将NULL转换为case s:

U

答案 1 :(得分:1)

我想你想要

-1

#include "cpprest/http_client.h"
#include <string>
using namespace std;
using namespace utility;
using namespace web::http;
using namespace web::http::client;

void main()
{
  http_client wHttpClient(U("https://en.wikipedia.org"));

  http_request wRequest(methods::GET);
  wRequest.set_request_uri(U("w/index.php?title=Barbecue&action=history"));

  http_response wResponse = wHttpClient.request(wRequest).get();

  long l;
  string str;

  while (true)
  {
    l = wResponse.body().read().get();
    if (l == -1) { break; }
    str += static_cast<char>(l);
  }

  cout << str;
}

取决于“空白”是指空字符串还是(case when gender in ('M', 'F') then gender end) as gender 值。