Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
HiI have a SQL Server 2005 table as followsRecNum varchar(6) main varchar(1) f1 varchar(1)...f8 varchar(8)main can contain 'X', '1'...'8' or nullf1 can only contain '1' or nullf2 can only contain '2' or nulletcIf main contains '3' then there should be a corresponding '3' in the f3 field,If main contains '7' then there should be a corresponding '7' in the f7 field.etcI want to list all the records where there is not a corresponding 'f'record but excluding recs where main is 'X' or nullI've tried a few things with CASE's but can't seem to come up with something that works.Any help appreciated.TIAAlan
bitsmed
Aged Yak Warrior
545 Posts
Posted - 2014-06-26 : 15:02:47
Try this:
select * from yourtable where not (main is null or main='X' or (main='1' and main=f1) or (main='2' and main=f2) or (main='3' and main=f3) or (main='4' and main=f4) or (main='5' and main=f5) or (main='6' and main=f6) or (main='7' and main=f7) or (main='8' and main=f8) )
Magua1
Starting Member
15 Posts
Posted - 2014-06-26 : 15:31:30
Thanks for you helpAlan
tkizer
Almighty SQL Goddess
38200 Posts
Posted - 2014-06-26 : 16:05:46
This is screaming for a database design change.Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/
SwePeso
Patron Saint of Lost Yaks
30421 Posts
Posted - 2014-06-27 : 08:47:27
Excluding NULL or X in Main column?
SELECT *FROM dbo.TableWHERE Main NOT IN ('X', F1, F2, F3, F4, F5, F6, F7, F8)
Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA