I am using SQL Server 2014 and I am working with a column from one of my tables, which list arrival dates.
It is in the following format:
ArrivalDate
2015-10-17 00:00:00.000
2015-12-03 00:00:00.000
I am writing a query that would pull data from the above table, including the ArrivalDate column. However, I will need to convert the dates so that they become the first day of their respective months.
In other words, my query should output the above example as follows:
2015-10-01 00:00:00.000
2015-12-01 00:00:00.000
I need this so that I can create a relationship with my Date Table in my PowerPivot model.
I've tried this syntax but it is not meeting my requirements:
CONVERT(CHAR(4),[ArrivalDate], 100) + CONVERT(CHAR(4), [ArrivalDate], 120) AS [MTH2]
If, for example, it is 15th of given month then you subtract 14 and cast the result to date:
SELECT ArrivalDate
, CAST(DATEADD(DAY, -DATEPART(DAY, ArrivalDate) + 1, ArrivalDate) AS DATE) AS FirstDay
FROM (VALUES
(CURRENT_TIMESTAMP)
) AS t(ArrivalDate)
ArrivalDate | FirstDay
2019-05-15 09:35:12.050 | 2019-05-01
But my favorite is EOMONTH which requires SQL Server 2012:
SELECT ArrivalDate
, DATEADD(DAY, 1, EOMONTH(ArrivalDate, -1)) AS FirstDay
FROM (VALUES
(CURRENT_TIMESTAMP)
) AS t(ArrivalDate)
ArrivalDate | FirstDay
2019-05-15 09:35:52.657 | 2019-05-01
Use FORMAT to format your date.
DECLARE #date DATETIME = '2015-10-17 00:00:00.000'
SELECT FORMAT(#date, 'yyyy-MM-01 HH:mm:ss.fff')
Or if you don't want time part:
SELECT FORMAT(#date, 'yyyy-MM-01 00:00:00.000')
LiveDemo
Beginning with SQL Server 2012, you can also use DATEFROMPARTS:
SELECT DATEFROMPARTS(YEAR(ArrivalDate), MONTH(ArrivalDate), 1)
FROM my_table
Round date to first of the month:
DATEADD(MONTH, DATEDIFF(MONTH, 0, DateColumn), 0)
Or just simply use the ROUND function -
SELECT ROUND (TO_DATE ('27-OCT-00'),'YEAR')
"New Year" FROM DUAL;
New Year
01-JAN-01
Related
I am trying to extract 2 years ago data with date range greater than 07/01/2019 and 2 years ago same month and week. Need suggestions on date conversion as well
select
tilr.BusinessUnitID
,emph.employeeID
,convert(varchar(10), cast(cast(tilr.date_key as varchar(10)) as date), 101) as ConvertDate
,tilr.paidhr as 'Paid hr'
from [dbo].[location] tilr
inner join [dbo].[Employee] emph
on emph.employeeID = tilr.employeeID
and emph.businessunitid = tilr.BusinessUnitID
and emph.date_key = tilr.date_key
where
tilr.date_key >= 20190701
and datename(year, convert(varchar(10), cast(cast(tilr.date_key as varchar(10))as date), 101))
< DateAdd(YY, -2, GETDATE())
Trying to get data for date range >= 07/01/2019 and < 10/23/2019 ( +/- days of same month 2 years ago) for comparison. With above query I'm getting data till end of the year 12/2019 instead of 10/2019.
Sample data
BusinessUnitID employeeID ConvertDate Paid hr
1234 1 07/01/2019 1.4
2345 2 10/25/2019 3.5
It looks like you need something like the following condition
tilr.date_key >= DATEADD(month, -2, DATEFROMPARTS( YEAR(GETDATE()) - 2, MONTH(GETDATE()), 1 )) AND
tilr.date_key < DATEADD(month, 1, DATEFROMPARTS( YEAR(GETDATE()) - 2, MONTH(GETDATE()), 1 ))
Note how calculations only use date functions, not conversion to/from varchar, and that there are no functions on the actual column value. This means that indexes can be used efficiently.
I am trying to show banner on page if dates is between StartDate AND EndDate
SELECT *
FROM TABLE
WHERE GETDATE() BETWEEN StartDate AND EndDate)
This fails to get record with ID 3 if I use GETDATE() which also compares datetime let since my time value is always 00:00:00.000 is there a way I can only use GETDATE to compare yyy-mm-dd part of database column so that above query will show all three records
ID StartDate EndDate
-------------------------------------------------------
1 2020-12-31 00:00:00.000 2021-03-15 00:00:00.000
2 2020-12-31 00:00:00.000 2021-03-31 00:00:00.000
3 2021-01-01 00:00:00.000 2021-02-28 00:00:00.000
I also use below query but it showed same result set.
SELECT *
FROM TABLE
WHERE (GETDATE() BETWEEN StartDate AND CONVERT(varchar, EndDate, 23))
Changes datatype to store only date part can solve the issue but I can't make changes to database
If you want to ignore times, do so on the GETDATE():
WHERE CONVERT(DATE, GETDATE()) BETWEEN StartDate AND EndDate
Or add a day to EndDate and use:
WHERE GETDATE() >= StartDate AND
GETDATE() < DATEADD(DAY, 1, EndDate)
To keep the query sarge-able (able to use indexes), convert GETDATE to how you want it, instead of converting the column.
WHERE
GETDATE() >= StartDate AND
CAST(CAST(GETDATE() AS date) AS datetime) <= EndDate
SELECT *
FROM TABLE
WHERE CAST(GETDATE() AS DATE) BETWEEN StartDate AND EndDate
I have a table who have creation date like:
SELECT [CreationDate] FROM Store.Order
So each register have one datetime like:
2018-03-14 00:00:00.000
2017-04-14 00:00:00.000
2017-06-14 00:00:00.000
I want to know how to COUNT only register of Date equals to current month and year, for example in this case,if I only have one
register in month 03 I just get 1 on count, how can I achieve it? Regards
Here is one option:
SELECT COUNT(*)
FROM Store.Order
WHERE
CONVERT(varchar(7), [CreationDate], 126) = CONVERT(varchar(7), GETDATE(), 126);
Demo
We can convert both the creation date in your table and the current date into yyyy-mm strings, and then check if they be equal.
The most efficient way is to do:
where creationdate >= dateadd(day, 1 - day(getdate(), cast(getdate() as date)) and
creationdate < dateadd(month, 1, dateadd(day, 1 - day(getdate(), cast(getdate() as date)))
Although this looks more complex than other solutions, all the complexity is on getdate() -- meaning that the optimizer can use indexes on creationdate.
In a calendar control, we can see some dates from the previous month and next month also. Sample image below
(ie Apr-2016: Starts from Mar-28 and ends in May-08
Mar-2016: Starts from Apr Feb-29 and ends in Apr-10)
Here, i need to generate a list of all the dates in a calendar control for a particular year month. My week start is Monday.
Here is the tsql script i have tried so far.
DECLARE #V_DATE DATE = GETDATE()
;WITH CTE_DATE AS (
SELECT DATEADD(dd,-(DAY(#V_DATE)-1),#V_DATE) CDATE
UNION ALL
SELECT DATEADD(dd,1,CDATE)
FROM CTE_DATE
WHERE DATEADD(dd,1,CDATE) <= DATEADD(dd,-(DAY(DATEADD(mm,1,CDATE))),DATEADD(mm,1,CDATE))
)
SELECT * FROM CTE_DATE
Result Is:
2016-04-01
2016-04-02
.
.
2016-04-29
2016-04-30
It will list all the days from a inputted year month, but i need to include the
missing dates from the previous month as well as next month.
Expected result for Apr-2016
2016-03-28
2016-03-29
.
2016-04-15
.
2016-05-07
2016-05-08
Expected result for May-2016
2016-04-25
2016-04-26
.
2016-05-15
.
2016-06-04
2016-06-05
Note:- The calendar control is always showing 42 days.
since your week is starts on Monday,you can take referece to date 0 '1900-01-01' which is a Monday. Adding 41 days would gives you your end date
select
date_fr = dateadd(day, datediff(day, 0, '2016-05-01') / 7 * 7, 0),
date_to = dateadd(day, datediff(day, 0, '2016-05-01') / 7 * 7, 41)
the following gives you date 1900-01-01 and Monday
select convert(datetime, 0), datename(weekday, 0)
Have you considered creating a dates table in your database. You would have columns for dates and a column for week number. Linking to this table you could find the week number for your start and end dates, you could then re-link to the table to find the first date of your start week and the last date of your end week. This would probably be more efficient than calculations at each step each time, it is a simple link.
I have create done script for this. This is working as per my expectation, may be helpful for future reference. (Thanks #Squirrel for the logic).
DECLARE #V_ST_DATE DATE = GETDATE()
SET #V_ST_DATE = DATEADD(DAY,-(DAY(#V_ST_DATE)-1),#V_ST_DATE)
SET #V_ST_DATE = DATEADD(WEEK,DATEDIFF(WEEK, 0, #V_ST_DATE) ,0) +
(CASE WHEN DATEADD(WEEK,DATEDIFF(WEEK, 0, #V_ST_DATE) ,0) > #V_ST_DATE THEN -7 ELSE 0 END)
;WITH CTE_DATE AS (
SELECT #V_ST_DATE CDATE,0 TDAYS
UNION ALL
SELECT DATEADD(DAY,1,CDATE) , DATEDIFF(DAY,#V_ST_DATE, DATEADD(DAY,1,CDATE))
FROM CTE_DATE
WHERE DATEDIFF(DAY,#V_ST_DATE, DATEADD(DAY,1,CDATE)) < 42
)
SELECT * FROM CTE_DATE
I have a database that I need to sum 2 values using the datetime column. Example:
Date Offered
4/16/2012 08:00:00 2
4/16/2012 08:30:00 18
4/16/2012 09:00:00 14
4/16/2012 09:30:30 42
I need to sum the values of 08:00:00 with 08:30:00 (total: 20) and 09:00:00 with 09:30:00 (total: 56) and so on.
This should work for you
select datepart(hour,myDate), SUM(Offered)
from myTable
group by
datepart(hour,myDate),
dateadd(d, 0, datediff(d, 0, myDate))
You need to group by both the hour and the date if you want it summed by individual day, otherwise you'll include other days (IE April 15 etc...)
Your pseudo code
Select HOUR(date), sum(offered) as sumO
FROM YourTable
Group By Hour(date)
Hour(date) would need to be altered to the correct syntax for the database you're working with.
SELECT [Hour] = DATEPART(HOUR, [Date]), Offered = SUM(Offered)
FROM dbo.table_name
WHERE [Date] >= '20120416'
AND [Date] < '20120417'
GROUP BY DATEPART(HOUR, [Date])
ORDER BY [Hour];
Use a DatePart function.
Syntax depends on your database
Select DatePart("year", Date), Sum(Offered)
From table1
Group By DatePart("year", Date)