Solved

Extracting date & time from a nvarchar field

Posted on 2010-11-29
12
359 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

21 Experts available now in Live!

Get 1:1 Help Now