Sorting by StartDateTime

Sorting by StartDateTime

Question:
I have a field called StartDateTime. In Microsoft Access it contained only a time. My code used to sort this field properly when outputting, so that morning events appeared at the top.

Since migrating to SQL Server, however, the field is now sorted by the first character, and the field contains dates in the 1800s that never used to be there. 9AM events appear right after 9PM events in my output.

So I ask:

1. How can I create just a TIME field for Microsoft SQL 7.0?

2. How can I sort the field properly (regardless of the answer for Question 1)?

Answer:
I’m not clear about whether you have dates and times in your table now.

You can use the DATEPART function to extract the time from the datetime you have. Then you’ll need to use the CONVERT function to change the data type of the date, because it is implicitly identified as an int.

The first thing to do is to get what you want out of the date.

SELECT DATEPART(hh,the_date) FROM MYTABLE will get you the hours portion of the datetime out of the field.

SELECT CONVERT(CHAR(2),DATEPART(hh, the_DATE)) will do the extraction and coerce the datatype into a character for you.

Hope this helps.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as