Lemmy: Bestiverse
  • Communities
  • Create Post
  • Create Community
  • heart
    Support Lemmy
  • search
    Search
  • Login
  • Sign Up
RSS BotMB to Lobste.rsEnglish · 8 hours ago

CAST(x AS STRING) casts to integer in SQLite

lalitm.com

external-link
message-square
0
fedilink
1
external-link

CAST(x AS STRING) casts to integer in SQLite

lalitm.com

RSS BotMB to Lobste.rsEnglish · 8 hours ago
message-square
0
fedilink
As an “SQLite consultant” for my local area of Google, I often have people come to me having written SQL like: SELECT CAST(bar AS STRING) AS baz FROM foo and ask me “Why is baz always an integer?! Have I hit an SQLite bug?”. I have to again reach for my list of “odd quirks that SQLite has that people don’t know about”. Because this is not a bug, at least according to the SQLite manual. Instead, the correct way to write the above query is: SELECT CAST(bar AS TEXT) AS baz FROM foo The reason for this? Quoting from “Determination of Column Affinity” For tables not declared as STRICT, the affinity of a column is determined by the declared type of the column, according to the following rules in the order shown: If the declared type contains the string “INT” then it is assigned INTEGER affinity. If the declared type of the column contains any of the strings “CHAR”, “CLOB”, or “TEXT” then that column has TEXT affinity. Notice that the type VARCHAR contains the string “CHAR” and is thus assigned TEXT affinity. If the declared type for a column contains the string “BLOB” or if no type is specified then the column has affinity BLOB. If the declared type for a column contains any of the strings “REAL”, “FLOA”, or “DOUB” then the column has REAL affinity. Otherwise, the affinity is NUMERIC. STRING does not match any of the numbered rules and so fallback to NUMERIC affinity which, in the general case, means integer. Due to SQLite’s staunch stance of being “backwards compatible” there’s very little chance of this paper-cut ever going away. But at least next time someone comes to me with this issue, I’ll be able to link to this post instead of writing the same thing for the nth time :)

Comments

alert-triangle
You must log in or register to comment.

Lobste.rs

lobsters

Subscribe from Remote Instance

You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: !lobsters@lemmy.bestiver.se
lock
Community locked: only moderators can create posts. You can still comment on posts.

RSS Feed of lobste.rs

Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 11 users / day
  • 61 users / week
  • 278 users / month
  • 1.28K users / 6 months
  • 2 local subscribers
  • 270 subscribers
  • 8.79K Posts
  • 442 Comments
  • Modlog
  • mods:
  • patrick
  • RSS Bot
  • BE: 0.19.5
  • Modlog
  • Instances
  • Docs
  • Code
  • join-lemmy.org