简体   繁体   中英

Extract Date from varchar in SQL Server 2012

Extract Date from varchar

I am currently using SQL Server 2012 and looking to extract a date from a string of text in a column defined as varchar.

An example of this is as follows:

Mouse,Mickey T  Jul 9, 2020 9:24 am EDT:

The table that this column belongs to is a representation of notes entered or updated in an application. The column itself represents text from the note.

Upon entry of the note in the application (new note or update made to the note), our system auto-creates the first line of the note update to be in the format listed above. I would like to be able to extract the date from the text when it is in the above format.

Is this possible? Thank you in advance.

Is the provided string always in the same format? This code will work if there is nothing else after the string, and that it contains a double space between the Name and month.

DECLARE @DateString VARCHAR(50) = 'Mouse,Mickey T  Jul 9, 2020 9:24 am EDT:'
DECLARE @DateExtract VARCHAR(50) = SUBSTRING(@DateString, CHARINDEX('  ', @DateString) + 1, --Starting Point: Find the double space
                                             LEN(@DateString) - CHARINDEX('  ', @DateString) - 5) --End Point: How many characters to extract?  Total Length - Starting Point - ' EDT:'

--Extraction Checking Script
SELECT @DateString AS InputString,
       CHARINDEX('  ', @DateString) + 1 AS [StartPoint], --Starting Point: Find the double space
       LEN(@DateString) - CHARINDEX('  ', @DateString) - 5 AS [EndPoint], --End Point: How many characters to extract?  Total Length - Starting Point - 5 ( to cater for the timezone: ' EDT:')
       @DateExtract AS ExtractedValue

-- Output Values
SELECT CONVERT(DATE, @DateExtract), CONVERT(DATETIME, @DateExtract)

EDIT: This version assumes the last colon could be mid string and therefore just been added at the beginning of a note. Therefore updated the code to find where the second colon appears in the text string.

DECLARE @DateString VARCHAR(50) = 'Mouse,Mickey T  Jul 09, 2020 09:24 am EDT: dasdasda'
DECLARE @DateExtract VARCHAR(50) = SUBSTRING(@DateString,                                                                                    --
                                             CHARINDEX('  ', @DateString) + 1,                                                               --Starting Point: Find the double space
                                             CHARINDEX(':', @DateString, CHARINDEX(':', @DateString) + 1) - CHARINDEX('  ', @DateString) - 4 --End Point: How many characters to extract?  Find the character position of the second colon (:) minus the character position of the double space minus the 4 characters for ' EDT'
)

--Extraction Checking Script
SELECT @DateString AS InputString,
       CHARINDEX('  ', @DateString) + 1 AS [StartPoint],                                                              --Starting Point: Find the double space
       CHARINDEX(':', @DateString) AS [Colon],                                                                        --Find the end point of the name/date entry significed by the location of the colon.
       CHARINDEX(':', @DateString, CHARINDEX(':', @DateString) + 1) - CHARINDEX('  ', @DateString) - 4 AS [EndPoint], --End Point: How many characters to extract?  Find the character position of the second colon (:) minus the character position of the double space minus the 4 characters for ' EDT'
       @DateExtract AS ExtractedValue

-- Output Values
SELECT CONVERT(DATE, @DateExtract), CONVERT(DATETIME, @DateExtract)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM