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. select upcoming birthday?

select upcoming birthday?

Scheduled Pinned Locked Moved Database
databasesql-serversysadmintutorialquestion
14 Posts 5 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 Meax

    i have users Date Of Birth as DOB in sql server table called UsersProfile. Person DOB a 3/13/2009 b 3/13/2009 c 3/25/2009 d 4/2/2009 e 4/15/2009 How to select next(upcoming) birthday from this table? if two persons bday falls on a same date, like Person a and Person b, how to select those two person?

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #4

    Meax wrote:

    i have users Date Of Birth

    what is it these unborn people are using? :doh:

    Luc Pattyn [Forum Guidelines] [My Articles]


    - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


    M 1 Reply Last reply
    0
    • L Luc Pattyn

      Meax wrote:

      i have users Date Of Birth

      what is it these unborn people are using? :doh:

      Luc Pattyn [Forum Guidelines] [My Articles]


      - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


      M Offline
      M Offline
      Meax
      wrote on last edited by
      #5

      sorry, meant to write 1980

      1 Reply Last reply
      0
      • M Meax

        i have users Date Of Birth as DOB in sql server table called UsersProfile. Person DOB a 3/13/2009 b 3/13/2009 c 3/25/2009 d 4/2/2009 e 4/15/2009 How to select next(upcoming) birthday from this table? if two persons bday falls on a same date, like Person a and Person b, how to select those two person?

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #6

        SELECT TOP 1 * FROM UsersProfile WHERE datepart(dy,DOB)>=datepart(dy,now()) ORDER BY datepart(dy,DOB) is what I would try to get the next one. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


        M 1 Reply Last reply
        0
        • L Luc Pattyn

          SELECT TOP 1 * FROM UsersProfile WHERE datepart(dy,DOB)>=datepart(dy,now()) ORDER BY datepart(dy,DOB) is what I would try to get the next one. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


          M Offline
          M Offline
          Meax
          wrote on last edited by
          #7

          what if 2 people has same birthday? like, Person DOB a 3/13/1980 b 3/13/1980 how to do it without writing TOP 2

          L 1 Reply Last reply
          0
          • M Meax

            what if 2 people has same birthday? like, Person DOB a 3/13/1980 b 3/13/1980 how to do it without writing TOP 2

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #8

            Hi, Since the first reply suggested something like Select Person from UsersProfile where DOB = ( Select min(DOB) from UsersProfile where DOB > GetDate() ) it must be rather easy to get it like so: Select Person from UsersProfile where DOB = ( whatever it takes to obtain the next birthday ) So I would try: Select Person from UsersProfile where DOB = ( SELECT min(datepart(dy,DOB)) FROM UsersProfile WHERE datepart(dy,DOB)>=datepart(dy, now())) BTW: This is PHP/MySQL code that works for me:

            $Query="SELECT \* FROM $table WHERE DAYOFYEAR(birthday)= ".
            	"(SELECT min(DAYOFYEAR(birthday)) FROM $table ".
            	"WHERE DAYOFYEAR(birthday)>=DAYOFYEAR(now()))";
            

            :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


            M 1 Reply Last reply
            0
            • M Meax

              thnaks for replying, however i am not getting any data with those quaries

              S Offline
              S Offline
              Syed Mehroz Alam
              wrote on last edited by
              #9

              Strange.. It works for me. Here are my test statements:

              declare @UsersProfile table
              (
              Person varchar(20),
              DOB datetime
              )

              insert into @UsersProfile
              select 'Person1', '2009-03-11'
              union select 'Person2', '2009-03-13'
              union select 'Person3', '2009-03-13'
              union select 'Person4', '2009-03-16'
              union select 'Person5', '2009-03-25'

              select * from @UsersProfile
              where DOB = ( select min(DOB) from @UsersProfile where DOB>GetDate() )

              Are you sure your DOB column is of DateTime type? Regards,

              My Blog My Articles Computers are incredibly fast, accurate, and stupid; humans are incredibly slow, inaccurate and brilliant; together they are powerful beyond imagination. - Albert Einstein

              1 Reply Last reply
              0
              • L Luc Pattyn

                Hi, Since the first reply suggested something like Select Person from UsersProfile where DOB = ( Select min(DOB) from UsersProfile where DOB > GetDate() ) it must be rather easy to get it like so: Select Person from UsersProfile where DOB = ( whatever it takes to obtain the next birthday ) So I would try: Select Person from UsersProfile where DOB = ( SELECT min(datepart(dy,DOB)) FROM UsersProfile WHERE datepart(dy,DOB)>=datepart(dy, now())) BTW: This is PHP/MySQL code that works for me:

                $Query="SELECT \* FROM $table WHERE DAYOFYEAR(birthday)= ".
                	"(SELECT min(DAYOFYEAR(birthday)) FROM $table ".
                	"WHERE DAYOFYEAR(birthday)>=DAYOFYEAR(now()))";
                

                :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                M Offline
                M Offline
                Meax
                wrote on last edited by
                #10

                How to make this work in sql server "SELECT * FROM $table WHERE DAYOFYEAR(birthday)= ". "(SELECT min(DAYOFYEAR(birthday)) FROM $table ". "WHERE DAYOFYEAR(birthday)>=DAYOFYEAR(now()))";

                J 1 Reply Last reply
                0
                • M Meax

                  How to make this work in sql server "SELECT * FROM $table WHERE DAYOFYEAR(birthday)= ". "(SELECT min(DAYOFYEAR(birthday)) FROM $table ". "WHERE DAYOFYEAR(birthday)>=DAYOFYEAR(now()))";

                  J Offline
                  J Offline
                  Jay Royall
                  wrote on last edited by
                  #11

                  Luc has given you more than enough help, if you can't help yourself from here then maybe you're in the wrong profession?!

                  M 1 Reply Last reply
                  0
                  • J Jay Royall

                    Luc has given you more than enough help, if you can't help yourself from here then maybe you're in the wrong profession?!

                    M Offline
                    M Offline
                    Meax
                    wrote on last edited by
                    #12

                    i am just trying to learn

                    J 1 Reply Last reply
                    0
                    • M Meax

                      i am just trying to learn

                      J Offline
                      J Offline
                      Jay Royall
                      wrote on last edited by
                      #13

                      Sure I realise that, I am too, but the best way to learn isn't to get other people to do it for you. Like I said, Luc has offered a great deal of help here, more than enough for you to work out the rest. If you still can't do it then post your code, I'm sure someone will be able to point you in the right direction.

                      1 Reply Last reply
                      0
                      • M Meax

                        i have users Date Of Birth as DOB in sql server table called UsersProfile. Person DOB a 3/13/2009 b 3/13/2009 c 3/25/2009 d 4/2/2009 e 4/15/2009 How to select next(upcoming) birthday from this table? if two persons bday falls on a same date, like Person a and Person b, how to select those two person?

                        J Offline
                        J Offline
                        je_gonzalez
                        wrote on last edited by
                        #14

                        SELECT * FROM UsersProfile WHERE ((403 + (31 * month(DOB) + day(DOB)) - (31 * month(getdate()) + day(getdate()))) % 403) = (SELECT MIN(((403 + (31 * month(DOB) + day(DOB)) - (31 * month(getdate()) + day(getdate()))) % 403)) FROM UsersProfile)

                        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