score:4
Accepted answer
You can use CASE
expression inside the DATEDIFF
function:
SELECT
Diff =
DATEDIFF(
HOUR,
timeIn,
CASE
WHEN timeOut < timeIn THEN DATEADD(DAY, 1, timeOut)
ELSE timeOut
END
)
FROM tbl
This will add one day on timeOut
if it's less than the timeIn
.
More questions
- SQL Calculate hours over night with static date
- SQL Query to calculate inventory on the first of each month over a date range
- Calculate SUM by date with week and month trick SQL
- How to combine date from one field with time from another field - MS SQL Server
- How to calculate difference in hours (decimal) between two dates in SQL Server?
- SQL Server GROUP BY datetime ignore hour minute and a select with a date and sum value
- How to compare datetime with only date in SQL Server
- Delete rows with date older than 30 days with SQL Server query
- How to: Create trigger for auto update modified date with SQL Server 2008
- How do I map TimeSpan with greater than 24 hours to SQL server Code First?
- Calculate exact date difference in years using SQL
- Update SQL Query with populated variables from AJAX functions over multiple PHP Pages
- SQL Server: how to select records with specific date from datetime column
- how to query where date with time = date without time in ms sql
- connect to sql "server group" with php to run query over multiple databases
- how to get today's Date only and not with the time using sql
- SQL Select Max Date with Multiple records
- How to convert hh:mm:ss to seconds in SQL Server with more than 24 hours
- What happens to a SQL Server 2005 table with an identity (int) that goes over maxint?
- Update sql date field in mssqlserver with YYYY-MM-DD format
- sql server lead - problems with date
- SQL Query with group by for multiple date ranges
- Need to calculate by rounded time or date figure in sql server
- SQL Select: Update if exists, Insert if not - With date part comparison?
- Max() Over Partition By with date conditions
- Divide rows with date in SQL Server 2014
- Issue with a date extraction in SQL Server
- Convert column with data MM/DD/YYYY varchar to date in sql server?
- Calculate percentage in MS SQL Server with int columns
- Calculate financial year start and end date based on year entered SQL Server and SSRS
More questions with similar tag
- Column name in SQL query from request
- Active Directory group member role check
- How to implement Two-way ordering in SQL?
- Converting SQL Server HashBytes varbinary to Bigint
- Convert gives an error
- Use data separated by delimiters in Sql Procedure
- Run SQL function for each returned record
- Attempted to perform an unauthorized operation when executing SQL Server 2019 service pack
- Error when altering field that is party of primary key, only on one server
- Evaluation differences between IF operator and If, then, else
- SSIS variable filter on different table in non linked Database
- Match 2 where clauses in query
- How to unlock user account in SQL Server 2005 Management Studio?
- Searching for a row based on the grouping of rows
- Convert Ms sqlserver query to Ms Access?
- Select multiple records grouped by primary key with max value on a column
- SQL Server returns error "Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'." in Windows application
- T-SQL MERGE: Each GROUP BY expression must contain at least one column that is not an outer reference
- T-SQL Fewest Sets of Common Dates that Includes All Row IDs
- Disable password policy in Sql Server Docker container
- Specific Partition D: full storage for SharePoint Data
- How to update the first row with its second row values in case some columns are duplicates
- Unable to preview SSIS Excel source data after skipping first few rows
- sp_sqlexec - To many arguments specified
- SQL Server and DMO using ADODB
- How can I use my SQL query in the cursor query?
- Separate date into 3 var (day, month, year) manually with string manipulation
- What happens when a nonclustered index is deleted?
- How can I connect from a website to a Microsoft SQL Database using PHP
- SQL Structure for several tables
Source:
stackoverflow.com