dateadd

DATEADD Calculation

跟風遠走 提交于 2019-12-19 11:25:37
问题 How the calculation happen for MONTH datepart in DATEADD() Add Month SELECT '2012-01-29' AS [Date], CAST(DATEADD(MONTH, 1, '2012-01-31') AS DATE) AS NextDate UNION SELECT '2012-01-31' AS [Date], CAST(DATEADD(MONTH, 1, '2012-01-31') AS DATE) AS NextDate UNION SELECT '2013-01-31' AS [Date], CAST(DATEADD(MONTH, 1, '2013-01-31') AS DATE) AS NextDate Result Subtract Month SELECT '2012-02-29' AS [Date], CAST(DATEADD(MONTH, -1, '2012-02-29') AS DATE) AS PrevDate UNION SELECT '2012-03-01' AS [Date],

Does SQL Server optimize DATEADD calculation in select query?

坚强是说给别人听的谎言 提交于 2019-12-18 06:56:49
问题 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? 回答1: Surprisingly, I've found that using GETDATE() inline seems to be more efficient than

How to select last one week data from today's date

一曲冷凌霜 提交于 2019-12-17 18:45:55
问题 How to select week data (more precisely, last 7 days data) from the current date in the fastest way as I have millions or rows in the table. I have a time stamp of created_date in sql table. I have tried this SELECT Created_Date FROM Table_Name WHERE Created_Date >= DATEADD(day,-7, GETDATE()) I have two question: Is this query is correct? Is this is the fastest way to get the last seven day data from a table having millions of rows ? 回答1: Yes, the syntax is accurate and it should be fine.

Add Number of days column to Date Column in same dataframe for Spark Scala App

梦想与她 提交于 2019-12-17 14:54:26
问题 I have a dataframe df of columns ("id", "current_date", "days") and I am trying to add the the " days " to " current_date " and create a new dataframe with new column called " new_date " using spark scala function date_add() val newDF = df.withColumn("new_Date", date_add(df("current_date"), df("days").cast("Int"))) But looks like the function date_add only accepts Int values and not columns . How can get the desired output in such case? Are there any alternative functions i can use to get the

How to call SQL dateAdd method using JPA @Query

99封情书 提交于 2019-12-13 02:46:44
问题 I have to search for row, which is older than 6 months. I have insert time(datetime) as one of the column in my table. if it would have been a sql query, it is straight forward like this :- select * from myTable where insertTime<=dateadd(month, -6, getDate()); But I am using JPA, so I had to write something like this :- select * from myTable where insertTime<=function('DATEADD',month, -6, function('getDate')); I am not sure how to put month paramater in above query. I am getting following

Could somebody explaine difference between two queries?

北慕城南 提交于 2019-12-12 16:28:00
问题 The first query is: declare @myDate datetime = DATEADD(D,-2000,getdate()) SELECT * FROM [myTable] where CreatedDate >= @myDate The second query is: SELECT * FROM [myTable] where CreatedDate >= DATEADD(D,-2000,getdate()) I expect that the first query may be faster, because of 'dateadd' function calculates once. But in practice this queries are both equal (2 seconds, 30 000 rows) 回答1: getdate() is a runtime constant function and is only evaluated once per function reference which is why SELECT

Datediff GETDATE Add

拈花ヽ惹草 提交于 2019-12-12 03:09:18
问题 In this type of code, AND Orders.ShipDate >= DATEADD(Day, Datediff(Day,0, GetDate() -6), 0) It supposed to pull records with the date 6 days ago, until today. How can I make it pull records from 7 days ago until yesterday? I know changing -6 to -7 will pull records from 7 days ago, but which variable is the end of the date span so I can change it to -1 ? 回答1: It's not a date span. The condition you have there is really only one condition: greater than. The right side of the greater than is 6

count number of users for past 7 days from a given date

耗尽温柔 提交于 2019-12-12 01:38:24
问题 I have table in which I have to update the number of users on that particular date and number of users in past 7 days from the given date. Sample data looks like Date UserCountToday UserCount7days 20120911 907575 20120910 953629 20120909 1366180 20120908 1388916 20120907 1009425 20120906 918638 20120905 956770 20120904 1018152 20120903 1306341 回答1: try this: with cte as (select *,ROW_NUMBER() over(order by [Date]) as row_num from t_users) select [Date],UserCountToday, (select SUM

MDX DateAdd function over a set of tuples

梦想的初衷 提交于 2019-12-11 15:06:20
问题 I am trying to create a set of dates using DateAdd() function but I am getting errors while trying to pass a set of tuples as parameter. The below code returns a member but I am looking for a set of new dates. WITH Member [EFF INJ DT] AS DATEADD("M",12, [INJURY DATE].CurrentMember) SELECT {[EFF INJ DT]} ON COLUMNS, [INJURY DATE].[DATE].Members ON ROWS FROM [WVWC DATA CUBE FROI SROI] I have the following attempt: WITH Set [EFF INJ DT] AS DATEADD("M",12, [INJURY DATE].CurrentMember) SELECT {

Pass a column as parameter to dateadd in SQL Server

邮差的信 提交于 2019-12-10 11:53:10
问题 I want to convert a column of UTC time to local time. My data looks like this: time_utc TZID timezone ------------------------------------------------ 2014-02-27 12:00:39.0 America/Toronto -5 2013-05-21 09:35:30.0 America/Goose_Bay -4 2015-01-08 06:58:58.0 America/Creston -7 I know that using select *, DATEADD(hour, 5,time_utc) from mytable will add 5 hours to column time_utc . However, as you can see, I have a variable time zone column. How can I pass this variable to the dateadd function? I