?
Solved

Extracting date & time from a nvarchar field

Posted on 2010-11-29
12
Medium Priority
?
408 Views
Last Modified: 2012-08-13
Please see attached query in Word
ee-query.doc
0
Comment
Question by:ejr19592004
[X]
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
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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 58

Accepted Solution

by:
cyberkiwi earned 400 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

650 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