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. Database query

Database query

Scheduled Pinned Locked Moved Database
databasesql-serversysadminhelp
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.
  • P Offline
    P Offline
    pradipta
    wrote on last edited by
    #1

    I have table (in sql server database) which contains two fields ,Name(varchar) and Date_of_Birth(Datetime) I want to fetch those records whose birth day is in this week (incuding today(system date)). Please help me with the query

    A 1 Reply Last reply
    0
    • P pradipta

      I have table (in sql server database) which contains two fields ,Name(varchar) and Date_of_Birth(Datetime) I want to fetch those records whose birth day is in this week (incuding today(system date)). Please help me with the query

      A Offline
      A Offline
      Alexander Kojevnikov
      wrote on last edited by
      #2

      SELECT t.Name FROM MyTable AS t WHERE DATEPART(week,t.Date_of_Birth)=DATEPART(week,GETDATE()) Alexandre Kojevnikov MCP (SQL2K, Win/C#) Leuven, Belgium

      P 1 Reply Last reply
      0
      • A Alexander Kojevnikov

        SELECT t.Name FROM MyTable AS t WHERE DATEPART(week,t.Date_of_Birth)=DATEPART(week,GETDATE()) Alexandre Kojevnikov MCP (SQL2K, Win/C#) Leuven, Belgium

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

        thats nice ,but i want only those date of birth which is between today and 7 days after today(not before). for example today date is 4/1/2003 i want to fetch those date of birth which is today and 7 days from today i.e the records should be like below() 4/1/2003 4/2/2003 4/3/2003 4/4/2003 4/5/2003 .........

        J A 2 Replies Last reply
        0
        • P pradipta

          thats nice ,but i want only those date of birth which is between today and 7 days after today(not before). for example today date is 4/1/2003 i want to fetch those date of birth which is today and 7 days from today i.e the records should be like below() 4/1/2003 4/2/2003 4/3/2003 4/4/2003 4/5/2003 .........

          J Offline
          J Offline
          Jeremy Oldham
          wrote on last edited by
          #4

          pradipta wrote: but i want only those date of birth which is between today and 7 days after today(not before). SELECT t.Name FROM MyTable AS t WHERE t.Date_of_Birth between GETDATE() AND DATEADD(d, 7, GETDATE()) You might need to manipulate this a bit. It will return exactly 7 days (7 days * 24 hours). The time element may throw the query off. For example, if the current system date/time is 04/02/2002 13:00:00 then a birthday with the following will not appear: 04/02/2002 1:00:00 and 04/02/2002 00:00:00. Jeremy Oldham

          P 1 Reply Last reply
          0
          • P pradipta

            thats nice ,but i want only those date of birth which is between today and 7 days after today(not before). for example today date is 4/1/2003 i want to fetch those date of birth which is today and 7 days from today i.e the records should be like below() 4/1/2003 4/2/2003 4/3/2003 4/4/2003 4/5/2003 .........

            A Offline
            A Offline
            Alexander Kojevnikov
            wrote on last edited by
            #5

            DECLARE @now AS datetime SET @now = GETDATE() DECLARE @today AS datetime SET @today = CAST(CONVERT(char(8), @now, 112) AS datetime) SELECT t.Name FROM MyTable AS t WHERE DATEDIFF(Day,@today,t.Date_of_Birth) BETWEEN 0 AND 6 112 is ISO format: yyyyMMdd. SQL Server handles conversion from this format to datetime no matter which DATEFORMAT is active. Alexandre Kojevnikov MCP (SQL2K, Win/C#) Leuven, Belgium

            1 Reply Last reply
            0
            • J Jeremy Oldham

              pradipta wrote: but i want only those date of birth which is between today and 7 days after today(not before). SELECT t.Name FROM MyTable AS t WHERE t.Date_of_Birth between GETDATE() AND DATEADD(d, 7, GETDATE()) You might need to manipulate this a bit. It will return exactly 7 days (7 days * 24 hours). The time element may throw the query off. For example, if the current system date/time is 04/02/2002 13:00:00 then a birthday with the following will not appear: 04/02/2002 1:00:00 and 04/02/2002 00:00:00. Jeremy Oldham

              P Offline
              P Offline
              pradipta
              wrote on last edited by
              #6

              i think U did not understand the problem.the query u have written only to retrive those DOB which will lie within this week.But DOB means only the month and day will be same whatever may be the year ,for exammple today is 4/4/2003 and DOB of a member 4/4/1975 then today is the birth day of that memeber. so i want only the dob from database whose day and month will be equal to today's day and month and also those whose b'day will be within 7 days beyond today just see the example dob in the data base DOB(mm/dd/yyyy) 4/4/1976 4/4/1970 4/6/1976 4/8/1999 1/1/1990 1/3/2000 ------------ Today is -4/4/2003 i want the the data from database 4/4/1976 4/4/1970 4/6/1976 4/8/1999 because the above dobs will lie on this week pradipta

              J 1 Reply Last reply
              0
              • P pradipta

                i think U did not understand the problem.the query u have written only to retrive those DOB which will lie within this week.But DOB means only the month and day will be same whatever may be the year ,for exammple today is 4/4/2003 and DOB of a member 4/4/1975 then today is the birth day of that memeber. so i want only the dob from database whose day and month will be equal to today's day and month and also those whose b'day will be within 7 days beyond today just see the example dob in the data base DOB(mm/dd/yyyy) 4/4/1976 4/4/1970 4/6/1976 4/8/1999 1/1/1990 1/3/2000 ------------ Today is -4/4/2003 i want the the data from database 4/4/1976 4/4/1970 4/6/1976 4/8/1999 because the above dobs will lie on this week pradipta

                J Offline
                J Offline
                Jeremy Oldham
                wrote on last edited by
                #7

                pradipta wrote: the query u have written only to retrive those DOB which will lie within this week.But DOB means only the month and day will be same whatever may be the year... Sorry about that. Who knows where my mind has be drifting. :-O Try the following: SELECT t.myName, cast(cast(Month(getdate()) as varchar(2)) + '/' + cast(Day(getdate())as varchar(2)) + '/' + cast(Year(getdate())as varchar(4)) as datetime), cast(cast(Month(t.DOB)as varchar(2)) + '/' + cast(Day(t.DOB)as varchar(2)) + '/' + cast(Year(getdate())as varchar(4)) as datetime) FROM DOB AS t WHERE datediff(d, cast(cast(Month(getdate()) as varchar(2)) + '/' + cast(Day(getdate())as varchar(2)) + '/' + cast(Year(getdate())as varchar(4)) as datetime), cast(cast(Month(t.DOB)as varchar(2)) + '/' + cast(Day(t.DOB)as varchar(2)) + '/' + cast(Year(getdate())as varchar(4)) as datetime)) Between 0 and 6 Let me know for sure that this works properly. Jeremy Oldham

                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