home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / database / theory / 653 < prev    next >
Encoding:
Internet Message Format  |  1992-12-14  |  2.6 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!darwin.sura.net!ukma!hsdndev!dartvax!kip-sn-16.dartmouth.edu!user
  2. From: carl.pedersen@dartmouth.edu (L. Carl Pedersen)
  3. Newsgroups: comp.databases.theory
  4. Subject: Re: date -> day of week conversion
  5. Message-ID: <carl.pedersen-141292200412@kip-sn-16.dartmouth.edu>
  6. Date: 15 Dec 92 01:51:07 GMT
  7. References: <Byqssz.Bu5@gabriel.keele.ac.uk> <dtb.724154401@otto> <1992Dec14.174550.24045@enterprise.rdd.lmsc.lockheed.com>
  8. Sender: news@dartvax.dartmouth.edu (The News Manager)
  9. Followup-To: comp.databases.theory
  10. Organization: Dartmouth College
  11. Lines: 49
  12.  
  13. In article <1992Dec14.174550.24045@enterprise.rdd.lmsc.lockheed.com>,
  14. stroup@aspen.ops.lmsc.lockheed.com () wrote:
  15. > >
  16. > csa09@keele.ac.uk (Paul Singleton) writes:
  17. > >Can anyone produce SQL code to convert a date from DD/MM/YY (or MM/DD/YY)
  18. > >format into the corresponding day of the week?  I assume that you can
  19. > >get at the individual DD, MM and YY fields (as integers?).
  20. > >
  21. > Maybe I'm missing the point here (or maybe this is an Oracle extension to 
  22. > SQL) but can't you just use the to_char function:
  23. >     Select to_char(my_date,'DAY') from my_table;
  24. > According to the doc, DAY gives you the name of the day, padded with blanks 
  25. > to a length of nine characters.
  26. >                         judie
  27.  
  28. Yes.  That is an ORACLE extension, obviously a good one.  Does anyone know
  29. if the SQL92 standard provides something like this?  It doesn't seem to be
  30. mentioned in the Melton/Simon book.
  31.  
  32. I agree with whoever said this is not the sort of functionality SQL was
  33. designed for, but      think it's still be possible to do the translation in a
  34. non-procedural way, depending on what functions are available in your local
  35. SQL dialect.  
  36.  
  37. In the gregorian calendar, the remainder of the year divided by 400
  38. determines whether or not it's a leap year.  If you can extract the year,
  39. and determine the remainder mod 400, you could use that as an index into a
  40. table of year types (400 rows, values leap or normal).
  41.  
  42. If you can extract the month, then the month combined with the year type
  43. can be used as a composite key into a table that tells how many days in the
  44. year preceded that month.  (24 rows).
  45.  
  46. Given all of the above, a complex expression can be written that converts a
  47. date to a number of days since the beginning of year zero.  If you know
  48. what day of the week that was, you can use the result + the beginning day
  49. to look up in a little table of days.
  50.  
  51. I make no claims that this is the best way to do this.  I also do not claim
  52. that it takes into account any special cases, like historical changes to
  53. the calendar etc.
  54.