Avatar billede spejder_leon Nybegynder
08. december 2002 - 13:57 Der er 23 kommentarer og
2 løsninger

Sortere efter delindhold af string (Dato)

Hej,

Jeg har angivet dato som dd-mm-åå i min database, som string/tekst.

Nu vil jeg gerne sortere, først efter år, så mdr, og så dag.

Hvodan gøres det?
Avatar billede terry Ekspert
08. december 2002 - 14:11 #1
Well I would have though that IF you soreted after date then it WOULD be sorted as you want :o)
Avatar billede terry Ekspert
08. december 2002 - 14:14 #2
The way you display dates is one thing, but the way they are stored is always the same, and I am sure it will be soreted after this. If not there are functions which you can use to extract each part of the date as columns, and you can sort after these.
Avatar billede terry Ekspert
08. december 2002 - 14:17 #3
SELECT DATEPART(yy, TheDateField) AS Y, DATEPART(m, TheDateField) AS M, DATEPART(d, TheDateField) AS D ORDER BY Y, M, D
Avatar billede janus_007 Nybegynder
09. december 2002 - 20:42 #4
Det er nu også et dumt format du har valgt! - Husk altid at vælge US ie. yyyymmdd, så har du ingen problemer overhovedet. Jeg ville lave det om med det samme hvis jeg var dig :O)
Avatar billede terry Ekspert
09. december 2002 - 20:52 #5
spejder man :o) hows it going here?
Avatar billede spejder_leon Nybegynder
09. december 2002 - 22:41 #6
Looking into it now.... -Thanks
Avatar billede spejder_leon Nybegynder
09. december 2002 - 23:30 #7
Hi again...

the databse is datbase
the tabel is kalender
the colum datofra

and the datoformat for chrismas is  24-12-02
Were are my mistake...



my code ís

Set kalpunkt = Server.CreateObject ("ADODB.Recordset")
kalpunkt.open "SELECT DATEPART(yy, datofra) AS Y, DATEPART(m, datofra) AS M, DATEPART(d, datofra) AS D ORDER BY Y, M, D FROM kalender", datbase
Avatar billede terry Ekspert
10. december 2002 - 09:09 #8
You dont say what the problem is! If you are not getting the correct records then try your SQL in the query analyser.
Avatar billede terry Ekspert
10. december 2002 - 09:11 #9
If it works in the query analyser then you have a problem with your code, and this is another problem from your original.
Avatar billede janus_007 Nybegynder
10. december 2002 - 10:02 #10
Ikke for at genere, men det er et meget underligt skoleengelsk du holder dig Terry!.
Avatar billede spejder_leon Nybegynder
10. december 2002 - 10:06 #11
FEJLEN ER

Fejltype:
Microsoft OLE DB Provider for ODBC Drivers (0x80040E14)
[Microsoft][ODBC Microsoft Access-driver] SELECT-sætningen indeholder et reserveret ord eller argument, der er stavet forkert eller mangler, eller tegnsætningen er ikke korrekt.
/kalender/kalender.asp, line 22


Linie 22 er netop:
kalpunkt.open "SELECT DATEPART(yy, datofra) AS Y, DATEPART(m, datofra) AS M, DATEPART(d, datofra) AS D ORDER BY Y, M, D FROM kalender", datbase
Avatar billede janus_007 Nybegynder
10. december 2002 - 11:40 #12
Du skal bytte om på order by og from...
SELECT DATEPART(yy, datofra) AS Y, DATEPART(m, datofra) AS M, DATEPART(d, datofra) AS D FROM kalender ORDER BY Y, M, D
Avatar billede terry Ekspert
10. december 2002 - 15:45 #13
janus_007>I'm not quite sure what the problem is, but am in fact english, although I have lived in DK for a number of years now :o)
Avatar billede terry Ekspert
10. december 2002 - 15:48 #14
spejder leon, well its obvious what your problem is now that we see some code!

You have placed your question in MS SQL server BUT you are using an Access driver! Can we please have some more info? IF you are in Access and are selecting from an SQL Server then you use Access SQL syntax unless you are making a passthrough query.
Avatar billede spejder_leon Nybegynder
10. december 2002 - 15:48 #15
I have this:

dim Y, MDR, DAY
kalpunkt.open "SELECT DATEPART(yy, datofra) AS Year, DATEPART(m, datofra) AS Mdr, DATEPART(d, datofra) AS Day FROM kalender ORDER BY Year, Mdr, Day DESC", datbase

