Results 1 to 2 of 2

Thread: TSQL help, View

  1. #1
    Join Date
    Aug 2011
    Posts
    1

    Unanswered: TSQL help, View

    Hello,
    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.

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    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.

    Code:
    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
    GO
    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •