This section will explain how to get the current date-time in various formats. Have a look at the below output and analyze how this function is displaying the result of the last date of anyĬonvert date time format using CAST and CONVERT If you want to get the last date of the 6 th month from today, then you need to pass 6 number to get its detail. You can pass the number based on your requirement.
I have used 1 number to get the last date of next month I have used the -1 number to get the last date of the previous month I have not passed any number to get the last date of the current month Here, I will use the SQL Server GETDATE function as a specified date for these functions to return various parts orĮOMONTH ( GETDATE ( ), 1 ) AS The DATENAME function will return the character string-based date and time of a specifiedĭate whereas the DATEPART function will return an integer-based date and time of a specified date.ĭATEADD function helps us to get current, future, or past date-time based on the specified number to the inputīelow are the parts of date-time which can be returned using these functions:Įach date-time part has its abbreviation which can also be used to return the same output. Use GETDATE function with DATENAME, DATEPART, and DATEADD functionsĭATENAME and DATEPART are SQL Server functions that return the same information but
Now, let’s discuss how to use the SQL Server GETDATE function with various other functions in the below section. These functions areĭATENAME, DATEPART, DAY, MONTH, YEAR, etc. Use to get much deeper and granular level results using the SQL Server GETDATE function. SQL Server offers few functions which we can Month, date, or day then you cannot get that using the above queries. Let’s assume you want to return the only year, The output of the above queries is returning in a specific format. Use cases of SQL Server GETDATE () function It captures the current time during the execution of their respective function and each statement wasĮxecuted one after another that is why there is a tiny difference in a small part of their seconds.īelow is the execution of the same function under one query execution and we can see the time is the same for all 3įunctions here because they executed under one batch. If you want to get the same result, then you must execute all 3 functions in one query as I did in the firstĮxample. Here is the output where we can see the same time returned by all 3 functions except their fractional seconds.