Getting time from date range interval text column in SQL

googlesheet test

I have a column that is in the format of

2/23/23 9:00 am - 2/23/23 9:59 am

I am using a select statement to get the start date and start time from this column as follows:

start date: LTRIM(RTRIM(CONVERT(DATE,RTRIM(LTRIM(LEFT([Date], CHARINDEX(' ',[Date]) + 0))))))
start time: RTRIM(LTRIM(FORMAT(CAST(REPLACE(REPLACE(RTRIM(LTRIM(RIGHT(RIGHT(LEFT([Date], CHARINDEX('-', [Date]) - 1), LEN(LEFT([Date], CHARINDEX('-', [Date]) - 1)) - PATINDEX( '%/[12][0-9] %',LEFT([Date], CHARINDEX('-', [Date]) - 1))),LEN(RIGHT(LEFT([Date], CHARINDEX('-', [Date]) - 1), LEN(LEFT([Date], CHARINDEX('-', [Date]) - 1)) - PATINDEX( '%/[12][0-9] %',LEFT([Date], CHARINDEX('-', [Date]) - 1)))) - 1))),'am','AM'),'pm','PM') AS datetime),'hh:mm tt')))

Desired output for time: 9:00 am

The start date works but for start time I am having issues now when the year is formatted as /2023 instead of /23 because I will have both of these year formats included in my table.

What I tried is adding another % to account for the 2 extra digits in the year as follows:

PATINDEX( '%/%[12][0-9] %'

but this gives me 'Conversion failed when converting date and/or time from character string.' error.

What should I change in my start time select statement to account for having both '2023' and '23' in the year part.

Maciej Los

If you want to get time difference between two dates, you can use something like this:

;WITH InitialData(TimeRange) AS
(
  SELECT '2/23/23 9:00 am - 2/23/23 9:59 am'
),
CTE( StartDate, EndDate ) AS
(
  SELECT CAST(LEFT(TimeRange, CHARINDEX('-', TimeRange)-2) AS DATETIME2), 
    CAST(RIGHT(TimeRange,LEN(TimeRange)-CHARINDEX('-', TimeRange)-1) AS DATETIME2)
  FROM InitialData
)
SELECT DATEDIFF(MI, StartDate, EndDate) AS TimeDiff
FROM CTE;

Working example here: DbFiddle

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to divide pandas date time column in half hourly interval

how to separate date and time from same column sql

Date & time text to column

pandas: create a date time column from an existing date time column

Excel - Getting a year range from a single column

How to display date and time from datetime column in SQL

spark scala create column from Dataframe with values dependent on date time range

Excel: convert from date to time range

Add time interval column from date time column with python

Compare date and time range from database with user input date and time

sql date range in column and count by group

Getting just the data from a selected column range

getting file from date range of the current directory

Getting a range of data from infinite time series

How to get count data column in SQL server between different dates and each date has time range?

SQL Getting Date Range

T-SQL to Group time interval change by date range in sql server

Getting a time from a text file

get exact date time from x date time range

SQL Server : get rows for date and time range

Finding time range for column value change in SQL

Getting specific values from a range of time in Pandas

Pandas Binning Only Time Column Not Date From a Custom Range

Getting start time from a datetime range in SQL Server

SQL (postgresql) update a date column using an interval from data in a join table

How do I use the conditional WHERE date >= current_date - INTERVAL '{time interval)' using column values?

Get 5 minutes Interval by creating columns as start and end time from date & time stamp column in pandas

SQL window function and (date+interval) as a border of range

Convert a simple text column date to a date in SQL