Solved

MS SQL 2012 Problem with converting date

Posted on 2013-11-06
7
351 Views
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:

SELECT *
INTO T_1213_Pres_Start_Date
FROM (
        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.

Sarah
0
Comment
Question by:ScuzzyJo
  • 3
  • 3
7 Comments
 
LVL 11

Accepted Solution

by:
Louis01 earned 500 total points
ID: 39626949
Precede you statement with
SET DATEFORMAT dmy;
0
 

Author Comment

by:ScuzzyJo
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?

Thanks
Sarah
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 39626990
try this

CONVERT(NVARCHAR(255),CONVERT(SMALLDATETIME, columnName,105))
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 11

Expert Comment

by:Louis01
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.

Louis
0
 

Author Comment

by:ScuzzyJo
ID: 39627023
Hi

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!

Sarah
0
 

Author Closing Comment

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

Expert Comment

by:Louis01
ID: 39627033
Thanks Sarah, any time!
Louis
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.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DTS Connection Failed 7 70
Slow Connectivity over ODBC 8 35
SSRS - How do I get a year value to display based on the current month? 8 42
string fuctions 4 26
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

778 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