SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to Convert nvarchar to date
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

hspatil31
Posting Yak Master

182 Posts

Posted - 05/25/2013 :  08:25:40  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3704 Posts

Posted - 05/25/2013 :  08:30:11  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 05/25/2013 :  09:19:27  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000