March 1, 2023 at 2:49 pm
Hello,
I have a field where I want to pull the year from. The field unfortunately is an "Intelligent field" and the year is in the middle. For example, the format is text - year - text. Is there a way I can extract the year from the field?
Here is some sample code:
CREATE TABLE #t (ID int IDENTITY, field varchar(100))
INSERT INTO #t (field) VALUES ('test1 - 2021 - test1')
INSERT INTO #t (field) VALUES ('test2 - 2022 - test2')
INSERT INTO #t (field) VALUES ('test3 - 2023 - test3')
SELECT * FROM #t
Many thanks in advance for any assitance!
March 1, 2023 at 2:53 pm
SELECT field, SUBSTRING(field, PATINDEX('%[2][0-9][0-9][0-9]%', field), 4) AS year
FROM #t
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply