question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Questions about converting to int in Sqlite

See original GitHub issue

For SQL Server, a number of Convert.ToX(value) functions are translated to CONVERT(X, @value) which is great. I’m particularly interested in Convert.ToInt32.

Like many I use Sqlite for unit testing and found that it’s not translated for Sqlite. I do see a slew of new function mappings for 6.0 which is cool.

My question are:

  1. Is Convert.ToInt32 (and similar) on the cards for Sqlite?
  2. The translation for Convert.ToInt32 in SQL Server uses CONVERT, which is semantically the closest, but my understanding is that CAST functions very similarly- and because CAST is an ANSI-SQL standard, could a general relational translation be provided instead of a SQL Server specific one? I’m genuinely interested to learn about the subtle differences and the decision making process behind function mapping choices.
  3. I don’t think I can add CAST using DbContext.HasDbFunction (for either SQL Server or Sqlite) due to its unusual syntax, i.e. not just taking a parameter but also as int/integer - is this correct or are there any workarounds?

EF Core version: Tried 3.1.13 / 5.0.4 / 6.0 preview 2 Database provider: Microsoft.EntityFrameworkCore.Sqlite

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
bricelamcommented, Apr 12, 2021

SQLite provides an extension for a UINT collation that ignores leading zeros. You could also re-implement it in C#:

sqliteConnection.CreateCollation(
    "UINT",
    // NB: This isn't a true re-implementation of SQLite's UINT collation, but should work for you
    (x, y) => long.Parse(x).CompareTo(long.Parse(y)));
var max = context.Entities.Max(e => EF.Functions.Collate(e.PaddedProperty, "UINT"));
0reactions
stevendarbycommented, Apr 13, 2021

Thank you @bricelam, that’s a good tip. I probably wasn’t clear enough that I’m writing primarily against SQL Server, and was hoping to find something that worked against SQLite in unit tests too. That’s why I sought a function mapping for CAST which works for both. However always good to pick up new tips!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Converting text to int in sqlite when querying
I want to convert both of the following columns to integer (they were placed as text in the SQlite db) as soon as...
Read more >
Frequently Asked Questions
Short answer: A column declared INTEGER PRIMARY KEY will autoincrement. Longer answer: If you declare a column of a table to be INTEGER...
Read more >
Sqlite spec: 4.2. Type Conversions Prior To Comparison
I would expect the following query: CREATE TABLE t (id integer primary key, str varchar(20)); INSERT ...
Read more >
converting int to real in sqlite
If we divide an integer by another integer then the result value will be round off to the nearest integer. So, you can...
Read more >
Untitled
If the tool doesn't convert your SQL file correctly please create an issue. ... Questions about converting to int in Sqlite #24526 -...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found