Avatar billede zeusdk Nybegynder
04. januar 2003 - 17:15 Der er 54 kommentarer og
1 løsning

Fejl i sql

Hej

Jeg har følgende script, hvor der er noget galt med linien "strSQL", fordi den returnerer ikke noget, selvom der er masser den burdte returnere.

SQL'en: Select distinct datopublish from search where (datopublish between #05-01-2003# and #03-01-2004#) order by datopublish

Kan du finde fejlen?

<%
    Set myConn=Server.CreateObject("ADODB.Connection")
    myConn.Open ("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE="+server.Mappath("/db/selvet.mdb"))
    strSQL = "Select distinct datopublish from search where (datopublish between #" & Date() + 1 & "# and #" & Date() + 364 & "#) order by datopublish"
   
    set rs = myConn.execute(strSQL)
   
    do while not rs.EOF
        if isDate(rs("datopublish")) then
            myDates = myDates & rs("datopublish") & ", "
        End If
    rs.MoveNext
    Loop
   
    if not myDates = "" then
        myDates = left(myDates,len(myDates)-2)
    end if
   
    response.write myDates
    myDateArr = Split(myDates,",")
%>
Avatar billede medions Nybegynder
04. januar 2003 - 17:20 #1
strSQL = "Select distinct datopublish from search where (date > " & Date() + 1 & "# AND date < #" & Date() + 364 & "#) order by datopublish"

//>Rune
Avatar billede medions Nybegynder
04. januar 2003 - 17:21 #2
Her er "syntaxen" for den:

SELECT * FROM tabelnavn WHERE date > #dato1# AND date < #dato1#

//>Rune
Avatar billede terry Ekspert
04. januar 2003 - 17:23 #3
You need to make sure that the date is in th ecorrect format

MM-DD-YY or YYYY-MM-DD
Avatar billede terry Ekspert
04. januar 2003 - 17:26 #4
Ithink you can use the format function in ASP, and also dateAdd.

Format(DateAdd("d", 1 Date()), "yyyy-mm-dd")
Avatar billede terry Ekspert
04. januar 2003 - 17:27 #5
strSQL = "Select distinct datopublish from search where (date > " & Format(DateAdd("d", 1, Date()), "yyyy-mm-dd") & "# AND date < #" & Format(DateAdd("d", 364, Date()), "yyyy-mm-dd")& "#) order by datopublish"
Avatar billede zeusdk Nybegynder
04. januar 2003 - 17:28 #6
>> medions
Microsoft JET Database Engine (0x80040E14)
Syntax error (missing operator) in query expression '(date > 05-01-2003# AND date < #03-01-2004#)'.
Avatar billede medions Nybegynder
04. januar 2003 - 17:30 #7
Jeg glemte en # sorry! prøv igen!

strSQL = "Select distinct datopublish from search where (date > #" & Date() + 1 & "# AND date < #" & Date() + 364 & "#) order by datopublish"

//>Rune
Avatar billede zeusdk Nybegynder
04. januar 2003 - 17:33 #8
>> medions
Microsoft JET Database Engine (0x80040E10)
No value given for one or more required parameters.
Avatar billede medions Nybegynder
04. januar 2003 - 17:43 #9
strSQL = "Select distinct datopublish from search where (date > #" & Format(DateAdd("d", 1, Date()), "yyyy-mm-dd") & "# AND date < #" & Format(DateAdd("d", 364, Date()), "yyyy-mm-dd")& "#) order by datopublish"

Så prøv lige den her!

//>Rune
Avatar billede zeusdk Nybegynder
04. januar 2003 - 17:45 #10
Microsoft VBScript runtime (0x800A000D)
Type mismatch: 'Format'
Avatar billede terry Ekspert
04. januar 2003 - 17:51 #11
if format doesnt work in VBSCript then what about datepart?
Avatar billede zeusdk Nybegynder
04. januar 2003 - 18:10 #12
>> terry
Du har frie hænder til at komme med kodeforlag ligesom medions :-)
Avatar billede _just4fun_ Nybegynder
04. januar 2003 - 18:13 #13
Er den første ikke ok hvis du bare vender de to datoer om?

Select distinct datopublish from search where (datopublish between #03-01-2003# and #05-01-2004#) order by datopublish
Avatar billede _just4fun_ Nybegynder
04. januar 2003 - 18:13 #14
ups... så ikke der var forskel på år også....
Avatar billede zeusdk Nybegynder
04. januar 2003 - 22:26 #15
>> medions

Jeg har desperat brug for at svar :)

Hvorfor siger den følgende til dit sidste kodeforslag?

Microsoft VBScript runtime (0x800A000D)
Type mismatch: 'Format'

