| Author | 
                                
                                 Topic  | 
                             
                            
                                    | 
                                         dr223 
                                        Constraint Violating Yak Guru 
                                         
                                        
                                        444 Posts  | 
                                        
                                        
                                            
                                            
                                             Posted - 2015-04-05 : 15:47:24
                                            
  | 
                                             
                                            
                                            Hi, I have 2 tables TableA and TableBTableAMedicalCodeID    EmisCode     ReadCode    1                EMISATT      NULL          2                NULL         PCSDT         3                TFHG         Hgi           4                YUGH         NULL           TableBDiaryID          EmisCode     ReadCode    1                EMISATT_AB     NULL        2                EMISATT_C      NULL        3                TFHG           Hgi         4                YUGH           NULL         Results I want to receive; DiaryID          MedicalCodeID 1                1                   2                1      3                3           4                4            Therefore, the idea is if the emiscode = EMISATT% on TableB take medicalcodeID = 1, otherwise, take the exact match.Code of the JOIN I have now is as shown (this code takes the exact match of the emiscode) but I want to update it to obtain the above results - where it will pull the EMISATT if the first 7 characters start with EMISATT, otherwise do exact matchISNULL(TableB.EmisCode, N'(novalue)') = ISNULL(TableA.Emiscode, N'(novalue)') Thank you so much | 
                                             
                                         
                                     | 
                             
       
                            
                       
                          
                            
                                    | 
                                     dr223 
                                    Constraint Violating Yak Guru 
                                     
                                    
                                    444 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2015-04-05 : 17:42:58
                                          
  | 
                                         
                                        
                                          Tried; ISNULL(                CASE WHEN LEFT (TableB.EmisCode,7) = 'EMISATT' THEN 'EMISATT'  ELSE TableB.EmisCode, N'(novalue)')           = ISNULL(TableA.Emiscode, N'(novalue)')  I receive the error- Incorrect syntax near ','.Please Help  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                       
                          
                            
                                    | 
                                     SwePeso 
                                    Patron Saint of Lost Yaks 
                                     
                                    
                                    30421 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2015-04-06 : 03:36:41
                                          
  | 
                                         
                                        
                                          [code]declare @TableA table (MedicalCodeID int, EmisCode varchar(20), ReadCode varchar(20));insert @tableA values(1, 'EMISATT', NULL), (2, NULL, 'PCSDT'), (3, 'TFHG', 'Hgi'), (4, 'YUGH', NULL);declare @TableB table (DiaryID int, EmisCode varchar(20), ReadCode varchar(20));insert @tableB values(1, 'EMISATT_AB', NULL), (2, 'EMISATT_C', NULL), (3, 'TFHG', 'Hgi'), (4, 'YUGH', NULL);-- SwePesoSELECT		b.DiaryID,		a.MedicalCodeIDFROM		@TableA AS aINNER JOIN	@TableB AS b ON b.EmisCode LIKE a.EmisCode + '%';[/code] Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                            
                                | 
                                    
                                      
                                     
                                    
                                 | 
                             
                         
                     | 
                 
             
         |