Question

Calculate Time Elapse (days, hours, minutes)

Asked by: epicazo

Hello Experts,
I am working with some data I imported from an AS400 system.  All my date (xxxDT) and time (xxxTM) fields are numeric:

I need three UFNs that will calculate the following
150 points....Length of Stay in Days (LOSD)
150 points....Length of Stay in Hours (LOSH)
150 points....Length of Stay in Minutes (LOSM)

PPADDT + PPADTM is admit DATE/TIME
PPDSDT + PPDSTM is discharge DATE/TIME

PPADDT      PPADTM      PPDSDT      PPDSTM
20090701      2108      20090701      2225
20090702      653      20090702      653
20090701      819      20090701      1155
20090701      2013      20090702      105
20090701      2240      20090702      220
20090701      2251      20090702      230
20090701      1829      20090702      1729
20090701      806      20090704      1400
20090702      938      20090702      938
20090701      739      20090701      739
20090701      433      20090701      500
20090701      856      20090701      945
20090701      925      20090701      925
20090701      2028      20090702      344
20090701      306      20090703      1200
20090701      1235      20090702      1135
20090702      735      20090702      735
20090702      909      20090702      909
20090702      1057      20090702      1245
20090702      1411      20090702      2240
20090702      1604      20090703      130
20090702      1921      20090703      315
20090702      2353      20090707      1318
20090703      138      20090703      1210

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-08-26 at 15:31:09ID24684984
Tags

Microsoft SQL Server 2000

,

T-SQL

,

Date Functions

Topic

MS SQL Server