Hvis det kan være nogen hjælp så ligger datoerne i en access-database og de er formateret efter følgende princip: f.eks.: 03-01-2003
Avatar billede medions Nybegynder
04. januar 2003 - 22:45 #16
Hmm så prøv med denne!

strSQL = "SELECT DISTINCT datopublish FROM search WHERE (date > #" & DatePart(DateAdd("d", 1, Date()), "dd-mm-yyyy") & "# AND date < #" & Datepart((DateAdd("d", 364, Date()), "dd-mm-yyyy")& "#) order by datopublish"

//>Rune
Avatar billede zeusdk Nybegynder
04. januar 2003 - 22:50 #17
Microsoft VBScript compilation (0x800A03EE)
Expected ')'
/dbsite/artikel/artikel1.asp, line 242, column 173
Avatar billede hossein Nybegynder
04. januar 2003 - 22:57 #18
Hvis feltet:

datopublish
01-02-1999
06-06-2002
03-03-2002
12-12-2002
09-08-2003
01-02-2003

Efter den nedenstående kode bliver putputet:
09-08-2003
01-02-2003
selvfølgelig formatet skal rettes

<%
Set myConn=Server.CreateObject("ADODB.Connection")
myConn.Open ("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE="+server.Mappath("/db/selvet.mdb"))
strSQL = "Select * from search  where (datopublish between #" & (Date() + 1) &  "# and #"& (Date() + 364) & " #) order by datopublish"
set rs = myConn.execute(strSQL)
do while not rs.EOF
if isDate(rs("datopublish")) then
myDates = myDates & rs("datopublish") & ", "
End If
rs.MoveNext
Loop
if not myDates = "" then
myDates = left(myDates,len(myDates)-2)
end if
response.write("***" & myDates & "***")
myDateArr = Split(myDates,",")
%>

Det har jeg testet og virker fint
Avatar billede medions Nybegynder
04. januar 2003 - 22:58 #19
Prøv at læs denne artikel og se om du får noget ud af det?!

http://activedeveloper.dk/brevkassen/default.asp?category=14&question=201

//>Rune
Avatar billede hossein Nybegynder
04. januar 2003 - 23:13 #20
datopublish i din tabel, har den en datatype som "Dato og klokkeset"?
Avatar billede terry Ekspert
05. januar 2003 - 10:17 #21
strSQL = "SELECT DISTINCT datopublish FROM search WHERE date > #" & DatePart(DateAdd("d", 1, Date()), "dd-mm-yyyy") & "# AND date < #" & Datepart(DateAdd("d", 364, Date()), "dd-mm-yyyy") & "# order by datopublish"
Avatar billede zeusdk Nybegynder
05. januar 2003 - 10:19 #22
>> terry
Microsoft VBScript runtime (0x800A000D)
Type mismatch: '[string: "dd-mm-yyyy"]'
Avatar billede zeusdk Nybegynder
05. januar 2003 - 10:29 #23
>>hossein
>datopublish i din tabel, har den en datatype som "Dato og klokkeset"?
Ja, og de er som sagt formateret på følgende måde: 05-01-2003
Avatar billede terry Ekspert
05. januar 2003 - 10:59 #24
what am I doing!!!!! (still sleeping here :o))
back in 5
Avatar billede terry Ekspert
05. januar 2003 - 11:02 #25
Just a question, are you trying to select those from todays dateand one year on?
Avatar billede zeusdk Nybegynder
05. januar 2003 - 11:03 #26
yes
Avatar billede terry Ekspert
05. januar 2003 - 11:41 #27
strSQL = "SELECT DISTINCT datopublish FROM search WHERE datopublish <= #"  & Year(date) & "-" & month(date) & "-" & Day(date) &  "# AND datopublish < #" & Year(dateadd("yyyy",1,date)) & "-" & month(dateadd("yyyy",1,date)) &  "-" & Day(dateadd("yyyy",1,date)) & "# order by datopublish"
Avatar billede terry Ekspert
05. januar 2003 - 11:41 #28
but I have no idea if these functions can be used in VBSCRIPT
Avatar billede terry Ekspert
05. januar 2003 - 11:48 #29
Avatar billede terry Ekspert
05. januar 2003 - 11:49 #30
Avatar billede terry Ekspert
05. januar 2003 - 11:53 #31
what you have to notice though, is that the formatDateTime function uses the computers REGIONAL settings, and this means if the server is using Danish Regional Settings you WILL get the wrong format.
Avatar billede zeusdk Nybegynder
05. januar 2003 - 11:54 #32
I corrected year and day by replacing them - and the sql-output was:

SELECT DISTINCT datopublish FROM search WHERE datopublish <= #5-1-2003# AND datopublish < #5-1-2004# order by datopublish

