Thursday, 14 November 2024

SQL DateTime functions

 Return Current date in SQL

SELECT GETDATE();



Return the current UTC date and time:

SELECT GETUTCDATE();



Get the month of date

SELECT month('2017/08/13 09:08') AS DayOfMonth;


Get the Year of Date

SELECT YEAR('2024/11/24 09:08') AS Year;


Get Month Name 

SELECT DATENAME(month, GETDATE()) AS monthname;



Find the Weekdays as today

SELECT DATENAME(WEEKDAY, GETDATE()) AS DatePartString;



Add a year to date & return the date: 

SELECT DATEADD(year, 1, '2024/09/25') AS YearAdd;



Subtract 2 months from  date & return the date:

SELECT DATEADD(month, -2, '2024/09/25') AS MonthSub;



Get the difference of month 

SELECT DATEPART(day, DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+2,0)))



Get the End of month date

SELECT DAY(EOMONTH(GETDATE()))



Get only todays date and month

select convert(varchar(5),getdate(),110)