dateadd

SQL Query for retreiving records that fall on the last day of the month

感情迁移 提交于 2019-12-01 13:34:02
I have a table with records from many dates. I would like to perform a query that only returns records that fall on the last day of the date's month. Something like: SELECT * FROM mytable WHERE DATEPART(d, mytable.rdate) = DATEPART(d,DATEADD(m,1,DATEADD(d, -1, mytable.rdate))) Except this doesn't work as the rdate in the right-hand side of the argument should be the last day of it's month for the dateadd to return the correct comparison. Basically is there an concise way to do this comparison? An equivalent for quarter-ends would also be very helpful, but no doubt more complex. EDIT:

SQL Query for retreiving records that fall on the last day of the month

大兔子大兔子 提交于 2019-12-01 10:55:31
问题 I have a table with records from many dates. I would like to perform a query that only returns records that fall on the last day of the date's month. Something like: SELECT * FROM mytable WHERE DATEPART(d, mytable.rdate) = DATEPART(d,DATEADD(m,1,DATEADD(d, -1, mytable.rdate))) Except this doesn't work as the rdate in the right-hand side of the argument should be the last day of it's month for the dateadd to return the correct comparison. Basically is there an concise way to do this comparison

Using Parameters in DATEADD function of a Query

落花浮王杯 提交于 2019-12-01 08:53:39
I am trying to us the DateAdd function of SQL in my Query. The problem is when I use a parameter to set the second arguement, the number argument I get an error which will say something like this: Failed to convert parameter value from a Decimal to a DateTime While if I enter it parameterless, i.e hardcode an Int, it works fine. This works: SELECT FieldOne, DateField FROM Table WHERE (DateField> DATEADD(day, -10, GETDATE())) while this does not: SELECT FieldOne, DateField FROM Table WHERE (DateField> DATEADD(day, @days, GETDATE())) Where @days = -10 Any ideas into what I am doing wrong?

Using DATE_ADD with a Column Name as the Interval Value

China☆狼群 提交于 2019-12-01 04:02:08
I have a table which contains products, a start date and an interval value : product_name start_date expiry_period Domain Registration (1 Year) 2013-12-08 00:00:00 1 Year Domain Registration (1 Year) 2013-12-01 00:00:00 1 Year Website Hosting (Bronze) 2013-12-19 00:00:00 1 Year Website Hosting (Silver) 2013-12-20 00:00:00 1 Year Website Hosting (Silver) 2013-12-21 00:00:00 1 Year Domain Registration (2 years) 2014-01-04 00:00:00 2 Year Domain Registration (1 Year) 2014-01-04 00:00:00 1 Year Website Hosting (Silver) 2014-01-06 00:00:00 1 Year Domain Registration (2 years) 2014-01-06 00:00:00 2

Using DATE_ADD with a Column Name as the Interval Value

非 Y 不嫁゛ 提交于 2019-12-01 01:34:14
问题 I have a table which contains products, a start date and an interval value : product_name start_date expiry_period Domain Registration (1 Year) 2013-12-08 00:00:00 1 Year Domain Registration (1 Year) 2013-12-01 00:00:00 1 Year Website Hosting (Bronze) 2013-12-19 00:00:00 1 Year Website Hosting (Silver) 2013-12-20 00:00:00 1 Year Website Hosting (Silver) 2013-12-21 00:00:00 1 Year Domain Registration (2 years) 2014-01-04 00:00:00 2 Year Domain Registration (1 Year) 2014-01-04 00:00:00 1 Year

MySQL: DATE_ADD

Deadly 提交于 2019-11-30 14:40:09
问题 Is there a difference between: SELECT DATE_ADD('2005-01-01', INTERVAL 3 MONTH); and SELECT '2005-01-01' + INTERVAL 3 MONTH; 回答1: No, they're the same. 回答2: I asked a similar question just now and found the answer myself. Here's the justification why they are the same: SELECT BENCHMARK(20000000, DATE_ADD(NOW(), INTERVAL 3 MONTH)); +--------------------------------------------------------+ | BENCHMARK(20000000, DATE_ADD(NOW(), INTERVAL 3 MONTH)) | +----------------------------------------------

MySQL: DATE_ADD

这一生的挚爱 提交于 2019-11-30 11:21:49
Is there a difference between: SELECT DATE_ADD('2005-01-01', INTERVAL 3 MONTH); and SELECT '2005-01-01' + INTERVAL 3 MONTH; No, they're the same. I asked a similar question just now and found the answer myself. Here's the justification why they are the same: SELECT BENCHMARK(20000000, DATE_ADD(NOW(), INTERVAL 3 MONTH)); +--------------------------------------------------------+ | BENCHMARK(20000000, DATE_ADD(NOW(), INTERVAL 3 MONTH)) | +--------------------------------------------------------+ | 0 | +--------------------------------------------------------+ 1 row in set (1.70 sec) SELECT

MySQL - DATE_ADD month interval

泪湿孤枕 提交于 2019-11-30 07:57:10
I face a problem with the function DATE_ADD in MySQL. My request looks like this : SELECT * FROM mydb WHERE creationdate BETWEEN "2011-01-01" AND DATE_ADD("2011-01-01", INTERVAL 6 MONTH) GROUP BY MONTH(creationdate) The problem is that, in the results, -I think- because June has only 30 days, the function doesn't work properly as I have the results of the first of July . Is there a way to tell DATE_ADD to work well and take the right number of days within a month? Adrian Carneiro DATE_ADD works just fine with different months. The problem is that you are adding six months to 2001-01-01 and

Does SQL Server optimize DATEADD calculation in select query?

萝らか妹 提交于 2019-11-29 11:40:22
I have a query like this on Sql Server 2008: DECLARE @START_DATE DATETIME SET @START_DATE = GETDATE() SELECT * FROM MY_TABLE WHERE TRANSACTION_DATE_TIME > DATEADD(MINUTE, -1440, @START_DATE) In the select query that you see above, does SqlServer optimize the query in order to not calculate the DATEADD result again and again. Or is it my own responsibility to store the DATEADD result on a temp variable? Surprisingly, I've found that using GETDATE() inline seems to be more efficient than performing this type of calculation beforehand. DECLARE @sd1 DATETIME, @sd2 DATETIME; SET @sd1 = GETDATE();