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. pass Comparison Operators as parameter to query in C#

pass Comparison Operators as parameter to query in C#

Scheduled Pinned Locked Moved Database
questioncsharpdatabase
3 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.
  • O Offline
    O Offline
    obarahmeh
    wrote on last edited by
    #1

    Dear All, I have a query that has some parameters: ( SELECT DISTINCT T.TICKET_NUMBER AS TicketNum, A.DESCRIPTION AS Action, O.SYMBOL_CODE AS Symbol, T.TRADE_PRICE AS Price, T.VOLUME_TRADED AS FillVol, T.EXTENDED_PRICE AS TotalValue, T.SUBMITTED_TIME AS ActTime, T.SUBMITTED_DATE AS TransDate FROM TSDETL AS T INNER JOIN TSORDR AS O ON O.SUBMITTED_DATE = T.SUBMITTED_DATE AND O.TICKET_NUMBER = T.TICKET_NUMBER INNER JOIN TSORDA AS A ON O.ORDER_ACTION = A.ACTION_CODE WHERE (T.SUBMITTED_DATE = @SUBMIT_DATE) AND (T.VOLUME_TRADED >= @Volume) AND (T.TICKET_NUMBER IS NOT NULL) AND (O.SYMBOL_CODE= @Symbol) ORDER BY ActTime DESC ) I passed the three parameters (@SUBMIT_DATE, @Volume, @Symbol) successfully, but I want to read the operator (>= or <=) as a parameter...I have a combo box with <= and >=, and I want to pass the value of this combo into the query. How can I do this??? this is because the operator may be >= or <=, and I don't want to write two queries.

    Kind Regards OBarahmeh

    L P 2 Replies Last reply
    0
    • O obarahmeh

      Dear All, I have a query that has some parameters: ( SELECT DISTINCT T.TICKET_NUMBER AS TicketNum, A.DESCRIPTION AS Action, O.SYMBOL_CODE AS Symbol, T.TRADE_PRICE AS Price, T.VOLUME_TRADED AS FillVol, T.EXTENDED_PRICE AS TotalValue, T.SUBMITTED_TIME AS ActTime, T.SUBMITTED_DATE AS TransDate FROM TSDETL AS T INNER JOIN TSORDR AS O ON O.SUBMITTED_DATE = T.SUBMITTED_DATE AND O.TICKET_NUMBER = T.TICKET_NUMBER INNER JOIN TSORDA AS A ON O.ORDER_ACTION = A.ACTION_CODE WHERE (T.SUBMITTED_DATE = @SUBMIT_DATE) AND (T.VOLUME_TRADED >= @Volume) AND (T.TICKET_NUMBER IS NOT NULL) AND (O.SYMBOL_CODE= @Symbol) ORDER BY ActTime DESC ) I passed the three parameters (@SUBMIT_DATE, @Volume, @Symbol) successfully, but I want to read the operator (>= or <=) as a parameter...I have a combo box with <= and >=, and I want to pass the value of this combo into the query. How can I do this??? this is because the operator may be >= or <=, and I don't want to write two queries.

      Kind Regards OBarahmeh

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Answered in the C# forum, please don't cross-post.

      I are troll :)

      1 Reply Last reply
      0
      • O obarahmeh

        Dear All, I have a query that has some parameters: ( SELECT DISTINCT T.TICKET_NUMBER AS TicketNum, A.DESCRIPTION AS Action, O.SYMBOL_CODE AS Symbol, T.TRADE_PRICE AS Price, T.VOLUME_TRADED AS FillVol, T.EXTENDED_PRICE AS TotalValue, T.SUBMITTED_TIME AS ActTime, T.SUBMITTED_DATE AS TransDate FROM TSDETL AS T INNER JOIN TSORDR AS O ON O.SUBMITTED_DATE = T.SUBMITTED_DATE AND O.TICKET_NUMBER = T.TICKET_NUMBER INNER JOIN TSORDA AS A ON O.ORDER_ACTION = A.ACTION_CODE WHERE (T.SUBMITTED_DATE = @SUBMIT_DATE) AND (T.VOLUME_TRADED >= @Volume) AND (T.TICKET_NUMBER IS NOT NULL) AND (O.SYMBOL_CODE= @Symbol) ORDER BY ActTime DESC ) I passed the three parameters (@SUBMIT_DATE, @Volume, @Symbol) successfully, but I want to read the operator (>= or <=) as a parameter...I have a combo box with <= and >=, and I want to pass the value of this combo into the query. How can I do this??? this is because the operator may be >= or <=, and I don't want to write two queries.

        Kind Regards OBarahmeh

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Would you like an ugly option? How about:

        CREATE FUNCTION DoFunction
        (
        @Op1 BIGINT
        ,
        @Oper NVARCHAR(2)
        ,
        @Op2 BIGINT
        )
        RETURNS BIT
        AS
        BEGIN
        IF ( @Oper = '>=' AND @Op1 >= @Op2 ) RETURN 1

        ELSE IF ( @Oper = '<=' AND @Op1 <= @Op2 ) RETURN 1
        

        -- More

        RETURN 0
        

        END

        select * from test where dbo.dofunction ( VOLUME_TRADED , '<=' , @Volume )=1

        Otherwise, I might suggest:

        SELECT * FROM test WHERE CASE WHEN @Oper='<=' AND VOLUME_TRADED <= @Volume THEN 1 WHEN @Oper='>=' AND VOLUME_TRADED >= @Volume THEN 1 ELSE 0 END = 1

        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