简体   繁体   English

如果字段不为空,则分配一个值

[英]Assign a value if the field is not null

I'm working on a SQL project on MS Access.我正在 MS Access 上进行 SQL 项目。 I would like to know if there is a way to assign a same value everytime a field is NOT NULL.我想知道是否有办法在每次字段非空时分配相同的值。 I know that there is the Nz() function which does the opposite, but I don't know other functions.我知道有 Nz() 函数的作用相反,但我不知道其他函数。 Also, I would like to put a different value everytime the field is NULL另外,我想在每次字段为 NULL 时输入不同的值

My table looks like that.我的桌子看起来像那样。

date


MARCH17
JUNE18

JULY19

and I would like to get something like that.我想得到类似的东西。

date
1
2
PRESENT
PRESENT
5
PRESENT

If I have to create another column, it's perfectly fine too.如果我必须创建另一列,也完全没问题。

Thanks in advance !提前致谢 !

You will need to place your new information in a new column, otherwise, if you run the query more than once, you will get PRESENT for everything since the first query replaces the NULL date with a sequence number.您需要将新信息放在新列中,否则,如果您多次运行查询,您将获得所有内容的PRESENT ,因为第一个查询用序列号替换了NULL date

If you have an id column you can use:如果您有一个id列,您可以使用:

UPDATE table SET new_column = (SELECT IIF(date IS NULL, id,'PRESENT'))

If you don't have an id column (which is strongly recommended) then you'll need to generate a sequence number.如果您没有id列(强烈推荐),那么您需要生成一个序列号。

Does your table have a Primary Key?你的表有主键吗? Then you want to count all nulls where primary key is less than this one to give you your number, and put present where it isn't null.然后你想计算主键小于这个的所有空值来给你你的数字,并把它放在不为空的地方。 So (assuming your field is F1 and Primary Key is called PK) the following calculated field因此(假设您的字段是 F1 并且主键称为 PK)以下计算字段

=IIf(ISNULL([F1]),DCOUNT("[PK]","MYTABLE","[PK]<" & [PK]) &""","PRESENT") =IIf(ISNULL([F1]),DCOUNT("[PK]","MYTABLE","[PK]<" & [PK]) &""","PRESENT")

You can use:您可以使用:

UPDATE 
    YourTable 
SET 
    [date] = IIf([date] Is Null, 
        (Select Count(*) + 1 From YourTable As T Where T.[date] <> 'PRESENT'),
        'PRESENT'))
WHERE 
    [date] <> 'PRESENT'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM