If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Access > need to know if there can be an sql statement which can do the following

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-06-12, 05:00
hish_r hish_r is offline
Registered User
 
Join Date: Feb 2012
Posts: 6
need to know if there can be an sql statement which can do the following

for creating table that contains list of dates between 2 dates which can be used for joining purposes

for example if i have 2 dates 1-1-2010 & 1-5-2010
the table should contains the following

1-1-2010
1-2-2010
1-3-2010
1-4-2010
1-5-2010

thank you
----

sorry i want access fourm

Last edited by hish_r; 02-06-12 at 05:17.
Reply With Quote
  #2 (permalink)  
Old 02-06-12, 05:20
hish_r hish_r is offline
Registered User
 
Join Date: Feb 2012
Posts: 6
creating table that contains list of dates between 2 dates

how i can creating table that contains list of dates between 2 dates which can be used for joining purposes

for example if i have 2 dates 1-1-2010 & 1-5-2010
the table should contains the following

1-1-2010
1-2-2010
1-3-2010
1-4-2010
1-5-2010

thank you
Reply With Quote
  #3 (permalink)  
Old 02-06-12, 06:35
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
create a new table called numbers, containing only one column called n, the primary key, and populate this table with the numbers 0 through whatever large number you want that will cover the number of dates between your first date and last date

then
Code:
SELECT DATEADD('day',n,'2010-01-01') AS thedate
  FROM numbers
 WHERE DATEADD('day',n,'2010-01-01') <= '2010-05-01'
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 02-06-12, 06:54
Sinndho Sinndho is offline
Registered User
 
Join Date: Mar 2009
Posts: 3,446
Her's a possibility (using VBA):
Code:
Public Sub CreateDateRangeTable(ByVal TableName As String, ByVal Date1 As Date, ByVal Date2 As Date)

    Const c_SQL As String = "INSERT INTO @T ( Dte ) VALUES ( @V );"
    
    Dim dbs As DAO.Database
    Dim tdf As DAO.TableDef
    Dim cnt As Long
    Dim i As Long
    
    Set dbs = CurrentDb
    For Each tdf In dbs.TableDefs
        If tdf.Name = TableName Then
            dbs.Execute "DROP TABLE " & TableName & ";", dbFailOnError
            Exit For
        End If
    Next tdf
    Set tdf = Nothing
    dbs.Execute "CREATE TABLE " & TableName & " (Dte DATETIME CONSTRAINT PrimaryKey PRIMARY KEY);", dbFailOnError
    cnt = DateDiff("d", Date1, Date2)
    For i = 0 To cnt
        dbs.Execute Replace(Replace(c_SQL, "@T", TableName), "@V", "#" & DateAdd("d", i, Date1) & "#")
    Next i
    Set dbs = Nothing
    
End Sub
__________________
Have a nice day!
Reply With Quote
  #5 (permalink)  
Old 02-06-12, 06:58
hish_r hish_r is offline
Registered User
 
Join Date: Feb 2012
Posts: 6
thx

thx alot
but do you have another way without using the DATEADD ?!\
Reply With Quote
  #6 (permalink)  
Old 02-06-12, 07:10
hish_r hish_r is offline
Registered User
 
Join Date: Feb 2012
Posts: 6
thank you

thank you
very much
Reply With Quote
  #7 (permalink)  
Old 02-06-12, 07:11
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
what's wrong with using DATEADD?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 02-06-12, 07:22
hish_r hish_r is offline
Registered User
 
Join Date: Feb 2012
Posts: 6
sorry there is no problem in using it
but the problem is in my database which doesn't support it
i'm thinking if there could be away by using basic sql statements without functions
Reply With Quote
  #9 (permalink)  
Old 02-06-12, 07:37
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by hish_r View Post
but the problem is in my database which doesn't support it
what kind of database is that?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 02-06-12, 07:52
hish_r hish_r is offline
Registered User
 
Join Date: Feb 2012
Posts: 6
i think dbisam version 4
Reply With Quote
  #11 (permalink)  
Old 02-06-12, 08:30
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
dbisam? i've never heard of that

are you sure that's a microsoft access database?

note: you posted in the microsoft access forum
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On