Function to substract 2 dates in sql with PostgreSQL - sql

I need a way to substract to dates and get the result in days in sql.
I'm using PostgreSQL 8.4

When both columns you want to subtract are of date type, you can just use the - operator.
Take a look at the documentation for date functions, in particular table 9-25.

Check here the documentation of PostgreSQl...
postgresql.org/docs/8.4
I copied it from this code from this website:
age(timestamp '2001-04-10', timestamp '1957-06-13')
hope it will help you...

Related

Function to convert char(255) non standard dates into timestamps

I have database with a column of date strings that are in the format YYYY-MM-DDThh:mm:ss.3nZ
I would like to transform them into TIMESTAMP so that I can then apply the BETWEEN function to identify rows that fall in a given time window (this being the ultimate goal of the exercise).
I can't just change the nature of the column unfortunately.
What is the best way to achieve this?
Thank you
Try this:
SELECT
to_timestamp('2021-01-28T10:26:32.359Z', 'YYYY-MM-DD"T"HH:MI:SS.FF3"Z"') t
FROM
dual;

Substract the hour from the timestamp.( hour is part of the timestamp column)

Need some help to perform this in Hive .
I do have timestamp like "2019-03-11T18:23:49-04:00"
How to I subtract the hour and minutes from the above timestamp.( -04:00)
The hour component may vary based on the timezone.
Thanks in advance.
Looks like you can't do this as easily as you can in MSSQL with dateadd. Some very good suggestions are made here. I also tested this and it works as it should.
select from_unixtime(unix_timestamp('2015-12-12 16:15:17')+8500)
I have modified this to work with your exact timestamp format
select from_unixtime(UNIX_TIMESTAMP(substring(translate("2019-03-11T18:23:49-04:00",'T',' '),1,19))+8500)

SQL - subtract between two dates

I'm trying to find the number of days between two date columns.
I tried to use DATEDIFF but I got an error. What more should I do
Thanks,
Are you sure you are using the correct SQL syntax for your database? Since you're using MySQL, you need to do a
SELECT DATEDIFF('2015-06-05', '2015-08-05');
and the difference is always expressed in days.
On SQL Server you need to specify the unit e.g.
SELECT DATEDIFF(day, '2015-06-05', '2015-08-05');

SQL query to convert Date to another format

Thanks for your help. I am not able to make out the type/format of the "Value" in a Date column.I guess its in Julian Date format.
The Column is paid_month and the values are below.
200901
200902
So,please help in writing SQL query to convert the above values(Mostly in Julian Format) in the Date Column to normal date (MM/DD/YYYY) .
Thanks
Rohit
Hi,
I am sorry for missing in giving the whole information.
1)Its a Oracle Database.
2)The column given is Paid_Month with values 200901,200902
3)I am also confused that the above value gives month & year.Day isnt given if my guess is right.
4)If its not in Julian format ,then also please help me the SQL to get at least mm/yyyy
I am using a Oracle DB and running the query
THANKS i GOT THE ANSWER.
**Now,i have to do the reverse meaning converting a date 01/09/2010 to a String which has 6 digits.
Pls help with syntax-
select to_char(01/01/2010,**
It looks like YYYYMM - depending on your database variant, try STR_TO_DATE(paid_month, 'YYYYMM'), then format that.
Note: MM/DD/YYYY is not "normal" format - only Americans use it. The rest of the world uses DD/MM/YYYY
For MySQL check
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
Example:
SELECT DATE_FORMAT(NOW(), '%d/%m/%Y')
For MySQL, you would use the STR_TO_DATE function, see http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date
SELECT STR_TO_DATE(paid_month,'%Y%m');
Sounds like the column contains some normal dates and some YYYYMM dates. If the goal is to update the entire column, you can attempt to isolate the YYYYMM dates and update only those. Something like:
UPDATE YourTable
SET paid_month = DATE_FORMAT(STR_TO_DATE(paid_month, '%Y%m'), '%m/%d/%Y')
WHERE LENGTH(paid_month) = 6
SELECT (paid_month % 100) + "/01/" + (paid_month/100) AS paid_day
FROM tbl;
I'm not sure about how oracle concatenates strings. Often, you see || in SQL:
SELECT foo || bar FROM ...
or functions:
SELECT cat (foo, bar) FROM ...

Using DAY(), WEEK(), and YEAR() at one query

i using MySQL Query for my task.
And I interested using Date and time function.
can i use DAY(), WEEK(), and YEAR() at one query?
SELECT Object
FROM table
WHERE DAY(date) BETWEEN 1 AND 7
GROUP BY WEEK(date, 1), YEAR(date)
i want do this bcoz i'm worry if sometimes my program have an error because of the date setting and not recognize some date.please give me an input.
Yes, you can use them all in a single query.
The only disadvantage I can think of is that using any of the DAY, WEEK or YEAR functions won't be able to use the index on the column the function is applied to, assuming one is present.
If you're having issues relating to date formatting, you should get familiar with:
DATE_FORMAT
STR_TO_DATE