MS SQL 2012 Problem with converting date

Posted on 2013-11-06
Last Modified: 2013-11-06
Hi All

I've adapted the following from a question I asked on here to convert nvarchar to a date into a new table:

INTO T_1213_Pres_Start_Date
        SELECT *, convert(smalldatetime,Pres_Start, 103) as Pres_Start_date
        FROM T_1213_OnlySSN
        WHERE ISDATE(Pres_Start) = 1
     ) as Pres_Start_Date;

The resulting table doesn't have all the records in it that I would expect, e.g. for one record, the original Pres_Start was 26/01/2013 and that record isn't copied over.  I really don't understand why as I've used "103" to signify a British date format.

Please could someone tell me where I'm going wrong?

Thanks for your help.

Question by:ScuzzyJo
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 11

Accepted Solution

Louis01 earned 500 total points
ID: 39626949
Precede you statement with

Author Comment

ID: 39626984
Hi Louis

That works perfectly, thanks.  Presumably, if I put it at the beginning of the query, it will apply all the way through and I won't have to keep typing it?

LVL 39

Expert Comment

by:Pratima Pharande
ID: 39626990
try this

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

LVL 11

Expert Comment

ID: 39627000
Hi Sarah
Setting the DATEFORMAT this way only makes it valid for that session.

You can set it globally for the server as follows:
EXEC sp_configure 'default language', 23 ; --23 = British English

OR for the user:
In Object Explorer, right-click a server and select Properties.
Click the Misc server settings node.
In the Default language for users box, choose the language in which Microsoft SQL Server should display system messages. The default language is English. You want British English.


Author Comment

ID: 39627023

Thanks, Pratima, but Louis beat you to it.

Louis, I think it will be OK for the session for now.

Thanks for your help.

I'll be back in about 2 mins with a different question, no doubt - the joys of learning something new!


Author Closing Comment

ID: 39627026
Louis answered really quickly and his solution worked with no problems.  Fantastic result!
LVL 11

Expert Comment

ID: 39627033
Thanks Sarah, any time!
p.s. Forgot to say: The reason for the SQL failing in the first place was on the ISDATE function. Unlike CONVERT where you can state the format (103 for dmy for instance) of the input or output, ISDATE uses the format dictated by the 'default language' setting of the server.

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question