Thread: TSQL help, View
08-12-11, 14:43 #1Registered User
- Join Date
- Aug 2011
Unanswered: TSQL help, View
I'm very new to having to create my own views n a SQL db to support reports (I usually just make the reports), and I would really appreciate any help you could give me to help with the following.
There are three fields that I need to report from where the data can come in with different configurations. What I need to do is say when there is Value "Fred" in any ColumnA record, Make a new Column called "Activity" and insert the value "Fred" there. Whenever "Fred" isn't a Value in Column A, insert the value from Column C into the new "Activity" column instead.
Case WHEN ColumnA LIKE N'%Fred%' THEN 'Fred' ELSE "take value from ColumnC and place it into new Activity Column' END AS Activity
Any help on how to get this to work would be greatly appreciated.
08-12-11, 15:01 #2Resident Curmudgeon
Provided Answers: 54
- Join Date
- Feb 2004
- In front of the computer
Transact-SQL is the name applied to the SQL dialect used by Microsoft as well as the dialect used by Sybase. For the purposes of your question, the two dialects are compatible. I'm going to guess that you're using Microsoft SQL Server and will move the thread to the appropriate forum... If I guessed wrong, let me know and I'll move it appropriately.
CREATE TABLE ginaSQL ( ColumnA VARCHAR(50) NULL , ColumnB INT IDENTITY , ColumnC VARCHAR(50) NOT NULL ) GO INSERT INTO ginaSQL (ColumnA, ColumnC) SELECT 'Bob', 'Carol' UNION ALL SELECT 'Ted', 'Alice' UNION ALL SELECT 'Fredrick', 'Ferdinanda' UNION ALL SELECT 'Ann', 'Mary' UNION ALL SELECT 'Fred', 'Wilma' UNION ALL SELECT 'Barney', 'Betty' GO SELECT ColumnB , CASE WHEN ColumnA LIKE 'Fred%' THEN 'Fred' ELSE ColumnC END AS Derived FROM ginaSQL GOIn theory, theory and practice are identical. In practice, theory and practice are unrelated.