Solved

Extracting date & time from a nvarchar field

Posted on 2010-11-29
12
379 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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
 
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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
OPENDATASOURCE 8 40
SQL Server Degrading on Write 13 67
How to disable/enable multiple sql jobs in efficient way 11 107
Query 14 55
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

772 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