Ai-je besoin de réécrire la déclaration de cas pour chaque domaine?

La condition de cas pour deux colonnes est la même. Dans l'instruction ci-dessous, j'utilise ceci deux fois mais pour une colonne différente, y a-t-il un autre moyen de ne pas répéter la condition deux fois?

case [CPHIL_AWD_CD] when ' ' then 'Not Applicable/ Not a Doctoral Student' when 'X' then 'Not Applicable/ Not a Doctoral Student' when 'N' then 'NO' when 'Y' then 'YES' end as CPHIL_AWD_CD ,case [FINL_ORAL_REQ_CD] when ' ' then 'Not Applicable/ Not a Doctoral Student' when 'X' then 'Not Applicable/ Not a Doctoral Student' when 'N' then 'NO' when 'Y' then 'YES' end as FINL_ORAL_REQ_CD 

Une variante de la réponse de thepirat000:

 -- Sample data. declare @Samples as Table ( Frisbee Int Identity Primary Key, Code1 Char(1), Code2 Char(2) ); insert into @Samples values ( 'Y', 'N' ), ( ' ', 'Y' ), ( 'N', 'X' ); select * from @Samples; -- Handle the lookup. with Lookup as ( select * from ( values ( ' ', 'Not Applicable/ Not a Doctoral Student' ), ( 'X', 'Not Applicable/ Not a Doctoral Student' ), ( 'N', 'No' ), ( 'Y', 'Yes' ) ) as TableName( Code, Description ) ) select S.Code1, L1.Description, S.Code2, L2.Description from @Samples as S inner join Lookup as L1 on L1.Code = S.Code1 inner join Lookup as L2 on L2.Code = S.Code2; 

La table de search est créée dans un CTE et référencée comme nécessaire pour plusieurs colonnes.

Mise à jour: La variable table est maintenant dotée d'une key primaire pour une raison inexplicable. Si quelqu'un peut réellement expliquer comment cela va bénéficier de la performance, j'aimerais l'entendre. Ce n'est pas évident à partir du plan d'exécution.

Il suffit de créer une table (temp?) Avec le mapping

 CREATE TABLE [Constants] ( [ID] nvarchar(1) PRIMARY KEY, [Text] nvarchar(max) ) INSERT INTO [Constants] VALUES (' ', 'Not Applicable/ Not a Doctoral Student') INSERT INTO [Constants] VALUES ('X', 'Not Applicable/ Not a Doctoral Student') INSERT INTO [Constants] VALUES ('N', 'No') INSERT INTO [Constants] VALUES ('Y', 'Yes') 

et effectuer une jointure interne

 SELECT C1.Text AS CPHIL_AWD_CD, C2.Text AS FINL_ORAL_REQ_CD, ... FROM YourTable T INNER JOIN Constants C1 ON C1.ID = T.CPHIL_AWD_CD INNER JOIN Constants C2 ON C2.ID = T.FINL_ORAL_REQ_CD 

Selon la suggestion de Dijkgraaf, voici la solution:

Créez une fonction avec la logique et appelez cela dans l'instruction select:

 CREATE FUNCTION dbo.GetCaseValue (@val varchar(50)) RETURNS varchar(50) WITH EXECUTE AS CALLER AS BEGIN return (select case @val when ' ' then 'Not Applicable/ Not a Doctoral Student' when 'X' then 'Not Applicable/ Not a Doctoral Student' when 'N' then 'NO' when 'Y' then 'YES' end) END 

Et appelez-le comme:

 select dbo.GetCaseValue([CPHIL_AWD_CD]) as 'CPHIL_AWD_CD', dbo.GetCaseValue([FINL_ORAL_REQ_CD]) as 'FINL_ORAL_REQ_CD' 

Pas vraiment. Ce que vous faites est, à ma connaissance, correct.

Vous POUVEZ faire une fonction définie par l'user comme ci-dessous, mais les avantages sont .. subjectifs.

 CREATE FUNCTION dbo.ufnMungeIt ( @In NVARCHAR(1) ) RETURNS NVARCHAR(255) BEGIN RETURN CASE @In when ' ' then 'Not Applicable/ Not a Doctoral Student' when 'X' then 'Not Applicable/ Not a Doctoral Student' when 'N' then 'NO' when 'Y' then 'YES' END END -- SELECT dbo.ufnMungeIt([CPHIL_AWD_CD]) AS CPHIL_AWD_CD, dbo.ufnMungeIt([FINL_ORAL_REQ_CD]) AS FINL_ORAL_REQ_CD