Perfect hash function for weekday names

This is probably only of limited usefulness in practice, but it came as a spinoff from a discussion on #postgresql. The basic problem is, given a string which you already know is an English weekday name, return the day number (Sunday=0):

create function dayno(text)
  returns integer
  language sql immutable
  as $f$
    select (( ((ascii(substring($1 from 3)) & 22)*10)
              # (ascii($1) & 23) )*5 + 3) % 7;
$f$;

I came up with several alternative functions, but this one has the advantage of only needing one substring() call (it makes use of the fact that ascii() looks only at the first character). It’s also case-insensitive (since it looks at only the low bits of the characters).

select dayno(d)
  from regexp_split_to_table('Sun,MON,tUe,WeD,tHU,FRi,saT',',') r(d);
 dayno 
-------
     0
     1
     2
     3
     4
     5
     6
(7 rows)

This entry was posted in PlanetPG, Postgres, SQL. Bookmark the permalink.

7 Responses to Perfect hash function for weekday names

  1. Its like you learn my thoughts! You seem to understand so
    much about this, such as you wrote the e book in it or something.
    I think that you could do with some p.c. to force the message home a little
    bit, however other than that, that is fantastic blog.
    A fantastic read. I’ll certainly be back.

  2. Great blog here! Also your site loads up fast! What web host are you using?
    Can I get your affiliate link to your host?
    I wish my web site loaded up as quickly as yours lol

  3. tôi đã sử dụng và cảm thấy rất hài lòng về Thương Mại &
    Dịch Vụ của Công Ty vận tải đường bộ Phước Tấn. vận chuyển nam bắc siêu nhanh chỉ từ 2-3 ngày là tôi đã nhận được
    hàng.

  4. Вечером 14 сентября произошла привычная осенняя презентация от фирмы Эпл, где были представлены долгожданные свежие релизы: серия гаджетов iPhone 13, Айпад, iPad mini
    и Apple Watch Series 7.
    1. Дизайн гаджетов претерпел наименьших изменений: наиболее ощутимое внешнее отличие
    – уменьшенный разрез в экране на
    передней доли смартфона с помощью смещения
    верхнего динамика далее к торцу корпуса.

  5. I loɡed as much as you will receiᴠe carried oⲟut
    riɡht here. The sketch is attractive, your aսthored material
    stylish. nonetһeless, you command get ggot an impatience oveг that you wish be ɗelivering the following.
    ᥙnwelⅼ unquestionabⅼy come fuгther
    foгmerly agaіn since exactly the same neaгly a lot often inside case you sһield
    this incгease.

  6. SEO says:

    Hi terrific blog! Does running a blog like this
    require a great deal of work? I’ve very little expertise in coding however I was hoping to start my own blog in the near
    future. Anyway, if you have any suggestions or techniques for
    new blog owners please share. I understand this is off topic nevertheless I simply needed to ask.
    Kudos!

  7. Great web site you have here.. It’s difficult to find high quality writing like yours these days.
    I really appreciate people like you! Take care!!

Leave a Reply

Your email address will not be published.