Participating Experts
2
Points
500
Comments
30

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Calculations
    i need to proform a calculation without a comand button. i want the total price to be calculated as soon as you enter the sale price and the amount. i need to populate lblUnitCost.
  2. calculator
    Deposit calculator in javascript, which can calculate the maturity amount of the monthly investments.
  3. Readmits to hospital within 90 days of discharge on Cryst…
    Hello: I have been asked to create a report showing those patients that are discharged at one point; let's say 3/1/06 will be the patient's discharge date, and readmitted to the hospital within 90 days of that discharge. (That would be an admission date by 6/1/06 - this is a...
  4. Converting Standard Time to Military Time in Crystal and Cal…
    First, I need to convert the following standard time to military time in Crystal. The time is a String field represented by 01:29 PM or 10:14 AM. Secondly I need to create a formula that will take this time and figure out the number of days the person was admitted. I have ...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: rob_farleyPosted on 2009-08-26 at 18:42:41ID: 25193907

Don't use User-Defined Functions for this - it will perform very poorly (like I noted on your other question).

Instead, put computed columns on your table (or just query the table using these formulae), that use:

LOSD as datediff(day, convert(datetime,convert(char(8),PPADDT),112), convert(datetime,convert(char(8),PPDSDT),112)) + 1

(if they leave on the same day, is that one day? If not, remove the +1)

LOSH as datediff(day, convert(datetime,convert(char(8),PPADDT),112), convert(datetime,convert(char(8),PPDSDT),112)) * 24 + (PPDSTM / 100) - (PPADTM / 100)

LOSM as datediff(day, convert(datetime,convert(char(8),PPADDT),112), convert(datetime,convert(char(8),PPDSDT),112)) * 24 * 60 + (PPDSTM / 100) * 60 + (PPDSTM % 100) - (PPDSTM / 100) * 60 - (PPADTM / 100)

But you may need to tweak them in case you want to count slightly differently.

Rob

 

by: mwvisa1Posted on 2009-08-26 at 20:09:07ID: 25194268

I agree with Rob, but wanted to add that you could use a derived table to make the calculations easier.

The basics is what Rob showed with converting the integer to character data then to date using the ISO standard format 112.  Since it converts to date from ISO string, this also works as a shorthand.

dateadd(mi, (ppadtm/100*60)+(ppadtm%100), convert(varchar,ppaddt))

convert(varchar, ppaddt) is same as Rob's convert(char(8), ppaddt) ==> '20090701'

dateadd takes three parameters with first being the interval (e.g., mi or minute); the adjustment amount; then original date.  Using the fact that the third parameter takes a date, the ISO string gets converted automatically to a date eliminating the convert(datetime, {value}, 112) showed earlier; however, that would be more explicit approach.  Then to keep things as tightly coded as possible, we use the same statement to add in number of minutes decoded as the hours * 60 and minutes from military time as Rob showed.

Hope this helps.

Regards,

--isa

select datediff(dd, admit, discharge) lsod,
datediff(hh, admit, discharge) lsoh,
datediff(mi, admit, discharge) lsom
from (
select dateadd(mi, (ppadtm/100*60)+(ppadtm%100), convert(varchar,ppaddt)) admit, 
dateadd(mi, (ppdstm/100*60)+(ppdstm%100), convert(varchar,ppdsdt)) discharge
from your_table
) t

                                              
1:
2:
3:
4:
5:
6:
7:
8:

Select allOpen in new window

 

by: mwvisa1Posted on 2009-08-26 at 20:11:16ID: 25194276

Forgot the reference to CAST/CONVERT:
http://msdn.microsoft.com/en-us/library/ms187928.aspx

 

by: rob_farleyPosted on 2009-08-26 at 20:42:46ID: 25194408

Kevin (--isa),

I don't agree with:

convert(varchar, ppaddt) is same as Rob's convert(char(8), ppaddt) ==> '20090701'

You're much better off using char(8), as varchar without a length will likely soon be deprecated.

And as I feel like epicazo wants something that is very much reusable (ie, why he wants a UDF rather than just a query), I figured that using computed columns (therefore needing to refer to original fields, no derived tables allowed) would be a useful option.

Rob

 

by: mwvisa1Posted on 2009-08-26 at 20:47:31ID: 25194427

Rob, I was trying to credit your solution.  I apologize for miscommunication.  I should have just typed the char(8), just took the lazy approach as I didn't want epicazo to miss the point that you already offered nice examples of the syntax -- my contribution was the derived table -- that is what I focused on.  My comments comparing to your was to just give you credit if that makes sense.

Here you go sir:

select datediff(dd, admit, discharge) lsod,
datediff(hh, admit, discharge) lsoh,
datediff(mi, admit, discharge) lsom
from (
select dateadd(mi, (ppadtm/100*60)+(ppadtm%100), convert(char(8),ppaddt)) admit, 
dateadd(mi, (ppdstm/100*60)+(ppdstm%100), convert(char(8),ppdsdt)) discharge
from your_table
) t

                                              
1:
2:
3:
4:
5:
6:
7:
8:

Select allOpen in new window

 

by: mwvisa1Posted on 2009-08-26 at 20:51:55ID: 25194441

To the point of the computed columns, I would think these would work also:
dateadd(mi, (ppadtm/100*60)+(ppadtm%100), convert(char(8),ppaddt)) ==> admit
dateadd(mi, (ppdstm/100*60)+(ppdstm%100), convert(char(8),ppdsdt)) ==> discharge

I offered the derived table as I was supporting your point that use of UDF is probably inefficient, but my point is instead of making 3 computed columns for the different measures with repeated hashing of the dates we make 2 computed columns with the dates and take advantage of the standard datediff (date functions) when querying. ;)

 

by: epicazoPosted on 2009-08-27 at 11:15:07ID: 25201085

I am sorry for the ingorance in my part, but what is a computed column?
Is it a computed column in a view or is it part of the actual table?

ERROR: syntax error converting datetime from character string
DATEDIFF(day, CONVERT (datetime, CONVERT (char(8), PPADDT), 112), CONVERT (datetime, CONVERT (char(8), PPDSDT), 112)) + 1

ERROR: Data Type expression error
dateadd(mi, (ppadtm/100*60)+(ppadtm%100), convert(char(8),ppaddt))



ERROR: Data Type expression error
datediff(day, convert(datetime,convert(char(8),PPADDT),112), convert(datetime,convert(char(8),PPDSDT),112)) * 24 * 60 + (PPDSTM / 100) * 60 + (PPDSTM % 100) - (PPDSTM / 100) * 60 - (PPADTM / 100)

ERROR: syntax error converting datetime from character string
(DATEDIFF(day, CONVERT (datetime, CONVERT (char(8), PPADDT), 112), CONVERT (datetime, CONVERT (char(8), PPDSDT), 112)) * 24 + PPDSTM / 100) - PPADTM / 100

 

