Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 536071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:46:59+00:00 2026-05-13T09:46:59+00:00

SQL Server 2005. I want a script to run in the near future when

  • 0

SQL Server 2005. I want a script to run in the near future when we are ready to deploy the new app. We have a lot of old data that must be moved to new tables in the new app.

One such set of data is sampling hours the techs have entered, which must be converted to seconds in the new app.

Sounds easy enough, but brace yourself… the old app had no validation. The new column will be bigint, but the old column was varchar. Check out the different kind of data I’ve encountered for hours.

24
24:00
22:57
24 HR
24hrs
24 hours
22.3 Hrs
24 hr’s
n/a
3.09
19
86394
86400 Sec
24:00 valid:19:07
24 hrs / valid=13:44
15:8 (valid=15:07)

Ok, so take a deep breath, it’s actually not too bad. I’ve done most of the hard work already, (identifying the various patterns the users have been using.) This is what have so far:

I create a function for repetitive parsing of HH or SS or HH:MM format of sampling duration.

CREATE FUNCTION HoursToSecond(@input nvarchar(5))
RETURNS bigint
AS
BEGIN
    DECLARE @return bigint
    SELECT @return = CASE
        WHEN ISNUMERIC(@input)=1
        THEN CASE
                WHEN CAST(@input AS decimal(9,3))<100
                THEN CAST(@input as decimal(9,3))*3600 --convert hrs to secs
                ELSE CAST(@input as bigint) --already in seconds
            END
        WHEN CHARINDEX(':',@input)>0
        THEN CAST(left(@input,CHARINDEX(':',@input)-1) as int)*3600 +  
             CAST(SUBSTRING(@input,CHARINDEX(':',@input)+1,2) as int)*60
        ELSE NULL
    END
    RETURN @return
END

Then I switch based on the patterns I see in the data.

INSERT INTO NewDatasheets (sample_time)
    SELECT 
        CASE
           WHEN ISNUMERIC(samplingtime)=1 THEN dbo.HoursToSecond(samplingtime)
           WHEN samplingtime LIKE '% %' THEN dbo.HoursToSecond(LEFT(samplingtime, CHARINDEX(' ',samplingtime)-1))
           WHEN samplingtime LIKE '%h%' THEN dbo.HoursToSecond(LEFT(samplingtime, CHARINDEX('h',samplingtime)-1))
           WHEN samplingtime LIKE '%:%' THEN dbo.HoursToSecond(samplingtime)
           ELSE NULL
        END
    FROM OldDatasheets

Ugly script job. Yes. And I didn’t even try to parse the hours after “valid”. But it’ll do 90% of the work. And I can query for the edge cases and clean those up by hand… but I want to avoid any manual work.

I was wondering if anyone has a better solution, perhaps with less lines of code or avoiding the creation of a function.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:47:00+00:00Added an answer on May 13, 2026 at 9:47 am

    Because this is a one-time job from the sound of things, not an ongoing job, it doesn’t have to be that elegant a piece of code, or frankly even pick up all the data. It won’t have to be maintained so you can pretty much write anything that’ll work, and there’s no point in spending longer processing the odd rarities than it would take to update them individually, such as:

    UPDATE Table SET NewValue=86400 WHEN OldValue='86400 Sec';
    UPDATE Table SET NewValue=86400 WHEN OldValue='24 hrs / valid=13:44';
    

    This way you at least update all identical cases in one go, but it’s far less work than trying to parse the real oddities. Write what you can to handle the bulk of the cases (which it looks like you have) and just go through the rest manually as above. If you spot any patterns you can script in that then by all means script them but sometimes a manual job is the right answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 394k
  • Answers 394k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer FIXED Seems to work after adding the "javax.faces.validator.LengthValidator.MAXIMUM_detail" in the… May 15, 2026 at 2:25 am
  • Editorial Team
    Editorial Team added an answer KeePass has a password strength function. You can download the… May 15, 2026 at 2:25 am
  • Editorial Team
    Editorial Team added an answer From http://en.wikipedia.org/wiki/Duff%27s_device In computer science, Duff's device is an optimized… May 15, 2026 at 2:25 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.