Using SQL Server 2005:
How can I get the numerical day of the month and day of the quarter in a query?
DECLARE @DATE DATETIME
SET @DATE = GETDATE()
SELECT DATEPART(dy, @DATE) AS DayOfYear
--, <something> AS DayOfQuarter
--, <something> AS DayOfMonth
, DATEPART(dw, @DATE) AS DayOfWeek
Thanks in advance!
In SQL Server, you can use DATEPART(QUARTER,whn) and YEAR(whn) . In Oracle, you can use TO_CHAR(whn,'Q') and TO_CHAR(whn,'YYYY') for the quarter and year. In PostgreSQL, you can use EXTRACT(QUARTER FROM whn) and EXTRACT(YEAR FROM whn) . In Access, you can use DatePart("q", whn) and YEAR(whn) .
select DATEADD(mm, DATEDIFF(m,0,GETDATE()),0): in this we have taken out the difference between the months from 0 to current date and then add the difference in 0 this will return the first day of current month.
Use the DATEDIFF() function to retrieve the number of days between two dates in a MySQL database. This function takes two arguments: The end date.
If you want to get a day from a date in a table, use the SQL Server DAY() function. This function takes only one argument – the date. This can be a date or date and time data type. (In our example, the column VisitDate is of the date data type.)
DECLARE @DATE DATETIME
SET @DATE = GETDATE()
SELECT DATEPART(dy, @DATE) AS DayOfYear
, DATEDIFF(d, DATEADD(qq, DATEDIFF(qq, 0, @DATE), 0), @DATE) + 1 AS DayOfQuarter
, DAY(@Date) AS DayOfMonth
, DATEPART(dw, @DATE) AS DayOfWeek
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With