June 13, 2013 at 4:51 am
Hi all experts,
Is it possible in SQL Server to convert a string type to date like
DECLARE @OrgDate Nvarchar(50)='2013-06'
select CAST(@OrgDate as DATE)
The above is wrong. But i would like to know whether there is any way of doing this.
Like in Oracle we have TO_DATE() function.
June 13, 2013 at 6:11 am
'2013-06' isn't a date, in string form, or otherwise. It's a year and a month. If you want to convert it to a date, you'll have to add the extra information on to make it convertible.
Which day of the month do you want it to convert to? Assuming the 1st, you could do this:
DECLARE @OrgDate Nvarchar(50)='2013-06'
select CAST(@OrgDate+'-01' as DATE)
June 15, 2013 at 10:10 am
Just a hint when using strings for using date values:
You should always use the format YYYYMMDD, so '20130601'. This format is not depending on the language setting.
If you write it '2013-06-01', depending on the language setting of the current session, it could be evaluated as 6th of January but also the 1st of June.
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply