How to access SQLite table programmatically (convert string to table name) - sql

I have a simple sqlite database with several tables like this
schedule_20140824
schedule_20140825
schedule_20140826
...
How do I generate a select statement into a table for given a date?
For example, let's say today is the 26th and I wanted my app to access today's schedule table. What is the correct way to do the following?
SELECT * FROM 'schedule_'||strftime('%Y%m%d','now');
Any advice would be appreciated. Thanks!

SQLite itself cannot generate SQL dynamically.
You have to do this in your programming language:
db.execute("SELECT * FROM schedule_" + date)

Related

How to find a specific table from my database with an sql script

i'm a total beginner with sql / data base but i'm trying to learn on a fake data base.
My problem is that i want to find a specific Table from a Schemas and i only know that in the table name there's "CRH".
I've been searching on every tutorial but none of them seems to be working i'm probably doing something wrong like i don't have the right dependencies.I'm using data beaver and i didn't modified it after the download.
here's my code :
SELECT * FROM SCHEMAS_NAME WHERE name LIKE '%CRH%'
You are using the Oracle 12c DBMS so try this:
SELECT table_name FROM all_tables
WHERE table_name LIKE '%CRH%';
Assuming your database is MS SQL Server: Try following code.
select * from sys.all_objects where type = 'U' and name like '%CRH%'

T-SQL: Exclude Columns from a SELECT statement based on string

My overall goal is to create new tables by selecting columns in existing tables with certain patterns/tags in their column names. This is in SQL Server.
For example, I have a common need to get all contact information out of a company table, and into its own contact table.
I haven't been able to find a programmatic approach in SQL to express excluding columns from a SELECT statement based on string.
When looking to options like the COL_NAME function, those require an ID arg, which kills that option for me.
Wishing there was something built in that could work like the following:
SELECT *
FROM TABLE
WHERE COL_NAME() LIKE 'FLAG%'
Any ideas? Open to anything! Thanks!!
One trick could be to firstly using the following code to get the column names you desire:
select * from information_schema.columns
where table_name='tbl' and column_name like 'FLAG%'
Then concatenate them into a comma-delimited string and finally create a dynamic sql query using the created string as the column names.

Need to select all tables from a database in SQL Server where the most recent date-timestamp is from a year or more ago

I'm going through all tables in a database trying to determine which tables are old (have not been altered in a long time). I've been going through and flagging all tables with old DTS's as "old"
Is there a more efficient way to do this? Can I run a statement that scans all tables in a database for date-timestamp fields and then looks at the most recent ones?
Thank you in advance for any help you can provide!
You can use the INFORMATION_SCHEMA.COLUMNS view to retrieve all the tables having the timestamp column (assuming the column name is known and not used for other columns).
Use these to generate Dynamic SQL of the form
SELECT COUNT(*) ,#TableName FROM #TableName WHERE #TimeStampColumn < #TimestampToCheck
The tables where count(*) is 0 are the ones you need to look at
I would do something like this:
Show a COUNT(*) of rows written in the last year... if you get 0, you know the table isn't in use.
Use the information_schema to get the columns/tables.
SELECT 'SELECT COUNT(*) AS ['+TABLE_NAME+'] FROM ['+TABLE_CATALOG+'].['+TABLE_SCHEMA+'].['+TABLE_NAME+'] WHERE '+COLUMN_NAME+' > DATEADD(YY,-1,CONVERT(DATETIME,FLOOR(CONVERT(FLOAT,GETDATE()))))'
-- SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE DATA_TYPE='DATETIME'
--AND (COLUMN_DEFAULT='(getdate())' OR COLUMN_DEFAULT='CURRENT_TIMESTAMP')
You can add the last line in, if you only want columns with a default value.
Then take the output, copy to a new window, and run it!

Find Dates - SQL

I have the following from and to dates and need to list using a dynamic sql query only the 3 dates that are mentioned below in netezza database
From To
20120603 20120831
Required Result:
20120702
20120703
20120801
Is there a possibility using dynamic SQL query for netezza that dates in a user specified range can be generated.
I'm not sue I understand your question, but I think you want
WHERE From IS NULL
u can try something like
select * from table where date in (20120702,20120703,20120801)

exporting by year and month from 100 tables

I've been asked to export data out of 100 tables.
for each table it would be something like this:
select * from table1
where datepart(yyyy,table1.DOS)=2011
and datepart(mm,table1.DOS)=01
and then:
select * from table1
where datepart(yyyy,table1.DOS)=2011
and datepart(mm,table1.DOS)=02
etc...
i would need to do this for every YEAR and for every MONTH for every table
i need to export these data sets to a CSV
can you please give me some guidance on how i can automate this instead of using the IMPORT/EXPORT wizard manually?
Dynamic SQL with sp_msforeachtable. Here is a link with info and examples:
http://weblogs.asp.net/nunogomes/archive/2008/08/19/sql-server-undocumented-stored-procedure-sp-msforeachtable.aspx
My Suggestion - Window Services help you out by setting the timing(Scheduling) in Application Configuration file