I am trying to return the number of years someone has been a part of our team based on their join date. However i am getting a invalid minus operation error. The whole getdate() is not my friend so i am sure my syntax is wrong.
Can anyone lend some help?
SELECT
Profile.ID as 'ID',
dateadd(year, -profile.JOIN_DATE, getdate()) as 'Years with Org'
FROM Profile
MySQL Solution
Use the DATE_DIFF function
The DATEDIFF() function returns the time between two dates.
DATEDIFF(date1,date2)
http://www.w3schools.com/sql/func_datediff_mysql.asp
This method only takes the number of days difference. You need to convert to years by dividing by 365. To return an integer, use the FLOOR method.
In your case, it would look like this
SELECT
Profile.ID as 'ID',
(FLOOR(DATEDIFF(profile.JOIN_DATE, getdate()) / 365)) * -1 as 'Years with Org'
FROM Profile
Here's an example fiddle I created
http://sqlfiddle.com/#!9/8dbb6/2/0
MsSQL / SQL Server solution
The DATEDIFF() function returns the time between two dates.
Syntax: DATEDIFF(datepart,startdate,enddate)
It's important to note here, that unlike it's MySql counterpart, the SQL Server version takes in three parameters. For your example, the code looks as follows
SELECT Profile.ID as 'ID',
DATEDIFF(YEAR,Profile.JoinDate,GETDATE()) as difference
FROM Profile
http://www.w3schools.com/sql/func_datediff.asp
Looks like you're using T-SQL? If so, you should use DATEDIFF:
DATEDIFF(year, profile.JOIN_DATE, getdate())
Using the method from this answer, you can get your result with the following query :
SELECT
Profile.ID as 'ID',
YEAR(getdate()) - YEAR(profile.JOIN_DATE) - (DATE_FORMAT(getdate(), '%m%d') < DATE_FORMAT(profile.JOIN_DATE, '%m%d')) as 'Years with Org'
FROM Profile
If this is MS SQL:
SELECT
Profile.ID as 'ID',
DATEDIFF(YEAR,Profile.JoinDate,GETDATE())
FROM Profile
Related
I'm trying to make a query that makes me able to select just the data from the current month from a table, the date field that I'm using as reference is Datetime Type (e.g 2021-02-19 03:23:31), this field is called "Date". My try is this as follow:
Select *
FROM CORP.T_LOGI_RN_Planeacion_Entregas
Where Cast(CORP.T_LOGI_RN_Planeacion_Entregas.Date As Date) >= Cast(Month(getdate()) As Date)
It's not working, (im not pretty sure if I need to use "Cast" Actually) it throws me this error:
Cannot cast type int to date
But using something like this where I'm not specifying the Month, it works:
Select *
FROM CORP.T_LOGI_RN_Planeacion_Entregas
Where Cast(CORP.T_LOGI_RN_Planeacion_Entregas.Date As Date) >= Cast(getdate() As Date)
Any idea?
Thanks by the way and sorry if the solution is so obvious I'm little bit new on this, best regards.
Since you have not specified your DB Product but from your syntax It seems SQL Server. YOu can use MONTH function here -
Select *
FROM CORP.T_LOGI_RN_Planeacion_Entregas
Where MONTH(CORP.T_LOGI_RN_Planeacion_Entregas.Date) = MONTH(GETDATE())
AND YEAR(CORP.T_LOGI_RN_Planeacion_Entregas.Date) = YEAR(GETDATE());
I am trying to build a vb.net application with a where clause to filter data using a DateTime field.
My table contains 5 fields and more than 10000 rows. I wanna use a DateTime field to find all the rows older than 7 years from now.
But this script will be re-used many times. So I don't wanna use this kind of where clause cause I don't wanna need to modify the where clause every time I wanna run the application :
select * from myTable WHERE myDateTimeField < '2006-09-07 00:00:00.000'
I'd like to find a way to write a where clause like this :
select * from myTable WHERE myDateTimeField "is older than 7 years from NOW"
I don't use VB.net very often (as you can see), so this thing is really bugging me
Just make use of DateTime:
Dim dateTime As DateTime
dateTime = DateTime.Now.AddYears(-7);
When you're building your SQL string just call ToString on your date (you can obviously format it however you need):
dateTime.ToString("MM/dd/yyyy");
SELECT * FROM myTable WHERE myDateTimeField < DATEADD(YEAR, -7, GETDATE())
That is if the data in the myDateTimeField is in the same local time zone of the sql server.
If your data is in UTC (which it often should be), then use:
SELECT * FROM myTable WHERE myDateTimeField < DATEADD(YEAR, -7, GETUTCDATE())
i think better would be provided you have SQL Server
strQuery = "select * from myTable WHERE myDateTimeField
<= DATEADD(YEAR, -7, GETDATE())"
What you are looking for is a DSL for datetime. Check out this blog post to get some ideas.
http://leecampbell.blogspot.com/2008/11/how-c-30-helps-you-with-creating-dsl.html
Good Luck
P.S.
If you need help translating it into vb.net just submit a comment.
Example (not working like this):
SELECT * FROM User
WHERE LastActivity > Date(1980,1,1)
Where Date should use the parameters year, month, day as integer numbers. To avoid troubles with locales, I explicitly do not want to use a string like "yyyy/mm/dd" or similar.
I am working with Microsoft SQL Server Management Studio on an MSSQL Database, if this matters.
Note: I am sure this is trivial, but I could not find the solution neither using google or SO.
To avoid troubles with locales, I explicitly do not want to use a
string like "yyyy/mm/dd" or similar.
To avoid this, the best way is passing the date as language-neutral like YYYYMMDD. This way it will be language independent:
SELECT * FROM User
WHERE LastActivity > '19800101';
Use ISO date format which is yyyymmdd
SELECT * FROM User
WHERE LastActivity > CONVERT(DATE, (CONVERT(VARCHAR(4), #Year) +
RIGHT('0' + CONVERT(VARCHAR(2), #Month),2) +
RIGHT('0' + CONVERT(VARCHAR(2), #Date),2) ))
I have always found that most databases will accept 'dd-mmm-yyyy' as a date parameter and I use it almost everywhere. There are some who don't (E.g. MS/Access) but SQL/Server does. This format is widely accepted, though obviously you may need to make it language specific.
SELECT * FROM User WHERE LastActivity > '11-Dec-2012';
I think the 'Convert' function does what your looking for. To make the 20th of March 1980, do the following:
Convert(date, '1980-03-20')
Your exact query would be:
SELECT * FROM User
WHERE LastActivity > Convert(date, '1980-01-01')
If you are using SQL Server 2012 and up, you can use the simpler DATEFROMPARTS function:
SELECT * FROM User
WHERE LastActivity > DATEFROMPARTS(1980,01,01)
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;
I need some help figuring out and SQL Statement.
I know what I want I just cant express it.
Im using php, so it doesnt need to be exclusivly SQL, its to act as a filter.
Pseudo code
$query="SELECT * FROM MyTable WHERE 'TIS' is not older than 2 days or empty = ''$ORDER"; }
TIS in the name of the column in my table were I store dates in this format 03-12-09 (d,m,y).
The $ORDER is for ordering the values based on values from other fields not dates.
Im looking at
SELECT *
FROM orders
WHERE day_of_order >
(SELECT DATEADD(day,-30, (SELECT MAX(day_of_order) FROM orders)) AS "-30 Days");
But i dont quite think im on the rigth track with this.
Thanks
Try the following:
SELECT *
FROM MyTable
WHERE COALESCE(TIS, SYSDATE) > SYSDATE - INTERVAL '2' DAY
$ORDER
I don't know what database you're using - the above uses Oracle's method of dealing with time intervals. If you're using SQL Server the following should be close:
SELECT *
FROM MyTable
WHERE COALESCE(TIS, GETDATE()) > DATEADD(Day, -2, GETDATE())
$ORDER
In MySQL try this:
SELECT *
FROM MyTable
WHERE COALESCE(TIS, NOW()) > DATE_SUB(NOW(), INTERVAL 2 DAYS)
$ORDER
I hope this helps.
So, I was pretty lost in all this.
How did it got solved:
First I understood that the Statement I was using was not supported by MySql thanks to eligthment from Bob Jarvis.
_ Second In a comment by vincebowdren wich "strongly" adviced me to change the data type on that field to Date wich indeed I had not, it was a string.
It was pretty Dumb for me to try using SQL operations for Dates on a field that had String values.
So I just RTFM: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
and:
mysql> SELECT something FROM tbl_name
-> WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= date_col;
Then proceeded to change the field value to date.
and this is my perfectly working query:
$query="SELECT * FROM MyTable WHERE DATE_SUB(CURDATE(),INTERVAL 2 DAY) <= TIS OR TIS = 0000-00-00 $ORDER "; }
I would like to thank the posters for their aid.