by: rob_farleyPosted on 2009-08-27 at 14:55:45ID: 25203104

Sounds to me as if your data is not all YYYYMMDD.

And, a computed column is a column you add to the table that you're loading with your AS400 data. You can read more in Books Online.

Rob

 

by: mwvisa1Posted on 2009-08-27 at 15:20:58ID: 25203234

AS400 dates some times come out as integers like:

1090701 which is 07/01/2009

Is this what your data looks like?

If so, then instead of this:
dateadd(mi, (ppadtm/100*60)+(ppadtm%100), convert(char(8),ppaddt))

You would have to do this:
dateadd(mi, (ppadtm/100*60)+(ppadtm%100), convert(char(8),19000000+ppaddt))

Best regards,
Kevin

 

by: epicazoPosted on 2009-09-02 at 14:25:00ID: 25245838

Sorry for the delay; I was caught up in another project.   My data looks like this.... yyyymmdd  but notice the time not always four digit..

PPADDT      PPADTM      PPDSDT      PPDSTM
20090701      2108      20090701      2225
20090702      653      20090702      653
20090701      819      20090701      1155
20090701      2013      20090702      105
20090701      2240      20090702      220
20090701      2251      20090702      230
20090701      1829      20090702      1729

 

by: rob_farleyPosted on 2009-09-02 at 16:22:53ID: 25246616

The time doesn't need the leading zeros (better to not have them). So, a minute past midnight would just be 1.

But are PPADDT and PPDSDT ALWAYS proper date numbers?

Rob

 

by: epicazoPosted on 2009-09-02 at 17:13:54ID: 25246909

YES always YYYYMMDD

 

by: rob_farleyPosted on 2009-09-02 at 21:04:43ID: 25247795

Ok... but this error:

ERROR: syntax error converting datetime from character string
(DATEDIFF(day, CONVERT (datetime, CONVERT (char(8), PPADDT), 112), CONVERT (datetime, CONVERT (char(8), PPDSDT), 112)) * 24 + PPDSTM / 100) - PPADTM / 100

Would suggest that you have some records that don't fit. Are any NULL or empty?

Rob

 

by: epicazoPosted on 2009-09-03 at 08:40:36ID: 25251828

(DATEDIFF(day, CONVERT (datetime, CONVERT (char(8), PPADDT), 112), CONVERT (datetime, CONVERT (char(8), PPDSDT), 112)) * 24 + PPDSTM / '100)') - PPADTM / 100   .... gives me an error "Error converting data type varchar to numeric"

There doesn't appear to be any blanks in calcated fields.

 

by: epicazoPosted on 2009-09-03 at 08:43:57ID: 25251861

my bad, yes I still get the ERROR: syntax error converting datetime from character string

 

by: epicazoPosted on 2009-09-03 at 08:50:14ID: 25251925

I just confirmed that some rows in my PPDSDT & PPDSTM column contain ZERO (0).

 

by: mwvisa1Posted on 2009-09-03 at 09:36:05ID: 25252375

Do you want those values included as some default date OR shown as NULL?

 

by: epicazoPosted on 2009-09-03 at 10:46:23ID: 25253040

The result should be null -- thank you so much

 

by: mwvisa1Posted on 2009-09-03 at 10:58:25ID: 25253143

Something like this may work.  Can apply it to any of the computed column formulas you were using either from Rob's or mine.  Just showing with my 'admit' column formula.

(case when nullif([ppaddt],(0)) IS NOT NULL then dateadd(minute,([ppadtm]/(100))*(60)+[ppadtm]%(100),CONVERT([char](8),[ppaddt],0))  end)

It utilizes NULLIF to return NULL if date field value is 0.  This will now allow us to compare both NULL and 0 in one statement.  A better way to account for other bad data is to do something like this:

(case when coalesce([ppaddt],0) >= 17530101 then dateadd(minute,([ppadtm]/(100))*(60)+[ppadtm]%(100),CONVERT([char](8),[ppaddt],0))  end)

This uses COALESCE or ISNULL to change NULL values to 0 and then ensures that whether value was NULL or a malformed date it is at least greater than 17530101 which represents 1753-01-01 (the minimum allowed date for the datetime data type in SQL).

Kevin

 

by: epicazoPosted on 2009-09-03 at 11:19:20ID: 25253353


OPTION1 (nullif): error
Server: Msg 206, Level 16, State 2, Procedure VW_mytest3, Line 5
Operand type clash: int is incompatible with void type
Server: Msg 8117, Level 16, State 1, Procedure VW_mytest3, Line 5
Operand data type numeric is invalid for modulo operator.

OPTION2 (coalesce): error
Server: Msg 206, Level 16, State 2, Procedure VW_mytest3, Line 5
Operand type clash: int is incompatible with void type
Server: Msg 8117, Level 16, State 1, Procedure VW_mytest3, Line 5
Operand data type numeric is invalid for modulo operator.

 

by: mwvisa1Posted on 2009-09-03 at 11:22:06ID: 25253390

Try the second with ISNULL, sorry I had only tested the first method I posted and worked fine for me but my data types are INT for both columns instead of NUMERIC.  

(case when isnull([ppaddt],0) >= 17530101 then dateadd(minute,([ppadtm]/(100))*(60)+[ppadtm]%(100),CONVERT([char](8),[ppaddt],0))  end)

 

by: epicazoPosted on 2009-09-03 at 11:30:03ID: 25253493

Server: Msg 206, Level 16, State 2, Procedure Test_mytest3, Line 5
Operand type clash: int is incompatible with void type
Server: Msg 8117, Level 16, State 1, Procedure Test_mytest3, Line 5
Operand data type numeric is invalid for modulo operator.

 

by: mwvisa1Posted on 2009-09-03 at 11:30:19ID: 25253495

Ok tested and added this for the numeric and modulo issue.  Not seeing that on my system, but have SQL Server 2005 installed; therefore, I wrapped the field with convert(int, ...) before using modulus operator.

(case when isnull([ppaddt],(0))>=(17530101) then dateadd(minute,floor([ppadtm]/(100))*(60)+convert(int,[ppadtm])%(100),convert([char](8),[ppaddt],(0)))  end)

Get these results:
PPADTM        PPADDT        Admit
153      20090701      7/1/2009 1:53:00 AM
1623      17530101      1/1/1753 4:23:00 PM
230      0      NULL
1213      56789      NULL

Hope that works as well for you.

 

by: mwvisa1Posted on 2009-09-03 at 11:32:31ID: 25253512

If the ISNULL or any check for NULL makes this fail, just try it without that as explicit check on value should filter nulls anyway:

(case when [ppaddt]>=(17530101) then dateadd(minute,floor([ppadtm]/(100))*(60)+convert(int,[ppadtm])%(100),convert([char](8),[ppaddt],(0)))  end)

PPADTM   PPADDT   Admit
153	20090701	7/1/2009 1:53:00 AM
1623	17530101	1/1/1753 4:23:00 PM
230	0	NULL
1213	56789	NULL

                                              
1:
2:
3:
4:
5:

Select allOpen in new window

 

by: epicazoPosted on 2009-09-03 at 11:36:01ID: 25253546

here is the full sql

SELECT     PPGRP# AS PPGRPno, PPPAT# AS PPPATno, LEFT(DATENAME(year, CONVERT(datetime, CAST(PPADDT AS VARCHAR(10)), 112)), 4) AS PPAdYear, 
                      LEFT(DATENAME(MONTH, CONVERT(datetime, CAST(PPADDT AS VARCHAR(10)), 112)), 3) AS PPAdMonth, DATENAME(WEEKDAY, CONVERT(datetime, 
                      CAST(PPADDT AS VARCHAR(10)), 112)) AS PPAdWeekDay, SUBSTRING(CONVERT(VARCHAR, DATEADD(m, - 4, GETDATE()), 112), 1, 6) 
                      + '01' + '-' + CONVERT(varchar, GETDATE(), 112) AS PeriodPriorMonths, LEFT(CONVERT(varchar, GETDATE(), 112), 6) + '01' AS PeriodPriorMonth2, 
                      LEFT(DATENAME(quarter, CONVERT(datetime, CAST(PPADDT AS VARCHAR(10)), 112)), 4) AS Qtr, dbo.ufn_Grp2HR(PPADTM) AS TimeFrame, 
                      RIGHT('0000' + CAST(1 * CAST(PPADTM AS decimal(4, 0)) AS varchar(20)), 4) AS AdmitTime, PPADDT, PPADTM, PPDSDT, PPDSTM, DATEDIFF(yyyy, 
                      CONVERT(datetime, CAST(PPPDOB AS VARCHAR(10)), 112), CONVERT(datetime, CAST(PPADDT AS VARCHAR(10)), 112)) AS CurrendAge, 
                      CONVERT(datetime, CONVERT(char(8), PPADDT), 112) AS Expr1, (CASE WHEN isnull([ppaddt], 0) >= 17530101 THEN dateadd(minute, ([ppadtm] / (100)) 
                      * (60) + [ppadtm] % (100), CONVERT([char](8), [ppaddt], 0)) END) AS GrpDay2
 
