I have several data tables where the "SampleName" column has integer and character values.
I'd like to split that column into 2; "Date" (int) and "SiteName" (Char).
Also - if there is a command function that can reorganize the integers into date values, this would be super helpful.
Any idea how to do this for multiple tables in one database?
Below is an example of the tables I'm looking at.
Note that SQLite does not support a formal date type. Instead, you can consider storing the date portion of the column as text, using text for the site name as well. Assuming you already had a Date and SiteName column in your table you could try the following:
UPDATE yourTable
SET
Date = SUBSTR(SampleName, 1, 8),
SiteName = SUBSTR(SampleName, 10, 3)
In most other databases, you could then also drop the SampleName column if it no longer served a purpose. But this isn't possible in SQLite; the closest thing would be to drop the table and recreate it all over again withou the SampleName column.
Related
I'm trying to write a table to an Oracle database using the ROracle package. This works fine, however all of the numeric values are showing the full floating point decimal representation on the database. For instance, 7581.24 shows up as 7581.2399999999998.
Is there a way of specifying the number of digits to be stored after the decimal point when writing the table?
I found a work around using Allan's solution here, but it would be better not to have to change the variable after writing it to the database.
Currently I write the table with code like this:
dbWriteTable(db_connection, "TABLE_NAME", table, overwrite = TRUE)
Thanks in advance.
It's not elegant but maybe good programming to make the types and precisions explicit. I did it with something like:
if (dbExistsTable(con, "TABLE_NAME")) dbRemoveTable(con, "TABLE_NAME")
create_table <- "create table CAMS_CFDETT_2019_AA(
ID VARCHAR2(100),
VALUE NUMBER(6,2)
)"
dbGetQuery(con_maps, create_table)
ins_str <- "insert into TABLE_NAME values(:1, :2)"
dbGetQuery(con, ins_str, df)
dbCommit(con)
Essentially, it creates the table and specifies the types for each column and the precision. Then it fills in the values with those from the dataframe (df) in R. You just have to be careful that everything matches up in terms of the columns. If you assign a number to oracle with precision 2 (VALUE NUMBER(3,2) and then push a value from R with more decimals, it will round it to the assigned precision (2 in this example). It will not truncate it. So df$value = 3.1415 in R would become VALUE 3.14 in the Oracle table.
I have a table t with around 500,000 rows. One of the columns (stringtext) contains a very long string and I have now discovered that that there are in fact only 80 distinct strings. I'd like to declutter table t by moving the strings into a separate table, s, and merely referencing them in t.
I have created a separate table of the long strings, including what is effectively an explicit row-index number using:
CREATE TEMPORARY TABLE stmp AS
SELECT DISTINCT
stringtext
FROM t;
CREATE TABLE s AS
SELECT _ROWID_ AS stringindex, stringtext
FROM stmp;
(It was creating this table that showed me there were only a few distinct strings).
How can I now replace stringtext in t with the corresponding stringindex from s?
I would think about something like Update t set stringtext = (select stringindex from s where s.stringtext = t.stringtext) and would recommend first making an index on s(stringtext) as SQLite might not be smart enough to build a temporary index. And then a VACUUMing would be in order.
Untested.
I am changing from holding data inside Rstudio to instead keeping it in SQL and importing tables as I need them.
The issue I keep running into is that when I import the table from SQL, I have a data frame that now needs to be converted to XTS, but the index is now an auto integer and not the first column which contains what should be my index.
Is there a way to just specify a new column as the index?
I assume there has to be but I can't find anything.
Data
Figured this out rather quickly after posting the question.
#Specified the row name as the index using rownames()
rownames(temp_from_sql) <- temp_from_sql$`Date Time`
#deleted the 'Date Time' column
temp <- temp_from_sql[,-1]
I have a project in SQLite which contains a table
Prefix table
prefix id
T6A-T6Z 1
YAA-YAZ 2
ZAA-ZAZ 3
7RA-7RZ 4
7TA-7YZ 5
For example I have the value “T6C” which falls in the range of the first record. I need the id of that record. I look into REGEXP as a possible solution but what I read I need a callback function This app is being developed in Adobe Air and I could not find a way to implement the callback.
I also tried the wildcard '_' approach but came up short on that.
Any help would be great.
This is a lousy data format. You should really have the beginning and ending values in separate fields. Oh well. You can do this with string manipulations:
select *
from prefix
where 'T6C' between substr(prefix, 1, 3) and substr(prefix, -3, 3)
If your prefix ranges followed the simple pattern of your first four (1-4) example id's, it would be a simple LIKE query using the first two characters of your prefix (range) beginning:
SELECT id FROM table WHERE prefix LIKE 'T6%';
But your fifth (5) example id has a prefix range that spans beyond the expected -7TZ range ending convention that the other four represented. If you have design control of the the prefix ranges then LIKE is another alternative.
I have a column in my sqlite table which is string and has the following format
2011-09-06 18:34:55.863414
You can see that it identifies date and time. I'd like to construct a query that will
delete all records that are older than certain date and time.
Is this possible?
Since your date is already in the best format (largest time-period values to smallest)
DELETE FROM myTable
WHERE myDateField < '2011-09-06 18:34:55.863414'
BTW -- dates are strings in sqllite, AFAIK (which is why the format matters -- biggest values to smallest, so it works alphabetically too). IF you want to treat them as dates, you can use functions. Some good examples here: http://sqlite.org/lang_datefunc.html
DELETE FROM tablename WHERE columnname < '2011-09-06 18:34:55.863414'
See:
http://www.sqlite.org/lang_datefunc.html