Avatar billede bgo Nybegynder
04. juli 2008 - 14:02 Der er 15 kommentarer og
2 løsninger

udvælg nyere rækker, med dato og tid i to tekstkolonner

Jeg har en tabel hvor dato er én tekstkolonne, og tid er en anden tekstkolonne. Jeg har hverken mulighed for at samle dem til én kolonne eller lave dem om til datotid.

Nu skal jeg så udvælge rækker der er nye end en bestemt dato og tid, ved hjælp af de to kolonner. Spørgsmålet er hvordan? Naturligvis kan jeg indlæse alle rækker og behandle det derefter, men jeg vil helst select'e mig ud af det, af hensyn til både tid og resourcer.

Nogle der ved hvordan? Eller om det slet ikke kan lade sig gøre?
Avatar billede terry Ekspert
04. juli 2008 - 14:36 #1
Try using the Cdate function in a query to convert the text "dates" to a real date.

Then you can use criteria to choose the records you want.
Avatar billede bgo Nybegynder
04. juli 2008 - 14:56 #2
so something like
"select Cdate(textdatefield) as newdate, Cdate(texttimefield) as newtime, * from table where (newdate >= " + laterthandate.tostring + ") and (newtime > " + laterthantime.tostring + ")"

?
Avatar billede terry Ekspert
04. juli 2008 - 16:49 #3
something like that should work, but you can also include the date and time in the same function call.

.. Cdate(textdatefield & ' ' & texttimefield) as DateTimeField

But ther is no guaranty that this will work without alterations. Working with dates is always a problem and when they are in text fields doesnt make it any easier.
Avatar billede bgo Nybegynder
07. juli 2008 - 13:52 #4
hehe working with dates is the ever dreaded task ;)

Anyway, your guaranty of no gauranty was spot on :D

I tried this:

"SELECT COUNT(*) AS amount, CDate(oprettelsesdato & ' ' & oprettelsestidspunkt) as DateTimeOpret, CDate(aendringsdato & ' ' & aendringstidspunkt) as DateTimeRet FROM sagstabel WHERE (DateTimeOpret > #" + lastrundate.ToString + "#) OR (DateTimeRet > #" + lastrundate.ToString + "#)"

and got this error:

"Du har forsøgt at køre en forespørgsel, der ikke medtager det angivne udtryk "CDate(oprettelsesdato & ' ' & oprettelsestidspunkt)" som en del af en aggregatfunktion."

Not sure what that means, anyhow, that made me try this:

"SELECT COUNT(*) AS amount FROM sagstabel WHERE (CDate(oprettelsesdato & ' ' & oprettelsestidspunkt) > #" + lastrundate.ToString + "#) OR (CDate(aendringsdato & ' ' & aendringstidspunkt) > #" + lastrundate.ToString + "#)"

but got this error:

"Datatyperne stemmer ikke overens i kriterieudtrykket."

to which my spontanious response was: "What do you thing I'm trying to work around???" :D
Avatar billede terry Ekspert
07. juli 2008 - 16:26 #5
If you can send me an example of your data then I will see if I can fin da bit of time to find a solution
ekspertenATsanthell.dk
AT=@
Avatar billede terry Ekspert
09. juli 2008 - 19:28 #6
did you receive the example bgo?
Avatar billede bgo Nybegynder
09. juli 2008 - 21:12 #7
yes I did - I'm trying to figure out how to incorporate it into my system :) The thing is, I'm not supposed to change anything in the source database ;) But it must be possible to use your example somehow anyway. I'll let you know how it works out :)
Avatar billede terry Ekspert
10. juli 2008 - 09:00 #8
"..The thing is, I'm not supposed to change anything in the source database ;) ..."

Are you saying that the dB is only for data and your using some other programming language to make the frontend?

You should be able to use the query qryDateTime(I think it was called) as the base for your SQL and incorporate the criteria in that. Then once its working you can copy the SQL into your front-end program. So ther should be no need to alter the Access dB.
Avatar billede bgo Nybegynder
10. juli 2008 - 14:30 #9
That is what I'm saying :) But I did manage to get some steps closer - I think :D

I created a new blank database, created a link to the table in the source database, and then added the qryDateTime query to that new database. Wasn't quite sure if that would work, but it did.

Only thing now is, it keeps saying "Datatyperne stemmer ikke overens i kriterieudtrykket." when trying to use ChangeDateTime in stead of CreateDateTime - can't quite figure out where the difference between them is
Avatar billede terry Ekspert
10. juli 2008 - 15:17 #10
Not sure what you mean there, whats ChangeDateTime and CreateDateTime ?
Avatar billede bgo Nybegynder
10. juli 2008 - 15:32 #11
That are the fields created in qryDateTime ;)

SELECT Tabel1.*, CDate([oprettelsesdato] & ' ' & [oprettelsestidspunkt]) AS CreateDateTime, CDate([aendringsdato] & ' ' & [aendringstidspunkt]) AS ChangeDateTime
FROM Tabel1;

For some reason I can make queries using CreateDateTime in the WHERE criteria, but not when using ChangeDateTime - as the error with datatypes pops up
Avatar billede bgo Nybegynder
10. juli 2008 - 16:00 #12
SELECT COUNT(*) as amount FROM (qryDateTime) WHERE ((qryDateTime.CreateDateTime) > #" + lastrundate + "#)

works fine, but

SELECT COUNT(*) as amount FROM (qryDateTime) WHERE ((qryDateTime.ChangeDateTime) > #" + lastrundate + "#)

doesn't

SELECT COUNT(*) as amount FROM (qryDateTime) WHERE ((qryDateTime.CreateDateTime) > #" + lastrundate + "#) OR ((qryDateTime.ChangeDateTime) > #" + lastrundate + "#)

and the one that would be needed (both as count and regular select) above neither - in both cases "Datatyperne stemmer ikke overens i kriterieudtrykket" pops up, very weird :D
Avatar billede jeppe_k Nybegynder
11. juli 2008 - 00:13 #13
Does this query help, just tried your scenario:
a = date in text form
b = time in text form

the where clause defines the date
SELECT Table1.a, Table1.b, CDate([a])+CDate([b]) AS c
FROM Table1
WHERE (((CDate([a])+CDate([b]))>#1/1/2001 10:10:0#));
Avatar billede terry Ekspert
11. juli 2008 - 08:22 #14
In your program try putting the SQL into a string variable and then copy the string into a query directly in Access. This way you can see exactly how the SQL is and more easily find the problem.
Avatar billede bgo Nybegynder
11. juli 2008 - 11:41 #15
I've got it now :) I raised the amount of points so I could give jeppe and terry 50 each :) So, terry, post an answer :)
Avatar billede bgo Nybegynder
11. juli 2008 - 11:43 #16
oh, the only thing that is wrong now is the date I compare with, as that is in danish format, but that's no problem :)
Avatar billede terry Ekspert
15. juli 2008 - 08:38 #17
;o)
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
Dyk ned i databasernes verden på et af vores praksisnære Access-kurser

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