How can I get the last day of a month in H2 SQL? In MySQL the following would work:
SELECT LAST_DAY(GETDATE())
Thank you.
EDIT:
Ended up with using the following:
SELECT TIMESTAMPADD(DAY, -DAY(TIMESTAMPADD(MONTH,1,GETDATE())), TIMESTAMPADD(MONTH,1,GETDATE()));
The reason for this is that it also support MySQL. Just replaced the functions from Vijaykumar's answer.
please try :
SELECT DATEADD(dd, -DAY(DATEADD(m,1,#Today)), DATEADD(m,1,#Today))
SELECT day(dateadd(dd,-day(ym_next),ym_next)) last_day_of_month
FROM (SELECT DATEADD(m,1,ym) ym_next
FROM (SELECT parsedatetime(concat(2016,'-',2,'-1'),'yyyy-MM-dd') ym)ym)ym
Related
I am looking for help to convert below sql query into hive supported date format. kindly assist.
GP: SQL
select to_date('19800302000000','yyyymmddhh24miss') date_of_birth
GP Output : 1980-03-02
GP query :
extract(year from age(current_date-1, to_date(b.birthday,'yyyymmddhh24miss'))) age
we are looking similar out in hive. please help us.
For select to_date('19800302000000','yyyymmddhh24miss') use this
select from_unixtime(unix_timestamp('19800302000000','yyyyMMddhhmmSS')).
If you dont want time part, use this
select to_date(from_unixtime(unix_timestamp('19800302000000','yyyyMMddhhmmSS'))).
For extract(year from age(current_date-1, to_date(b.birthday,'yyyymmddhh24miss'))) age
use below code. it should give difference of years between yesterday and DOB.
select
year(current_date() - interval 1 day ) -
year(from_unixtime(unix_timestamp('19800302000000','yyyyMMddhhmmSS'))) age
I have a set of dates that are in the format DD-MMM-YYYY. I need to be able to compare dates by using only the DD-MMM part of the date, since the year isn't important.
How would I achieve this?
I have tried reading up on the DATEPART function (edit: which evidently wouldn't work) but I can only theoretically get that to return either the DD or the MMM parts, not both of them at once.
Edit: added oracle tag. Sorry.
Example of date field: 01-MAR-1994
If your column is of type DATE then it doesn't have a format.
If I understand you right, then you want to view the mon-dd part only, so you need to convert it with TO_CHAR function,
i.e.:
select to_char(your_date_column, 'mon-dd') from your_table
Convert your dates using the following format, it will only month and the date part. You have to replace getdate() with you date fields.:
select convert(varchar(5),getdate(),110)
Assuming that you are using SQL Server or Oracle since you attempted using DATEPART, you can just get the day and month using the DAY() and MONTH() functions. Assuming, again, that the dates you are comparing are in two different tables, it would look similar to this:
SELECT MONTH(t1.date), DAY(t2.date)
FROM table AS t1
INNER JOIN table2 AS t2
ON t1.key = t2.key
WHERE MONTH(t1.date) = MONTH(t2.date)
AND DAY(t1.date) = DAY(t2.date)
EDIT: If you are just comparing rows in the same table, you only need a very simple query.
SQLFiddle
select id, TO_CHAR(most_recent, 'mon-dd')
from (
select id, MAX(date1) AS most_recent
from table1
group by id
)
You can also combine month and day into one integer:
EXTRACT(MONTH FROM datecol) * 100 + EXTRACT(DAY FROM datecol) AS MonthDay
Then it's easier to sort and compare.
select FORMAT(yourcoulmn_name, 'dd/MM') from table
This should do the trick
`select CONVERT(varchar(7),datetime_column,100) from your_table`
date_default_timezone_set("Asia/Kolkata");
$m = date("m");//Month
$d = date("d");//Day
$sql = "SELECT * FROM contactdata WHERE MONTH(date) = '$m' AND DAY(date) = '$d' ";
only checks day and month and returns today, day and month from database
SELECT LEFT(REPLACE(CONVERT(varchar(10),GETDATE()-1,3),'/',''),4)
WOuld this work for you?
FROMAT(DATETIME, 'dd-MMM') = FROMAT(DATETIME, 'dd-MMM') use any format you want
I'm using sql server 2008. How to query out a data which is the date is today and 7 days before today ?
Try this way:
select * from tab
where DateCol between DateAdd(DD,-7,GETDATE() ) and GETDATE()
Query in Parado's answer is correct, if you want to use MySql too instead GETDATE() you must use (because you've tagged this question with Sql server and Mysql):
select * from tab
where DateCol between adddate(now(),-7) and now()
I'm right now creating View and I need to display only the YEAR from a normal DateTime
How can I do that to return just a Year as a value from ?
I have already tried it with EXTRACT but somehow it not working ..
Thanks for help and fast answer
Use YEAR() function
SELECT YEAR(MyDateCol) FROM MyTable
See this SQLFiddle
You can use DATEPART() to extract the year from datetime value.
DATEPART(YEAR,GETDATE());
Check out SQLFIDDLE
Year function
SELECT YEAR(getdate())
SELECT YEAR(columnname) from yourtablename
Suppose I have a date 2010-07-29. Now I would like to check the result of one day ahead. how to do that
For example,
SELECT *
from table
where date = date("2010-07-29")
How to do one day before without changing the string "2010-07-29"?
I searched and get some suggestion from web and I tried
SELECT *
from table
where date = (date("2010-07-29") - 1 Day)
but failed.
MySQL
SELECT *
FROM TABLE t
WHERE t.date BETWEEN DATE_SUB('2010-07-29', INTERVAL 1 DAY)
AND '2010-07-29'
Change DATE_SUB to DATE_ADD if you want to add a day (and reverse the BETWEEN parameters).
SQL Server
SELECT *
FROM TABLE t
WHERE t.date BETWEEN DATEADD(dd, -1, '2010-07-29')
AND '2010-07-29'
Oracle
SELECT *
FROM TABLE t
WHERE t.date BETWEEN TO_DATE('2010-07-29', 'YYYY-MM-DD') - 1
AND TO_DATE('2010-07-29', 'YYYY-MM-DD')
I used BETWEEN because the date column is likely DATETIME (on MySQL & SQL Server, vs DATE on Oracle), which includes the time portion so equals means the value has to equal exactly. These queries give you the span of a day.
If you're using Oracle, you can use the + and - operators to add a number of days to a date.
http://psoug.org/reference/date_func.html
Example:
SELECT SYSDATE + 1 FROM dual;
Will yield tomorrow's date.
If you're not using Oracle, please tell use what you ARE using so we can give better answers. This sort of thing depends on the database you are using. It will NOT be the same across different databases.
Depends of the DateTime Functions available on the RDBMS
For Mysql you can try:
mysql> SELECT DATE_ADD('1997-12-31',
-> INTERVAL 1 DAY);
mysql> SELECT DATE_SUB('1998-01-02', INTERVAL 31 DAY);
-> '1997-12-02'
If youre using MSSQL, you're looking for DateAdd() I'm a little fuzzy on the syntax, but its something like:
Select * //not really, call out your columns
From [table]
Where date = DateAdd(dd, -1, "2010-07-29",)
Edit: This syntax should be correct: it has been updated in response to a comment.
I may have the specific parameters in the wrong order, but that should get you there.
In PL SQL : select sysdate+1 from dual;