In SQL Server, there are many different ways to get what you need from the DateTime field.
Here is a list of what we needed for our project.
This example is based on my son's [date of birth] (Carr Barron). He sadly passed on October 12, 2023, just 4 days before his birthday.
- Year = datepart(year, EntryDate) as year -- Year (1993)
- Month = datepart(m, EntryDate) as month -- Month Number (10)
- Month Name = FORMAT(EntryDate,'MMMM') as MonthNam -- Month Name (October)
- Day = datepart(day, EntryDate) as day -- Day of the the month (16)
- Day Name = FORMAT(EntryDate,'dddd') as DayName - Name of the Day (Saturday)
- Hour = datepart(hh, EntryDate) as hour -- Hour in military Time (14)
- Hour = FORMAT(CAST(EntryDate AS DATETIME),'hh') as Standardhour -- Hour in Standard Time (2)
- Minutes = datepart(minute, EntryDate) as minute -- Minutes
- FORMAT(CAST(EntryDate AS DateTime), 'tt') as AMPM -- Gets the AM or PM (PM)
Here is an example you can run against your database.
(Change the table name from MyTable to your Table name.)