Home > database >  Conditions of SQL access for help
Conditions of SQL access for help

Time:10-02


Scenario: you want the contract unit is empty, take GangDian, the name of the secondary unit is empty, take the name of the contract unit, t1 is empty, the secondary unit name, all the make up blank, SQL to achieve, excuse me,, try the case when if not,,,

CodePudding user response:

Case the when can definitely! Please see the first case the when writing
 
Select
Case
When [the contract unit]=' 'then [GangDian]
The when secondary unit []=' 'then [the contract unit]
When [t1]=' 'then secondary unit []
SQL conditions did not write all else ' '
End
The from tableName

CodePudding user response:

The meaning of the original poster is supposed to be like this?

The create table test2
(id int identity,
[the contract unit] nvarchar (50),
[GangDian] nvarchar (50),
Secondary unit [] nvarchar (50),
[t1] nvarchar (50)
)


- scene: want to contract unit is empty, take GangDian, the name of the secondary unit is empty, take the name of the contract unit, t1 is empty, the secondary unit name, all the make up blank, SQL to achieve, excuse me,, tried a case when if not,,
The delete test2
Insert into test2 ([contract unit], [GangDian], [secondary units], [t1])
Values (N '[the contract unit]', N '[GangDian]', N '/secondary units', N' [t1])

Insert into test2 ([contract unit], [GangDian], [secondary units], [t1])
Values (null, N '[GangDian]', N 'secondary unit [], null)

Select id, [GangDian], COALESCE ([contract unit], [GangDian])] [the contract unit, COALESCE (secondary unit [], [the contract unit]) secondary unit [], COALESCE ([t1], [secondary units]) [t1]
The from test2
  • Related