Sql query to retrieve Comments based on Selected Month and Data

Last post 01-04-2008 7:56 AM by Corwin. 2 replies.

Sort Posts:

  • Sql query to retrieve Comments based on Selected Month and Data

    01-04-2008, 6:13 AM
    • Loading...
    • rahilameen
    • Joined on 07-04-2007, 1:12 PM
    • Posts 12

    hi,

    i am generating report for my project.

    my table goes like

    ID   VASID     VASSID     JanMail   JanVisit    JanPhone    JanComment    FebMail   FebVisit    FebPhone   FebComment................. DecPhone  

    1      25            4              True         False         False            Mail me             False       True           False            Visit me                  

     

    My report has 3 check boxes for Mail,Visit,Phone respectively and a listbox of 12 months january-december respectively.. 

    once user check on Mail and select a month suppose January in my report,the report should generate only January Comments.

    here user can select any number of months.if user selects january,february and march, report should generate respective months Comments.

    Please help me in writing SQL Query to get the comments.......

     

    if any one know the solution for the above problem,Please help me

     

    Thanks & Regards,

     

    Filed under: , , ,
  • Re: Sql query to retrieve Comments based on Selected Month and Data

    01-04-2008, 7:46 AM
    Answer
    • Loading...
    • aadreja
    • Joined on 02-03-2007, 11:36 AM
    • Ahmedabad, India
    • Posts 351

    you can do this using IN clause.

    Create a string of selected month.
    e.g. user have selected Mail and Month Jan, Feb your string goes like this
    "JanMail=true AND FebMail=true"

    if user have selected Phone and Month Jan, Feb your string goes like this
    "JanPhone=true AND JanPhone=true"

    Pass this parameter to your query

    now you query will be like this

    DECLARE @sql varchar(400)
    SET @sql = 'select * from mytable where ' + @criteria
    exec(@sql)

    hope it helps

    Thanks,
    Ritesh

    ---------------------------------------------
    Please do not forget to mark as Answer if my reply is helpful.
  • Re: Sql query to retrieve Comments based on Selected Month and Data

    01-04-2008, 7:56 AM
    Answer
    • Loading...
    • Corwin
    • Joined on 11-30-2007, 10:01 AM
    • Ohio
    • Posts 267

    You may want to look at creating two tables.  One has your ID VASID and VASSID.  The other contains ID, Month, Type and Comment.  This is a one to many relationship.  For the data you showed, you would have records like:

    ID  Jan  Mail  Comment
    ID  Feb  Visit Comment

    Your SQL would look something like:

    SELECT t1.ID, t1.VASID, t1.VASSID, t2.Comment
      FROM table1 t1 INNER JOIN table2 t2 ON t1.ID = t2.ID
     WHERE t2.Month IN (:ParameterMonthList)
       AND t2.Type = :ParameterTypeValue

    Hope this helps.

    - Corwin
    (My Blog - www.ballhead.com/corwin)
Page 1 of 1 (3 items)
Microsoft Communities
Page view counter