Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to Convert nvarchar to date

Author  Topic 

hspatil31
Posting Yak Master

182 Posts

Posted - 2013-05-25 : 08:25:40
Dear All,

I am trying following query. I have field nvarchar in ABC table which contains the date(Format: 12/12/2013).
I want to convert this to DATE. I am getting following error :

Error:
"Conversion failed when converting date and/or time from character string."
Can anyone please help me how to convert to DATE ?

Query:
SELECT DISTINCT PIDTBL.Id, CONVERT(DATE,SAO.Name)FROM [ABC] PIDTBL
INNER JOIN [XYZ] PSAM ON PIDTBL.Id = PSAM.ProductId
INNER JOIN [PQR] SAO ON PSAM.SpecificationAttributeOptionId = SAO.Id WHERE SAO.SpecificationAttributeId = 12

Thanks and Regard's
Harish Patil

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-05-25 : 08:30:11
Are you using dd/mm/yyyy format, or mm/dd/yyyy format? Can't tell from the 12/12/2013 example that you are using.

If you are using dd/mm/yyyy format, use CONVERT(DATE,SAO.Name, 103) and if you are using mm/dd/yyyy format, use CONVERT(DATE,SAO.Name, 101)

The 103 and 101 are styles - see here: http://msdn.microsoft.com/en-us/library/ms187928.aspx

You can avoid this type of problem if you specify your date literals in the unambiguous format for YYYYMMDD, for example '20130127'.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-25 : 09:19:27
see the reason for error here. The date values have to be of valid format and it has to be consistent inside varchar field

http://visakhm.blogspot.in/2011/12/why-iso-format-is-recommended-while.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -