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. Doubt in query.........,

Doubt in query.........,

Scheduled Pinned Locked Moved Database
databasetutorialquestion
12 Posts 6 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.
  • M Member 3879881

    Hi i have table like this format: Id sMonth sYear value1 608 6 2007 100 608 7 2007 200 608 8 2007 150 608 6 2008 652 608 7 2008 120 608 8 2008 234 Suppose i need the records between the 6th month 2007 to 6th month 2008 O/P: Id sMonth sYear value1 608 6 2007 100 608 7 2007 200 608 8 2007 150 608 6 2008 652 How to write the query???, If its in date format we can use between condition..., But in this case how u can compare the months and years ???,

    Regards, Magi

    A Offline
    A Offline
    andyharman
    wrote on last edited by
    #2

    How about

    select * from MyTable
       where (sYear > 2007 or (sYear = 2007 and sMonth >= 6))
       and (sYear < 2008 or (sYear = 2008 and sMonth <= 6))
    

    or

    select * from MyTable
       where convert(varchar(6), (Year*100)+sMonth) between '200706' and '200806'
    

    Regards Andy -- modified at 14:19 Wednesday 18th July, 2007

    If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message".

    M R C 4 Replies Last reply
    0
    • A andyharman

      How about

      select * from MyTable
         where (sYear > 2007 or (sYear = 2007 and sMonth >= 6))
         and (sYear < 2008 or (sYear = 2008 and sMonth <= 6))
      

      or

      select * from MyTable
         where convert(varchar(6), (Year*100)+sMonth) between '200706' and '200806'
      

      Regards Andy -- modified at 14:19 Wednesday 18th July, 2007

      If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message".

      M Offline
      M Offline
      Member 3879881
      wrote on last edited by
      #3

      Hi andy, Thank u for ur reply..., Ya i getting o/p for the first query..., But second query is not giving proper o/p..., Anyway one solution is enough..., Thank's lot..., keep in touch,

      Regards, Magi

      A 1 Reply Last reply
      0
      • A andyharman

        How about

        select * from MyTable
           where (sYear > 2007 or (sYear = 2007 and sMonth >= 6))
           and (sYear < 2008 or (sYear = 2008 and sMonth <= 6))
        

        or

        select * from MyTable
           where convert(varchar(6), (Year*100)+sMonth) between '200706' and '200806'
        

        Regards Andy -- modified at 14:19 Wednesday 18th July, 2007

        If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message".

        R Offline
        R Offline
        RepliCrux
        wrote on last edited by
        #4

        5 votes to you man. I was about to say bad database design ;P

        1 Reply Last reply
        0
        • M Member 3879881

          Hi i have table like this format: Id sMonth sYear value1 608 6 2007 100 608 7 2007 200 608 8 2007 150 608 6 2008 652 608 7 2008 120 608 8 2008 234 Suppose i need the records between the 6th month 2007 to 6th month 2008 O/P: Id sMonth sYear value1 608 6 2007 100 608 7 2007 200 608 8 2007 150 608 6 2008 652 How to write the query???, If its in date format we can use between condition..., But in this case how u can compare the months and years ???,

          Regards, Magi

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #5

          writing query for your requirement will be bit tough. Why don't you use date fields rather than storing date like this. In date fields you can use > or < operators to get the dates in between two dates.


          My Website | Ask smart questions

          M 1 Reply Last reply
          0
          • N N a v a n e e t h

            writing query for your requirement will be bit tough. Why don't you use date fields rather than storing date like this. In date fields you can use > or < operators to get the dates in between two dates.


            My Website | Ask smart questions

            M Offline
            M Offline
            Member 3879881
            wrote on last edited by
            #6

            Thanks for ur reply navaneeth..., s navaneeth, i amn't getting date format from my end user..., Just i am getting month and year only..., So cant say in dateformat in my table..., But any way thanks for ur suggestion..., See andy's first query is giving solution for this probs..., Keep in touch, Friendly,

            Regards, Magi

            1 Reply Last reply
            0
            • M Member 3879881

              Hi andy, Thank u for ur reply..., Ya i getting o/p for the first query..., But second query is not giving proper o/p..., Anyway one solution is enough..., Thank's lot..., keep in touch,

              Regards, Magi

              A Offline
              A Offline
              andyharman
              wrote on last edited by
              #7

              You're welcome. The second query should have used (sYear*100). I hadn't woken-up properly:)

              1 Reply Last reply
              0
              • M Member 3879881

                Hi i have table like this format: Id sMonth sYear value1 608 6 2007 100 608 7 2007 200 608 8 2007 150 608 6 2008 652 608 7 2008 120 608 8 2008 234 Suppose i need the records between the 6th month 2007 to 6th month 2008 O/P: Id sMonth sYear value1 608 6 2007 100 608 7 2007 200 608 8 2007 150 608 6 2008 652 How to write the query???, If its in date format we can use between condition..., But in this case how u can compare the months and years ???,

                Regards, Magi

                K Offline
                K Offline
                Krish KP
                wrote on last edited by
                #8

                SELECT * FROM TblA WHERE Convert(Datetime, '01/'+sMonth+'/'+sYear, 103) BETWEEN '01-Jun-2007' AND '30-Jun-2008'

                Regards KP

                R 1 Reply Last reply
                0
                • A andyharman

                  How about

                  select * from MyTable
                     where (sYear > 2007 or (sYear = 2007 and sMonth >= 6))
                     and (sYear < 2008 or (sYear = 2008 and sMonth <= 6))
                  

                  or

                  select * from MyTable
                     where convert(varchar(6), (Year*100)+sMonth) between '200706' and '200806'
                  

                  Regards Andy -- modified at 14:19 Wednesday 18th July, 2007

                  If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message".

                  C Offline
                  C Offline
                  ChandraRam
                  wrote on last edited by
                  #9

                  andyharman wrote:

                  select * from MyTable where convert(varchar(6), (Year*12)+sMonth) between '200706' and '200806'

                  Actually, (Year*12)+sMonth wont get you those values at all ;)

                  1 Reply Last reply
                  0
                  • A andyharman

                    How about

                    select * from MyTable
                       where (sYear > 2007 or (sYear = 2007 and sMonth >= 6))
                       and (sYear < 2008 or (sYear = 2008 and sMonth <= 6))
                    

                    or

                    select * from MyTable
                       where convert(varchar(6), (Year*100)+sMonth) between '200706' and '200806'
                    

                    Regards Andy -- modified at 14:19 Wednesday 18th July, 2007

                    If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message".

                    C Offline
                    C Offline
                    ChandraRam
                    wrote on last edited by
                    #10

                    And looks like I wasn't reading properly either, before posting my message... pl ignore - I saw your clarification after I had hit the submit.

                    1 Reply Last reply
                    0
                    • K Krish KP

                      SELECT * FROM TblA WHERE Convert(Datetime, '01/'+sMonth+'/'+sYear, 103) BETWEEN '01-Jun-2007' AND '30-Jun-2008'

                      Regards KP

                      R Offline
                      R Offline
                      RepliCrux
                      wrote on last edited by
                      #11

                      You are right, the only thing is not all months have last day as 30, it can be 30, 28, 31... but well this is the right way to do anyway, there is a store procedure which gets the last day of the month in a particular year, can always use that.

                      K 1 Reply Last reply
                      0
                      • R RepliCrux

                        You are right, the only thing is not all months have last day as 30, it can be 30, 28, 31... but well this is the right way to do anyway, there is a store procedure which gets the last day of the month in a particular year, can always use that.

                        K Offline
                        K Offline
                        Krish KP
                        wrote on last edited by
                        #12

                        given Month & Year are constant (june, 2007, 2008) Also the date which is derived is always 1st.

                        Regards KP

                        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