I have a problem with a query on PostgreSQL. I have a case for school where I need calculate the age of a person when he register for school on 1st Septembre of 2005. In a file I have their date of birth and only the YEAR of school registration.
The year registration is a int type ('2005') but I need to convert it to 01-09-2005 (DD-MM-YYYY) which is the start of the school year. How can I do this ?
Should I convert it necessary into a date type if I want to calculate the difference between the date of registration and the date of birth ?
If yes, how can I convert it in order for the date of birth to be on the date type and have a format '01-09-2005'.
I have an answer but that's not the way I should do it :
SELECT AGE('01/09/2005', auditeur.date_nais)
FROM auditeur
JOIN inscription ON auditeur.id_auditeur = inscription.id_auditeur
What I want is to replace the '01/09/2005' with annee ('2005') which is an int type.
That's how you get the desired date from an integer:
(2005 || '-09-01')::date
The concatenation force-converts 2005 to text. Use ISO 8601 format YYYY-MM-DD. Then the cast to date does not depend on the current datestyle setting.
SELECT age((i.annee || '-09-01')::date, a.date_nais) AS age_at_registration
FROM auditeur a
JOIN inscription i USING (id_auditeur)
WHERE a.id = 123; -- select person somehow
Related:
How to concatenate columns in a Postgres SELECT?
Selecting records between two timestamps
There is no need to generate a string to then convert to a date. You have 3 integers: annee from a table and constant values 09 for month and 01 for day. With these just use the make_date function which takes 3 integers and returns a date.
select age( make_date (annee,09,01), auditeur.date_nais)
from auditeur
join inscription
on auditeur.id_auditeur = inscription.id_auditeur
Related
I Want to extract data from a table for monthwise in oracle sql.
I tried it as follow,
SELECT * From customer ct
Where ct. Application_signed _date in (Date '31-12-2020, Date '01-10-2020')
But i am unable to Fetch the data for this range, whereas data is available for this months.
The correct format for a date in Oracle -- and almost everywhere else -- is YYYY-MM-DD:
select *
From customer ct
where ct.Application_signed_date in (Date '2020-12-31, Date '2020-10-01')
Note that Oracle dates can have time components. If that is possible in your data, then one fix is:
where trunc(ct.Application_signed_date) in (Date '2020-12-31, Date '2020-10-01')
Below query works fine with the recent dates (for e.g. if the date is 20-JUN-16, 30-SEP-19). But if I query with date value set to 29-MAR-80, 01-JAN-94 it does not return any result. Wondering what could be the reason. Am not allowed to change the existing year format to YYYY.
We are on Oracle 19c.
select * from V_IV_MUC where TRUNC(VALUE_TIMESTAMP) = '01-FEB-80';
Here, your date '01-FEB-80' will be converted to '01-FEB-1980', if your two digit year format is RR(in NLS_DATE_FORMAT) and it will be converted to '01-FEB-2080' if your date format is set to YY(in NLS_DATE_FORMAT).
YY -- convert the year to current century year - 80 --> 2080
RR -- convert the year to the year based on current year (1950-2049) so 80 --> 1980.
You can use any of the following two queries to avoid any such dependencies on database settings.
-- use date literal
select * from V_IV_MUC where TRUNC(VALUE_TIMESTAMP) = date '1980-02-01';
-- use date string with format
select * from V_IV_MUC where TRUNC(VALUE_TIMESTAMP) = to_date('01-FEB-80','dd-mon-rr');
I'm trying to convert a date (date type) into int. This int should be something like the number of days since the 1 January 1900. How to get this in postgresql? In excel I'm getting this automatically when i concatenate a date with a string.
Example : 2011/11/01 convert into int as 36831
Simply subtract the two dates:
select date '2011-11-01' - date '1900-01-01'
the result will be the number of days.
More details in the manual:
http://www.postgresql.org/docs/current/static/functions-datetime.html
Can someone explain to me why when I perform a LIKE select in SQL (T-SQL) on a varchar column I can do the following:
SELECT *
FROM Table
WHERE Name LIKE 'Th%'
to get names beginning with Th, but when I do the same on a datetime column I need a % before the year, like:
SELECT *
FROM Table
WHERE Date LIKE '%2013%'
to get dates in 2013. The datetimes are stored in yyyy-MM-dd hh:mm:ss format. I know I could use a DATEPART style query but I was just interested in why I need the extra % here.
The DATETIME is converted to a VARCHAR before the comparison, and there definitely is no guarantee that the conversion will be in the pattern you mention. DATETIME is not stored internally as a VARCHAR but as a FLOAT.
You should stop wondering because the syntax is not useful.
SELECT *
FROM Table
WHERE Date LIKE '%2013%'
Will give you a full table scan because the date will be converted to a varchar when comparing. In other words, don't do it !
Use this syntax instead:
SELECT *
FROM Table
WHERE Date >= '2013-01-01T00:00:00'
and Date < '2014-01-01T00:00:00'
If the Date field is in timestamp:-
SELECT *
FROM Table
WHERE year(Date) = '2013'
The sql server converts datetime to this format (Jan 1, 1900 9:20AM.)Because of that reason We need to use an extra %.
If you want to search the records start with month Jan
you can use following query for date time
SELECT *
FROM Table
WHERE Date LIKE 'Jan%'.
No need of extra '%'.
I have a table student with the following columns:
no - integer
name - string
startdate - date
enddate - date.
Date format is MM/DD/YYYY.
I will give a date as input. Now I need a query the inputdate which found in between the start and end date.
For an example I will give 04/14/2012, then the query should return the 1st record as in the figure.
(because input date (04/14/2012) is found in between the 04/10/2012 to 04/20/2012)
Please help me.
The issue you are having is caused by your assumption that sqlite has a date/datetime type when in fact it doesn't.
I suggest you read the following http://www.sqlite.org/datatype3.html to have a better understanding of sqlite types.
The dates in the MM/DD/YYYY format are handled as TEXT by sqlite, and so those dates are compared as strings. For example, 02/01/2012 is considered bigger than 01/02/2012by sqlite if compared directly.
You will need to transform those dates to a format that can be string-compared. Here is an example:
sqlite> create table foo (d TEXT);
sqlite> insert into foo values ('02/01/2012');
sqlite> select substr(d, 7, 4) || substr(d, 1, 2) || substr(d, 4, 2) from foo;
20120201
You should post what you have tried so far.
There should be a between clause that you can use:
select * from table
where inputdate between startdate and enddate
Dates as a date type in SQLite don't exist. There are a number of approaches to dealing with dates - store them as integer seconds since 1 Jan 1970 (unixepoch) or store them as strings, but if you do, then you really need to store them in 'YYYY-MM-DD' format because that is what the date functions require as input.
Assuming you use the string format in the format I suggested then your query would look something like
SELECT * FROM Table WHERE Date(Inputdate) BETWEEEN Date(startDate) AND Date(EndDate);
(although you may want to format the output of the date columns to US date format with
SELECT Strftime("%m/%d/%Y",startDate) As StartDate ...
If you use seconds since 1970 its somewhat easier because the seconds just compare without needing the convert them to dates, although you still might want to output in US date format, so ...
SELECT Strftime("%m/%d/%Y",startDate) As StartDate ... FROM Table WHERE inputDate BETWEEN startDate and EndDate;
sqlite> select *from tbl_node where mydate between '2014-02-02' and '2014-02-06';
it show the output :-
1|1|123|456|12eb-ab|1|1|254|123|19|2014-02-03 16:00:44
2|1|123|456|12eb-ab|1|1|254|123|19|2014-02-03 16:01:03
3|1|123|456|12eb-ab|1|1|254|123|19|2014-02-03 16:00:57
4|1|123|456|12eb-ab|1|1|254|123|19|2014-02-03 16:00:34
Here mydate is column name in tbl_node;
we can also use from current time , using now.
sqlite> select *from tbl_node where mydate between '2014-02-02' and 'now';