Previous Code Entry
Left Arrow
SQL Server - Return only records where the count is not 0
Current Code Entry
Next Code Entry
SQL Server Replace Text within a long string
Right Arrow
SQL Server - Get Time from DateTime
Article Entry Date: March 24,2024 @ 11:54:47 / Last Updated On: March 24,2024 @ 12:03:09
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.
  1. Year = datepart(year, EntryDate) as year -- Year (1993)
  2. Month = datepart(m, EntryDate) as month -- Month Number (10)
  3. Month Name = FORMAT(EntryDate,'MMMM') as MonthNam -- Month Name (October)
  4. Day = datepart(day, EntryDate) as day -- Day of the the month (16)
  5. Day Name = FORMAT(EntryDate,'dddd') as DayName - Name of the Day (Saturday)
  6. Hour = datepart(hh, EntryDate) as hour -- Hour in military Time (14)
  7. Hour = FORMAT(CAST(EntryDate AS DATETIME),'hh') as Standardhour -- Hour in Standard Time (2)
  8. Minutes = datepart(minute, EntryDate) as minute -- Minutes
  9. 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.)