my requirement is cast the '2013050' date into date in sql
please help
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Feb 21, 2013, 5:42 AM
cc represents the century
yy represents the year in that century (0 to 99)
ddd represents the day in that year (1 to 365) or (1 to 366 in a leap year)
As Jignesh said, that's not a supported format in SQL Server.
However, it's fairly easy to convert it into a 'normal' date in C# code if that's any help to you:
Jignesh TrivediPosted Feb 21, 2013, 7:04 AM
it must
Declare @value varchar(7) = '2013050'
select DATEADD(d,CAST(SUBSTRING(@value,5,3) as int) - 1, cast(SUBSTRING(@value,1,4) + '-01-01' as date))
thx.
VulpesPosted Feb 21, 2013, 6:44 AM
Jignesh TrivediPosted Feb 21, 2013, 5:59 AM
Thanks Vulpes for given understanding..
Atul try,
Declare @value varchar(7) = '2013050'
select DATEADD(d,CAST(SUBSTRING(@value,5,3) as int),cast(SUBSTRING(@value,1,4) + '-01-01' as date))
here i am assuming first date of year is 1st jan.
it might help you.
Jignesh TrivediPosted Feb 21, 2013, 3:20 AM
sorry, but can you please explain what is cc?
and also as you say your date is in ccyyddd format
so your example is '2013050' so here ddd = 050 so i thinkit is not possible.
Mai Hu NaPosted Feb 21, 2013, 1:57 AM
Jignesh TrivediPosted Feb 20, 2013, 10:49 PM
hi,
it just look like unvalid date because 7 digit date '2013050' if we part this date year is 2013 month is 05 and date is 0?
it is not possible of 0 date.. :)
you may just refer below link
http://www.sql-server-helper.com/sql-server-2008/sql-server-2008-date-format.aspx
it contain conversion of date in different format
hope this will help you.