0

我有这个 SQL 查询:

DECLARE @table TABLE (Gender VARCHAR(15))
INSERT INTO @table
VALUES 
    ('F'), 
    ('AAA'), 
    ('M'), 
    ('null'), 
    ('NULL')

如何编写标量函数来清理此 Gender 列,插入 F 或 M 代替“AAA”或那些空值。

4

2 回答 2

0
create yourTable
(Gender bit not null)

位只有01

select 
    case gender 
    when 1 then 'F'
    when 0 then 'M'
    end as gender
from yourTable

或在表中使用 char(1)

于 2013-09-02T07:21:17.773 回答
0

您可以从这个开始,但是您确定它应该是 M 还是 F 的条件是什么?

UPDATE yourTable
SET gender = 
             CASE
                 WHEN -- your condition where you determine if it should be M -- 
                      THEN 'M'
                 ELSE 'F'
             END
WHERE LOWER(gender) NOT IN ('m', 'f')
于 2013-09-02T07:07:19.393 回答