0

In SQL Server 2008, I want to get the year of following string format. Parameter will be one of following strings (DateTime.MinValue and DateTime.MaxValue from C#):

'1/1/0001 12:00:00 AM' and '12/31/9999 11:59:59 PM'

Here is my expected answer:

0001 from 1st string.
9999 from 2nd string.
1
  • try date_format function Commented May 2, 2013 at 4:03

2 Answers 2

1

If you know what the format of the input string is, you can just do:

SELECT ... SUBSTRING([Input], PATINDEX('%[0-9][0-9][0-9][0-9]%', [Input]), 4)
FROM   ...

Or

SELECT ... SUBSTRING([Input], CHARINDEX('/', [Input], 4) + 1, 4)
FROM   ...
Sign up to request clarification or add additional context in comments.

Comments

0

This could be another way to do the same as well.

declare @test nvarchar(50)
SET @test='1/1/9999 12:00:00 AM'
declare @testdate date
set @testdate=CONVERT(date,@test,101)
select DatePart(year,@testdate)

Check out this sqlfiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.