Date-output (on the screen):
14-01-1999, 01-01-2001, 01-02-2001, 05-02-2001, 15-02-2001, 20-02-2001, 01-03-2001, 02-03-2001, 07-03-2001, 09-03-2001, 11-03-2001, 16-03-2001, 20-03-2001, 23-03-2001, 27-03-2001, 30-03-2001, 01-04-2001, 04-04-2001, 06-04-2001, 07-04-2001, 09-04-2001, 13-04-2001, 18-04-2001, 20-04-2001, 22-04-2001, 23-04-2001, 25-04-2001, 01-05-2001, 04-05-2001, 2002, 19-12-2002, 20-12-2002, 21-12-2002, 22-12-2002, 23-12-2002, 24-12-2002, 25-12-2002, 26-12-2002, 27-12-2002, 28-12-2002, 29-12-2002, 30-12-2002, 31-12-2002, 01-01-2003, 02-01-2003, 03-01-2003, 04-01-2003, 05-01-2003, 06-01-2003, 07-01-2003, 08-01-2003, 09-01-2003, 10-01-2003, 11-01-2003, 12-01-2003, 13-01-2003, 14-01-2003, 15-01-2003, 16-01-2003, 17-01-2003, 18-01-2003, 19-01-2003, 24-01-2003

Problem:
It takes all dates - and not only dates from todays date and one year on.
Avatar billede terry Ekspert
05. januar 2003 - 12:21 #33
I'm not sure what you mean! "I corrected year and day by replacing them" do you mean that you hardcoded the dates?

If so try

SELECT DISTINCT datopublish FROM search WHERE datopublish <= #2003-01-05# AND datopublish < #2004-01-05# order by datopublish


Here is another possible solution

SELECT DISTINCT datopublish
FROM search
WHERE (((CLng([datopublish]))>=CLng(Date()) And (CLng([datopublish])) < CLng(DateAdd("yyyy",1,Date()))))
Avatar billede zeusdk Nybegynder
05. januar 2003 - 12:24 #34
>I'm not sure what you mean! "I corrected year and day by replacing them"
I just put day before month and month before year.

I will now try your new solution.
Avatar billede terry Ekspert
05. januar 2003 - 12:26 #35
The way dates are displayed on the screen is taken from the PC's regional settings which in this case is the SERVER. This has NOTHING to do with how the date is actually stored in the database. Dates are stored as large numbers and that is why I can use Clng to convert it to a long integer.
Avatar billede zeusdk Nybegynder
05. januar 2003 - 12:28 #36
>SELECT DISTINCT datopublish FROM search WHERE datopublish <= #2003-01-05# AND datopublish < #2004-01-05# order by datopublish
Expected 'Case'
Avatar billede zeusdk Nybegynder
05. januar 2003 - 12:30 #37
>Expected 'Case'
Sorry - my mistake.
Avatar billede zeusdk Nybegynder
05. januar 2003 - 12:32 #38
But "SELECT DISTINCT datopublish FROM search WHERE datopublish <= #2003-01-05# AND datopublish < #2004-01-05# order by datopublish" does the same as "Kommentar: zeusdk 05/01-2003 11:54:57"

I will now try your last new solution.
Avatar billede terry Ekspert
05. januar 2003 - 12:33 #39
?
what is 05/01-2003 12:28:40 telling us?
Avatar billede terry Ekspert
05. januar 2003 - 12:34 #40
what about
05/01-2003 11:41:12
have you tried this?
Avatar billede zeusdk Nybegynder
05. januar 2003 - 12:40 #41
SELECT DISTINCT datopublish FROM search WHERE datopublish <= #01/05-2003# AND datopublish < #01/05-2004# order by datopublish

Does the same as "Kommentar: zeusdk 05/01-2003 11:54:57"

And

strSQL = "SELECT DISTINCT datopublish FROM search WHERE (((CLng([datopublish]))>=CLng(Date()) And (CLng([datopublish])) < CLng(DateAdd("yyyy",1,Date()))))"

Is not working because of the 2 "'s in the string.
Avatar billede zeusdk Nybegynder
05. januar 2003 - 12:44 #42
I misunderstod "Kommentar: terry 05/01-2003 12:33:14" and "Kommentar: terry
05/01-2003 12:34:26"

Please wait - I will soon write the errors that they returned.
Avatar billede zeusdk Nybegynder
05. januar 2003 - 12:50 #43
>what is 05/01-2003 12:28:40 telling us?
>what is 05/01-2003 12:28:40 telling us?
They are both doing the same as "Kommentar: zeusdk 05/01-2003 11:54:57".

The problem is that some solutions return all the dates and other solutions return no dates.
Avatar billede zeusdk Nybegynder
05. januar 2003 - 12:51 #44
Sorry it should have said:

>what is 05/01-2003 12:28:40 telling us?
>05/01-2003 11:41:12?
They are both doing the same as "Kommentar: zeusdk 05/01-2003 11:54:57".

The problem is that some solutions return all the dates and other solutions return no dates.
Avatar billede terry Ekspert
05. januar 2003 - 13:00 #45
strSQL = "SELECT DISTINCT datopublish FROM search WHERE (((CLng([datopublish]))>=CLng(Date()) And (CLng([datopublish])) < " & CLng(DateAdd("yyyy",1,Date())) & ")"

cant be far wrong, although IF the other answers are not working then there IS something else which is wrong I am sure!
Avatar billede terry Ekspert
05. januar 2003 - 13:02 #46
can you send me the database? eksperten@santhell.dk
Avatar billede zeusdk Nybegynder
05. januar 2003 - 13:03 #47
Microsoft JET Database Engine (0x80040E14)
Missing ), ], or Item in query expression '(((CLng([datopublish]))>=CLng(Date()) And (CLng([datopublish])) < 37991)'.
Avatar billede terry Ekspert
05. januar 2003 - 13:37 #48
well the problem is obvious when you can see the result with the correct data!

This WILL hopefully work:

strSQL = "SELECT DISTINCT datopublish FROM search WHERE datopublish >= #"  & Year(date) & "-" & month(date) & "-" & Day(date) &  "# AND datopublish < #" & Year(dateadd("yyyy",1,date)) & "-" & month(dateadd("yyyy",1,date)) &  "-" & Day(dateadd("yyyy",1,date)) & "# order by datopublish"
Avatar billede terry Ekspert
05. januar 2003 - 13:39 #49
the problem is we have been focused on getting the date correct that we didnt notice
<= (less than or equals)
which should be >= (Greater than or equals)
Avatar billede terry Ekspert
05. januar 2003 - 13:40 #50
using clng() will give problems because there are records with NULL in date the field!
Avatar billede zeusdk Nybegynder
05. januar 2003 - 14:11 #51
Yes, yes, yes, yes !!! :-) It is working now thanks to you :-)

I have another - proberly a very little problem - and that is that I have a dropdown-box where all the dates, which are NOT in the sql-string are listed. But they are not listed at all - instead all the dates are listed. If you also can fix this problem I will give you 100 points ekstra.

<%
    Set myConn=Server.CreateObject("ADODB.Connection")
    myConn.Open ("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE="+server.Mappath("/db/selvet.mdb"))
   
    strSQL = "SELECT DISTINCT datopublish FROM search WHERE datopublish >= #"  & Year(date) & "-" & month(date) & "-" & Day(date) &  "# AND datopublish < #" & Year(dateadd("yyyy",1,date)) & "-" & month(dateadd("yyyy",1,date)) &  "-" & Day(dateadd("yyyy",1,date)) & "# order by datopublish"
    'strSQL = "Select distinct datopublish from search where (datopublish between #" & Date() + 1 & "# and #" & Date() + 364 & "#) order by datopublish"

    set rs = myConn.execute(strSQL)
   
    do while not rs.EOF
        if isDate(rs("datopublish")) then
            myDates = myDates & rs("datopublish") & ", "
        End If
    rs.MoveNext
    Loop
   
    if not myDates = "" then
        myDates = left(myDates,len(myDates)-2)
    end if
   
    response.write "*** " & myDates & " ***"
    myDateArr = Split(myDates,",")
%>

<select name="datoPublikation" style="font-size: 10pt; font-family: Trebuchet MS">

<%
'Jeg skal have en uge til at reagere på
dato = date () + 1
datoSlut = DateAdd("yyyy",1,date)
i = 0
do while dato <= datoSlut
  if CDate(myDateArr(i)) <> CDate(dato) then
    Response.Write "<option value=""" & dato & """>" & dato & "</option>"
  else
    i = i + 1
    if i > ubound(myDateArr) then i = ubound(myDateArr)
  end if
  dato = dateAdd("d",1,dato)
loop
%>

</select>
Avatar billede zeusdk Nybegynder
05. januar 2003 - 14:14 #52
No, wait I fixed myself.
Avatar billede terry Ekspert
05. januar 2003 - 14:19 #53
So its all working now?

Thanks :o)
Avatar billede zeusdk Nybegynder
05. januar 2003 - 14:21 #54
Thank you again - terry.
Avatar billede zeusdk Nybegynder
05. januar 2003 - 14:21 #55
>So its all working now?
Yes, yes - everything :-)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester