Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
so i have a field with time and a field with date and i want to combine them to get a datetime field

then use this value and compare if it's less than
GETUTCDATE()


but i'm getting an error

What I have tried:

select 
cast(Table.date +' ' +(Select HourValue from table2 where table2.Hour = Table.Hour) +
+ ':' + (Select MinuteValue from table3 where table3.Minute = Table.Minute) as datetime) as Timee

from	Table
Posted
Updated 2-Oct-20 5:16am
v3
Comments
[no name] 2-Oct-20 11:06am    
Try selecting into variables first and using those in your "cast" expression. You can at least tell what you're selecting or not.

datatypes - How to combine date and time to datetime2 in SQL Server? - Database Administrators Stack Exchange[^]
SQL
SELECT 
    DATEADD(day, DATEDIFF(day, '19000101', DatePart), CAST(TimePart AS DATETIME2(7))) As [DateAndTime]
FROM
    YourTable
;
 
Share this answer
 
That's probably not going to work, even if you didn't get an error: your "inner selects" are unlikely to return a single value which matches the criteria, even if you could phrase it like that.

Instead, use a JOIN to combine the three tables row-by-row and then work out the details.

But ... that implies that Table.Date is a VARCHAR or NVARCHAR value and if so, is a very bad idea. Change your design to use DATE or DATETIME instead, and then use DATEADD to include your hour and minute values.
And I have my doubts about the whole existence of Table2 and Table3 - they look like you have created them to get round some other design mistake but I can't suggest what that might be because I have no idea what your tables look like or contain!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900