convert date to integer in postgresql - sql

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

Related

how to change date format to just year and month but must stay as a date value not varchar, or char..?

I read other similar question where the answer is to use FORMAT(getdate(),'yyy-MM') or something similar. However the problem for me in using anything like this, is that it changes the date type to a varchar or char. I need it to stay as datetype but just want Year and Month.. I tried the following..-> FORMAT(a.completeddate,'yyy-MM') which works to change to year and month but the date is no longer a datetype or date format. So when I try to do the following -> select #FirstCompletion = (A.completeddate) i get this error..Conversion failed when converting date and/or time from character string. Basically I need to convert the date column to year and month as date format so I can then pass values to variables using select #FirstCompletion = (A.completeddate) and set #secondMonth = DATEADD(month, 2, #FirstCompletion) which are Datetype variables.. Would appreciate any help I can get.. Thanks..

Convert INT INTO DATE to calculate AGE

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

Date difference in days in postgresql

I am trying to calculate the difference between dates in days.
Datatype is Text for columns snapshot_date and date_opened.I am getting an ERROR: function date_part(unknown, integer) does not exist
SELECT DATE_PART('day', snapshot_date::date -date_opened::date)::number from my_table
As documented in the manual subtracting one date from the other returns an integer representing the number of days, so:
snapshot_date::date - date_opened::date
is all you need.
This assumes that both columns can safely be cast to a date.

How to take differece between 2 dates of different format in SQL

I have a table with a LOAD_STRT_DTM colum. This is a date column and values are like this - 18-JUL-14 08.20.34.000000000 AM.
I want to find the data which came before 5 days.
My logic is -
Select * from Table where 24 *(To_DATE(Sysdate,'DD-MM-YY') - To_DATE(LOAD_STRT_DTM,'DD-MM-YY')) >120
The issue is -
Select (To_DATE(Sysdate,'DD-MM-YY') - To_DATE(LOAD_STRT_DTM,'DD-MM-YY')) from table
This query should give the NumberOfDays between two dates. But this is not working, I Doubt, the issue is because of the format of the LOAD_STRT_DTM colum.
Please let me know where i am doint it wrong.
If your column is DATE datatype everything is ok, just shoot an:
select * from table where LOAD_STRT_DTM > sysdate - 5;
No need to convert dates to DATE datatype.
(To_DATE(Sysdate,'DD-MM-YY') - To_DATE(LOAD_STRT_DTM,'DD-MM-YY'))
You don't have to convert a DATE into a DATE again. IT is already a DATE. You just need to use it for date calculations. You use TO_DATE to convert a STRING into a DATE.
For example, if you have a string value like '18-JUL-14', then you would need to convert it into date using TO_DATE. Since your column is DATE data type, you just need to use as it is.
This is a date column
I want to find the data which came before 5 days.
Simply use the filter predicate as:
WHERE load_strt_dtm > SYSDATE - 5;
NOTE : SYSDATE has both date and time elements, so it will filter based on the time too. If you want to use only the date part in the filter criteria, then you could use TRUNC. IT would truncate the time element.
I have answered a similar question, have a look at this https://stackoverflow.com/a/29005418/3989608
It looks like LOAD_STRT_DTM is a TIMESTAMP rather than a DATE, given the number of decimal points following the seconds. The only thing you have to be cautious about is that Oracle will convert a DATE to a TIMESTAMP implicitly where one of the operands is a TIMESTAMP. So the solution
WHERE load_strt_dtm > SYSDATE - 5
will work; as will
WHERE load_strt_dtm + 5 > SYSDATE
but the following will not:
WHERE SYSDATE - load_start_dtm < 5
the reason being that TIMESTAMP arithmetic produces an INTERVAL rather than a NUMBER.
first convert two dates to same format select datediff(dd,convert(varchar(20),'2015-01-01',112),convert(varchar(20),'01-10-2015',112))

check for dates syntax - teradata SQL

I am trying to check for dates but after running the query below, it displays no result. Could someone recommend me the correct syntax?
SELECT TOP 10 * FROM MY_DATABASE.AGREEMENT
WHERE end_dt=12/31/9999
12/31/9999 might look like a date for you but for the database it's a calculation:
12 divided by 31 divided by 9999 and because this involves INTEGER division this results in an INTEGER 0
So finally you compare a DATE to an INT and this results in typecasting the DATE to a INT.
The only reliable way to write a date literal in Teradata is DATE followed by a string with a YYYY-MM-DD format:
DATE '9999-12-31'
Similar for TIME '12:34:56.1' and TIMESTAMP '2014-08-20 12:34:56.1'
Is it a date column? Then try where end_dt = '9999-12-31'.
The question you ask is not very clear. The date you specify is language dependent.
Try
SELECT TOP 10 * FROM MY_DATABASE.AGREEMENT WHERE end_dt='99991231'