Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - Get Numerical Day of Month/Quarter

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!

like image 292
Nick Vaccaro Avatar asked Mar 28 '11 15:03

Nick Vaccaro


People also ask

How do I get quarter dates in SQL?

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) .

How do I get the day of the month in SQL?

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.

How do I get the number of days in SQL?

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.

How do I extract just the day from a date in SQL?

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.)


1 Answers

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
like image 55
Cade Roux Avatar answered Oct 30 '22 04:10

Cade Roux