Combine several values from text file to one value in postgresql database - sql

I need to copy the content of a textfile (not csv) into a Postgres/Postgis database with a simple sql script.
The textfile looks like
#date value
2007 11 29 9 29 14.830 4.1
2007 12 7 21 46 25.560 5.3
So the date is compound by 6 values (year, month, day, hour, minute, second). I have one timestamp field in my DB where the date should be stored (as one single value).
I know I can use the COPY command to copy each column value of the file to a column value in the database,
COPY myDb FROM myValues.txt;
but I need to combine several values from the text file to one value in the database.
Sorry if the question has already been asked, I don't really know how to search for this as I just started with sql.

Related

SQL on IBM i - Selecting data based on combined fields from DB

We are trying to select records from a file containing sales data where the date in the IBM i file is stored in 3 separate numeric fields (Year, Month, Day). We have a .net application that is using date pickers to select a from and through date range. Is there a way in an SQL statement to combine the 3 fields from the database into a single value so that the database date can be used to make the SQL select easier?
Use the digits command to tie them together. Your parameters will be YYYYMMDD.
Where digits(year) || digits(month) || digits(day) BETWEEN ? and ?

how to convert a number to date in oracle sql developer

I have a excel format dataset that need to be imported to a table, one column is a date, but the data is stored in number format, such as 41275, when importing data, i tried to choose data format yyyy-mm-dd, it gives an error: not a valid month, also tried MM/DD/YYYY also error: day of month must be between 1 and last day of month. does anyone know what is this number and how can i convert it to a date format when importing it into the database?Thanks!
The expression (with respect to the Excel's leap year bug AmmoQ mentions) you are looking for is:
case
when yourNumberToBeImported <= 59 then date'1899-12-31' + yourNumberToBeImported
else date'1899-12-30' + yourNumberToBeImported
end
Then, you may either
Create a (global) temporary table in your Oracle DB, load your data from the Excel to the table and then reload the data from the temporary table to your target table with the above calculation included.
or you may
Load the data from the Excel to a persistent table in your Oracle DB and create a view over the persistent table which would contain the above calculation.
The number you got is the excel representation of a certain date ...
excel stores a date as the number of days, starting to count at a certain date ... to be precise:
1 = 1-JAN-1900
2 = 2-JAN-1900
...
30 = 30-JAN-1900
so, to get your excel number into an oracle date, you might want to try something like this...
to_date('1899-12-31','yyyy-mm-dd') + 41275

QLIKVIEW Month and Year displayed wrong

I have problem with dates. When I convert the dates from daily basis to monthly and yearly, the tables start with 0 as the first value.
For example, see table below. I loaded the data into Qlikview from a MySQL database and I'm pretty sure I've loaded the data properly to Qlikview. Any help would be appreciated.
year month
0 0
2008 1
2009 2
2010 3
4
5
6
7
8
9
10
11
12
Set up a chart object of type straight table, and add the your dimensions there and expression to it. You will most likely see that missing values are going to get assigned zero to it.
So the answer is, most likely that your data is sometimes missing a date and therefore getting the zero. Look at your table viewer (ctrl-t) and make sure that your table links are correct and eventually coalesce your dates to a default so you won't get null converted to zero.
If this is not the issue, please post your load script.

SQL search date range in part of text field

I have a text field in a SQL database with dates that are generally like 13 Jan 1897 but could be just 1962 or Jan 2013 or ?1956 etc.
I want to be able to do a WHERE on year ranges. eg. from 1945 to 1987.
I realise that it may be difficult to cater for all formats of the data. I would be happy if it just catered for:
13 Feb 1972, and Feb 1972, and 1972
If the only date formats you are concerned about are like those above and you just want the Year it looks like you could just use the last 4 characters of the value.
In SQL Server that would be:
RIGHT(datefield,4)
Or in most languages (including SQL Server):
SUBSTRING(datefield,LEN(datefield)-3,4)
In ANSI SQL:
MID(datefield,LEN(datefield)-3,4)
Given you are using MySQL. If you want to be able to query your table with proper date comparisons, you may want to create a new field with DATETIME type and fill it with DATETIMES generated from your current field. You'd need queries like :
UPDATE _TABLE_ set NEW_DATETIME_FIELD = STR_TO_DATE(_CURRENT_FIELD_, '%Y') where CHAR_LENGTH(_CURRENT_FIELD_) = 4;
The above query would work for records with values in _CURRENT_FILED_ like '1945'. You'd need to run this query for each type of date string you got in the current field.
The new DATETIME field will allow you to properly use MySQL date functions against it.

is it possible to find out how much of the db data is older than some N years in SQL Server?

I have two database in SQL Server. I wanted to find out the data older than (let say 3) years.
I know the database creation date, currently I have around 550 GB (both the database) of data spanned for 7 years, I wanted to know 'how much of the DB data (out of total 550 GB)is older than 3 years OR (5 years)'?
I was going through this link but couldn't get the expected data.
SQL SERVER – Query to find number Rows, Columns, ByteSize for each table in the current database – Find Biggest Table in Database
One of the solution coming in my mind right now is to find out the total number of rows accounted for 7 years (easily get this number), total number of rows accounted for 5 years (starting from the date creation) (don't know how to get this number).
then for row_count_7_years accounts for 550 GB of data , what will be the row_count_5_years? i will get the approx data.
Please Help
For such purposes you should keep some datetime field as marc mentioned. I suppose you don't have it.
In you suggested solution you can get the whole count of rows from your table (for 7 years i suppose), but you wouldn't be able to get the rows for 5 years, because there is no date.
You can get the whole number of records for 7 years and divide them on the number of years, and ONLY IN CASE you have your database avarage fulfill, you can make query for top (numberOFRows in one year)*5 and order them by row_number(). The result - the rows, you should delete. But I wouldn't recommend you to use this solution.
I would recommend you to alter your tables and add the datetime columns for each of them. Before that you should make the backup for the whole date and copy it somewhere. After 3 years you would be able to make your clean up.
as mentioned above u shud have a date column , however if you dont , depending on the realtionships in your tables u might be able to estimate the number of rows looking up realtionships with some other table that has the datetime column , else if you have a backup ( unlikely but still) you can restore that to identify the delta