And get the fault
Fejltype:

Microsoft OLE DB Provider for ODBC Drivers (0x80040E10)
[Microsoft][ODBC Microsoft Access-driver] Der er for få parametre. Der var ventet 6.
/kalender/kalender.asp, line 17
Avatar billede spejder_leon Nybegynder
10. december 2002 - 15:50 #16
Which means some think like:

to few parametres, excpectet 6
Avatar billede terry Ekspert
10. december 2002 - 16:15 #17
and now we find we are in ASP!!!!!!! spejder, is this access database or SQL SERVER??
Please read my comment 10/12-2002 15:48:16
Avatar billede spejder_leon Nybegynder
10. december 2002 - 16:47 #18
Yes, And sorry. But Im working on a ASP site. wich get the information frow a access database.

But I belive that we only are working on a syntax error right now.

Sorry if I have misplaced my question.
Avatar billede terry Ekspert
10. december 2002 - 17:07 #19
In access there is a BIG difference!

"SELECT DATEPART("yyyy", datofra) AS Y, DATEPART("m", datofra) AS M, DATEPART("d", datofra) AS D FROM kalender ORDER BY Y, M, D DESC"

You must also be carefull using field names such as Day and Year they may very well be reserved words in Access and you may get an error!
Avatar billede spejder_leon Nybegynder
10. december 2002 - 21:00 #20
stil get an error---:

Fejltype:
Der opstod en Microsoft VBScript-kompileringsfejl (0x800A0401)
Slut på sætning var ventet
/kalender/kalender.asp, line 17, column 32
kalpunkt.open "SELECT DATEPART("yyyy", datofra) AS Y, DATEPART("m", datofra) AS M, DATEPART("d", datofra) AS D FROM kalender ORDER BY Y, M, D DESC" , datbase


In english. End of sentence were expected
Avatar billede terry Ekspert
10. december 2002 - 21:18 #21
The problem is more than likley because " is used in the string

"SELECT DATEPART(""yyyy"", datofra) AS Y, DATEPART(""m"", datofra) AS M, DATEPART(""d"", datofra) AS D FROM kalender ORDER BY Y, M, D DESC"

Notice I have used two " in the datepart function. If is still doesnt work try three
another idea is
DIM sSQL

sSQL = "SELECT DATEPART("yyyy", datofra) AS Y, DATEPART("m", datofra) AS M, DATEPART("d", datofra) AS D FROM kalender ORDER BY Y, M, D DESC"

I know almost nothing about ASP but I think you can use this to display the contents of sSQL on the screen


response.write sSQL

and what we are after is the following. The you should be able to copy this to th equery builder in Access and see if it works.

SELECT DATEPART("yyyy", datofra) AS Y, DATEPART("m", datofra) AS M, DATEPART("d", datofra) AS D FROM kalender ORDER BY Y, M, D DESC
Avatar billede spejder_leon Nybegynder
10. december 2002 - 23:41 #22
Sorry,

the ansver should be.

kalpunkt.open "SELECT * FROM kalender ORDER BY CDate(datofra) asc",datbase

But I'm a scout and althow good work should make people happy, and that schould be enough payment, it is Christmas, And you fellows have helped and worked... so I will Pay ;-)

Look at the result at http://roskilde.dbs.dk/kalender/kalender.asp

Thanks. And have a Marry Christmas
Avatar billede spejder_leon Nybegynder
10. december 2002 - 23:42 #23
Hope you are satisfied, else please write me
Avatar billede terry Ekspert
11. december 2002 - 09:22 #24
Hi scout, no problems with the points. Notice my answer 08/12-2002 14:11:04! This is in my mind the same as what you are doing here. IF your field is a date/time type then CDATE should NOT be necessary. But if say it is a text then it may be necessary althoug hyou may get incorrect results!!!!

EXAMPLE>
If its a text field and you have entered in mm/dd/yyyy format then

06/07/2002 is 7th june 2002
But CDate doesnt know that, it thinks its 6th July!!!!

Anyway if yousay its working now then fine :o)
Avatar billede spejder_leon Nybegynder
11. december 2002 - 11:59 #25
You are right about 06/07/2002 is 7th june 2002. BUT! 6 juli 2003 worked when i testted....
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
Computerworld tilbyder specialiserede kurser i database-management

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