Solved

Extracting date & time from a nvarchar field

Posted on 2010-11-29
12
370 Views
Last Modified: 2012-08-13
Please see attached query in Word
ee-query.doc
0
Comment
Question by:ejr19592004
12 Comments
 
LVL 4

Expert Comment

by:ong-hh
ID: 34235640
have u try this
SELECT CAST('2000-05-08 12:35:29.998' AS smalldatetime)
0
 

Author Comment

by:ejr19592004
ID: 34235736
Got this:

Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type smalldatetime.
0
 
LVL 10

Expert Comment

by:james-ct16
ID: 34235814
Howdy
Having gone through your word doc i think you may have a couple of issues to address. With having a smalldate date and a small date time for the same date for each row you are storing the same data three times, once as the varchar and twice as a small date. You cannot use the small date as time only or date only. Date and Time data types are not introduced until sql 2008.

To get the data formated as you want the following query should work

SELECT
convert(varchar, cast('01/10/10 01:00:04' as smalldatetime),103),
convert(varchar, cast('01/10/10 01:00:04' as smalldatetime),108)

So I would store the date as a datetime type and then format as required, if formating is required for reporting I would consider a view with date and time formated as per the query above.

Hope that helps
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 100 total points
ID: 34235815
update dataset set eventdate = convert(datetime, nullif(left(eventdatetime,8),''),3)

update dataset set eventtime = convert(datetime, nullif(right(eventdatetime,11),''),9)
0
 

Author Comment

by:ejr19592004
ID: 34235910
@cyberkiwi had high hopes for this, but I got this:

Msg 241, Level 16, State 1, Line 1
Conversion failed when converting datetime from character string.
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34235941
I tried it for every single row you listed, in fact I have even verified it on SQL Server 2000/8.
Do you have any other formats of data that is not as shown?

Maybe it is your language setting..
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34235950
Maybe, for the first one, use

update dataset set eventdate = convert(datetime, nullif(left(eventdatetime,8),''),1)

The last 1 or 3 refer to whether it is dd/mm/yy (3) or mm/dd/yy (1)
0
 

Author Comment

by:ejr19592004
ID: 34235980
It might be. This bloody thing is trying my patience. Let me use the upsizing wizard from access to sql rather than the import/export in ss. see if that makes any diference. give me a minute.
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34236010
select * from dataset
where len(eventdatetime) between 1 and 19

-- non conformists
0
 

Author Comment

by:ejr19592004
ID: 34236017
ok I ran the last one and i got this:

1      Transaction Time      NULL      NULL      Area Num
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34236041
What do you know... that looks like some HEADER row...
Just delete it before even starting the updates.  try http:#a34235815 again, but use 1 or 3 for the first query depending on whether 1/10 is 1-Oct or 10-Jan
0
 

Author Closing Comment

by:ejr19592004
ID: 34236050
code spot on. my mistake. 5 lines of rubbish crept into the file.
much obliged.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now