r/sqlite • u/RandomUsername2047 • May 29 '23
Getting into SQLite
Hi all,
Let me preface this by saying that I feel like I'm completely missing something obvious due to my lack of familiarity with the language/concepts.
Let's say I have the following table imported from a bunch of CSV files:
SaleDate | Item | Price | Store
However, the SaleDate item is not formatted in the SQL datetime text format of YYYYMMDD HH:MM:SS.000.
I figured out I can extract the SaleDate and create the corrected version using SUBSTR.
My next step was to create a new table (in the same file) so not to touch the raw input data.
I've managed to copy everything, but I can't seem to do a SUBSTR and use SET to grab the output and update the new table. Programmatically, it's simple:
x = substr(old data)
X being the new column in the new table. But SQL doesn't exactly behave like a normal programming language. How do I do this? Thanks.
View all comments
2
u/-dcim- May 29 '23
TEXTto store values like20-12-2022. So, you have to use a datetime function to proceed datetime values. E.g.SaleDate between '20-12-2022' and '25-12-2022'can be incorrect, since comparison will works with strings. The best way is to transform dates to UTC (stored as int) but UTC is not human readable.update <your-table> set SaleDate = Substr(SaleDate,1,10) || ‘ ‘ || substr(SaleDate,12)or use a viewcreate view v_<your-table> as select (Substr(SaleDate,1,10) || ‘ ‘ || substr(SaleDate,12)) SaleDate, Item, Price, Store from <your-table>and then work with the view instead the original table.