简体   繁体   中英

Assign a value if the field is not null

I'm working on a SQL project on MS Access. 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. Also, I would like to put a different value everytime the field is 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.

If you have an id column you can use:

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.

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

=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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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