FROM         dbo.HPPATMFL

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:

Select allOpen in new window

 

by: mwvisa1Posted on 2009-09-03 at 11:45:11ID: 25253643

??

I am sorry, I thought you were following Rob's advice of using this in a computed column.  Let me look at the SQL and get back to you.  My tests were in a computed column as you can see in the results I posted.

 

by: mwvisa1Posted on 2009-09-03 at 12:16:33ID: 25253949

Hopefully I don't have any type-o's that get you lost on my point which I had suggested earlier (use a derived table)...

As you can see I am getting necessary columns and converting values to dates in the derived table to make use of SQL date functions outside of it.

Hope that makes sense.

select [PPGRP#] as PPGRPno, [PPPAT#] as PPPATno,
datename(year, admit) as PPAdYear,
left(datename(month, admit),3) as PPAdMonth,
datename(weekday, admit) as PPAdWeekDay,
convert(char(8), dateadd(m, datediff(m, 0, getdate())-4, 0), 112) 
  + '-' + convert(char(8), getdate(), 112) as PeriodPriorMonths,
convert(char(8), 
   dateadd(m, datediff(m, 0, getdate()), 0),
   112) as PeriodPriorMonth2, 
left(datename(quarter, admit), 4) AS Qtr,
dbo.ufn_Grp2HR(PPADTM) AS TimeFrame,
convert(char(5), admit, 108) AS AdmitTime,
PPADDT, PPADTM, PPDSDT, PPDSTM,
datediff(year, dob, admit) AS CurrendAge, 
datediff(day, admit, discharge) lsod,
datediff(hour, admit, discharge) lsoh,
datediff(minute, admit, discharge) lsom
from (
	select [PPGRP#], [PPPAT#],
	PPADDT, PPADTM, PPDSDT, PPDSTM, PPPDOB,
	-- calculated dates
	case when [ppaddt]>=(17530101) 
    	   then dateadd(minute,
	      floor([ppadtm]/(100))*(60)
	         + convert(int,[ppadtm])%(100),
	      convert([char](8),[ppaddt], 0)
	   )  
	end as admit,
	case when [ppdsdt]>=(17530101) 
	   then dateadd(minute,
	      floor([ppdstm]/(100))*(60)
		+convert(int,[ppdstm])%(100),
	      convert([char](8),[ppdsdt], 0)
	   )  
	end as discharge,
	case when [pppdob]>=(17530101) 
	   then convert(datetime, 
		convert([char](8),[pppdob], 0), 
		112
	   )
	end as dob
    from dbo.HPPATMFL
) t
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:

Select allOpen in new window

 

by: rob_farleyPosted on 2009-09-03 at 19:03:26ID: 25256466

Wow - I got up this morning to see a lot of activity on this question.

It's great you've found the problem data. And yes, I'd be wrapping the expressions up in CASE to handle the bad rows.

You may prefer to use 19000101, rather than 1753... but it depends on if you're SQL 2008 or SQL 2005.

Rob

 

by: epicazoPosted on 2009-09-03 at 20:40:36ID: 25256704

I added another 50 points -- you guys are awesome.

 

by: epicazoPosted on 2009-09-03 at 20:45:44ID: 31620987

thank you both.  I decided to take Rob's advice to go with the computed column and mwvisa1 code.

If I had more points per answer, I would give them -- you experts are awesome!

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...