DateAdd function in Sqlite server with column name as input - sqlite

select DATE(Orders.OrderDate,'10 DAY') from Orders. it will give the following result "1996-07-14"
When using column name directly instead of value in numeric it will give the empty result
select DATE(Orders.OrderDate,'Orders.OrderId DAY') from Orders.
What wrong with the above select query?

Try concatenating a string using the OrderId column:
SELECT DATE(Orders.OrderDate, '+' || Orders.OrderId || ' DAYS')
FROM Orders
You want a query of the form
SELECT DATE(Orders.OrderDate, '+7 DAYS') FROM Orders
and string concatenation will let you achieve that.

Related

select the union of several tables together in a single step

I am very new to Oracle 11g and am trying to generate a large string by appending text for each column in a select statement and using a cursor to store the results. However I want the last statement to not have a union all included. The final result I want to build large string of each row generated or simply execute the result if possible.
Note: column1 has a list of schemas that I am interested in.
select 'select * from ' || column1 || '.' || column2 || ' union all ' from mytable
This is where column1 is the schema, column2 is the table name.
What is the simplest way to generate the final string without using rtrim to remove the last string. And is there a simple way to append all these rows together in the string automatically?
The final goal is to actually just execute the union into a resulting cursor.
If you're querying in a loop anyway I wouldn't try to construct the string as part of the select at all; I'd do it all within the loop. Something like (untested):
declare
str varchar2(32768);
begin
for rec in (select column1, column2, rownum as rn from mytable)
loop
if rec.rn > 1 then
str := str || ' union all ';
end if;
str := str || 'select * from "' || rec.column[ || '"."' || rec.column2 ||'"';
end loop;
-- do something with str e.g. display to verify the syntax
-- before using in a cursor
dbms_output.put_line(str);
end;
Rather than adding union all to the end of every row except the last one,the rn check means it's added to the start of every row except the first one, which is easier to detect.
I've also wrapped the schema and table names in double quotes, just in case you have to deal with any quoted identifiers. But if your stored values don't match the case of the owners and table names in all_tables this will cause a problem rather than solve it.

SQLite: Selecting the maximum corresponding value

I have a table with three columns as follows:
id INTEGER name TEXT value REAL
How can I select the value at the maximum id?
Get the records with the largest IDs first, then stop after the first record:
SELECT * FROM MyTable ORDER BY id DESC LIMIT 1
Just like the mysql, you can use MAX()
e.g. SELECT MAX(id) AS member_id, name, value FROM YOUR_TABLE_NAME
Try this:
SELECT value FROM table WHERE id==(SELECT max(id) FROM table));
If you want to know the query syntax :
String query = "SELECT MAX(id) AS max_id FROM mytable";

Sqlite Query replacing a column with a column from another table

I have 2 tables, one is indexing the other.
I am querying Table#1, and it has one column (string) that has an ID in it that corresponds to a unique row in Table#2. Im trying to write a query in Sqlite that allows me to retrieve the value from Table#2 if the column value in Table#1 is not an empty string.
Kinda like:
"SELECT TMake,TModel,TTrim,IYear,[%q] AS TPart1 FROM AppGuide WHERE TPart1 != ''"
But instead of retrieving the Index value (TPart1) Id like to get the string from Table#2.
Is this possible?
Any help is appreciated.
You could use a correlated subquery:
SELECT TMake,
TModel,
...,
(SELECT stringvalue
FROM Table2
WHERE Table2.ID = Table1.TPart1)
FROM Table1
WHERE Table1.TPart1 != ''
However, these are rather slow to execute, so you'd better use a join (this returns exactly the same result):
SELECT Table1.TMake,
Table1.TModel,
...,
Table2.stringvalue
FROM Table1 LEFT JOIN Table2 ON Table1.TPart1 = Table2.ID
WHERE Table1.TPart1 != ''
If you don't want to get records from Table1 that have no matching Table2 record, drop the LEFT.

SQLite: Combine Date Column & Time Column

i simply have two fields. dtStartTime and dtStartDate.
I want to do a query now which returns one combined field dtStart using SQLite
I have tried
SELECT (dtStartDate+dtStartTime) as dtStart1, from ...
but it returns wrong values...
Thank you, shorty
PS: Dates are stored as unixepoch
SELECT datetime(d, t)
FROM (
SELECT date('now') as d, time('now') as t) as dt;
probably:
SELECT DATETIME(DATE(dtStartDate) || ' ' || TIME(dtStartTime)) FROM YourTable;

SQLite, how to use expression in datetime modifier

In short: I need to calculate the ending datetime given starting time and length in minutes.
I have a table with columns StartTime (type datetime) and LengthMinutes (type int). To calculate the ending time I would need some sql like this:
select datetime(StartTime, '+LengthMinutes minutes') from my_table;
How do I refer to the column LengthMinutes from within the modifier?
Edited: solved using dan04's suggestion. Thanks!
SELECT datetime(StartTime, '+' || LengthMinutes || ' minutes') FROM my_table;
select datetime(strftime('%s',StartTime)+lengthMinutes*60,'unixepoch') from my_table;

Resources