Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. change the predicate to use "CASE"

change the predicate to use "CASE"

Scheduled Pinned Locked Moved Database
databasetutorial
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    angelagke
    wrote on last edited by
    #1

    how to change SQL statement below to use CASE instant of "AND" "OR" after condition where :- ALTER PROCEDURE dbo.authorInfor @au_lname varchar(40) AS select title, price, Name = substring (au_lname, 1,40), type from authors a, titles t, titleauthor ta where a.au_id = ta.au_id AND t.title_id = ta.title_id AND(au_lname = @au_lname OR au_lname = '' OR au_lname is Null) Thanks. Angela

    F E 2 Replies Last reply
    0
    • A angelagke

      how to change SQL statement below to use CASE instant of "AND" "OR" after condition where :- ALTER PROCEDURE dbo.authorInfor @au_lname varchar(40) AS select title, price, Name = substring (au_lname, 1,40), type from authors a, titles t, titleauthor ta where a.au_id = ta.au_id AND t.title_id = ta.title_id AND(au_lname = @au_lname OR au_lname = '' OR au_lname is Null) Thanks. Angela

      F Offline
      F Offline
      Frank Kerrigan
      wrote on last edited by
      #2

      If AND OR works use it; its must more efficient than using case. Look where you want to go not where you don't want to crash. Bikers Bible

      A 1 Reply Last reply
      0
      • A angelagke

        how to change SQL statement below to use CASE instant of "AND" "OR" after condition where :- ALTER PROCEDURE dbo.authorInfor @au_lname varchar(40) AS select title, price, Name = substring (au_lname, 1,40), type from authors a, titles t, titleauthor ta where a.au_id = ta.au_id AND t.title_id = ta.title_id AND(au_lname = @au_lname OR au_lname = '' OR au_lname is Null) Thanks. Angela

        E Offline
        E Offline
        Eric Dahlvang
        wrote on last edited by
        #3

        CASE is generally used in instances where you want a different field value returned based on specified conditions. Your select statement is only concerned with returning all fields based on certain conditions. In other words, CASE wouldn't be used in the WHERE clause of an sql statement such as yours. You could clean this up a little though, like this: select title, price, Name = substring (au_lname, 1,40), type from authors a inner join titleauthor ta on a.au_id = ta.au_id inner join titles t on ta.title_id = t.title_id where au_lname = @au_lname OR isnull(au_lname,'') = '' Or your where clause could be like this: where isnull(au_lname,'') IN (@au_lname,'') Other than that, nothing comes to my mind. ---------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

        A 2 Replies Last reply
        0
        • F Frank Kerrigan

          If AND OR works use it; its must more efficient than using case. Look where you want to go not where you don't want to crash. Bikers Bible

          A Offline
          A Offline
          angelagke
          wrote on last edited by
          #4

          really can works and get the result that what i want with my previous SQL statement... the matter is my boss want me to change the predicate to use "Case" :doh: anyway thanks for your suggestion;) angela

          1 Reply Last reply
          0
          • E Eric Dahlvang

            CASE is generally used in instances where you want a different field value returned based on specified conditions. Your select statement is only concerned with returning all fields based on certain conditions. In other words, CASE wouldn't be used in the WHERE clause of an sql statement such as yours. You could clean this up a little though, like this: select title, price, Name = substring (au_lname, 1,40), type from authors a inner join titleauthor ta on a.au_id = ta.au_id inner join titles t on ta.title_id = t.title_id where au_lname = @au_lname OR isnull(au_lname,'') = '' Or your where clause could be like this: where isnull(au_lname,'') IN (@au_lname,'') Other than that, nothing comes to my mind. ---------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

            A Offline
            A Offline
            angelagke
            wrote on last edited by
            #5

            no wonder i can't get what i want if i change the predicate to use "CASE" in my SQL statement :| can see that your SQL statement is more simplify and better than mine ;) but 1 of my condition is when user do not insert any value for author's name, all author's name record will display in my result.. if using your SQL statement above, if i insert null for my author's name, then the result is empty..

            1 Reply Last reply
            0
            • E Eric Dahlvang

              CASE is generally used in instances where you want a different field value returned based on specified conditions. Your select statement is only concerned with returning all fields based on certain conditions. In other words, CASE wouldn't be used in the WHERE clause of an sql statement such as yours. You could clean this up a little though, like this: select title, price, Name = substring (au_lname, 1,40), type from authors a inner join titleauthor ta on a.au_id = ta.au_id inner join titles t on ta.title_id = t.title_id where au_lname = @au_lname OR isnull(au_lname,'') = '' Or your where clause could be like this: where isnull(au_lname,'') IN (@au_lname,'') Other than that, nothing comes to my mind. ---------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

              A Offline
              A Offline
              angelagke
              wrote on last edited by
              #6

              I get a SQL statement that can use CASE under my condition Declare @au_lname char(30) Select @au_lname = '' select title, price, Name = substring (au_lname, 1,40), type from titleauthor ta JOIN authors a on ta.au_id = a.au_id JOIN titles t on ta.title_id = t.title_id Where 1 = Case When RTRIM(@au_lname) = '' Then 1 When RTRIM(@au_lname) <> '' And RTRIM(UPPER(@au_lname)) = RTRIM(UPPER(au_lname)) Then 1 Else 0 End it's cool ;) angela

              E 1 Reply Last reply
              0
              • A angelagke

                I get a SQL statement that can use CASE under my condition Declare @au_lname char(30) Select @au_lname = '' select title, price, Name = substring (au_lname, 1,40), type from titleauthor ta JOIN authors a on ta.au_id = a.au_id JOIN titles t on ta.title_id = t.title_id Where 1 = Case When RTRIM(@au_lname) = '' Then 1 When RTRIM(@au_lname) <> '' And RTRIM(UPPER(@au_lname)) = RTRIM(UPPER(au_lname)) Then 1 Else 0 End it's cool ;) angela

                E Offline
                E Offline
                Eric Dahlvang
                wrote on last edited by
                #7

                I see. Thank you for posting your solution. ---------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups