0

How can I compare Date ([now] day, month, year, hour, minute) and data from two columns? The first column has date only like "2020-01-01" and second column for same row has data like "15:30:00" and this column datatype is time7.

I need to check if ([now] day, month, year, hour, minute) > from this two columns to do something.

this is data from data base

I need to compare it in client side in C#

5
  • 3
    Please provide some sample data and the expected results. Commented Apr 7, 2020 at 22:31
  • date column time column ---------------- || -------------------- 2020-01-01 || 15:50:00 this data in Data Base i need when i check now like 2020-04-08 00:36:00 then date in database less do something Commented Apr 7, 2020 at 22:34
  • 1
    Actual examples of real data from your database please. Commented Apr 7, 2020 at 22:35
  • 1
    Please edit and additional information direction into the question - don't add as comments. Commented Apr 7, 2020 at 22:38
  • 1
    Are you trying to compare the values on the database side in SQL, or on the client side in C#? Commented Apr 7, 2020 at 22:46

2 Answers 2

1

You can combine date part and time part into single datetime like this:

var time = TimeSpan.Parse("15:30:00", System.Globalization.CultureInfo.InvariantCulture);
var date = DateTime.Parse("2020-01-01", System.Globalization.CultureInfo.InvariantCulture);

var dateTime = date.Add(time);

if (DateTime.Now > dateTime)
{
    // TODO
}

Sign up to request clarification or add additional context in comments.

1 Comment

I'd strongly recommend doing this in the database query instead @HaythamAbdElghany.
0

You can solve the issue at the source and introduce a computed column:

CREATE TABLE [dbo].[whatever]
(
    [that_day] CHAR(10) NULL, 
    [that_time] CHAR(8) NULL, 
    [day_time] AS TRY_CONVERT(datetime2, that_day + ' ' + that_time) 
)

However this should only be the first step and fix the anti-pattern of storing date and time information as text in your database.

1 Comment

thank you Filburt for your answer, but i don`t need update table in database i just want process it on the client side in C# and Nenad answer it helped , thanks any way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.