Comparing date time values in SQL

I have date values stored as integers in the format YYYYMMDD.
Time values are also stored as integers, so there are no leading zeros.  Here are some examples
13:00 is stored as 130000
09:30 is stored as 93000
Midnight + 1 second is stored as 1
Midnight + 1 minute is stored as 100

Suppose I have two tables with these date time values.  How do I evaluate the number of time between the relevant rows?  If they are on the same day I need to compare the hours, minutes and seconds.  Otherwise the number of days, weeks etc.
AlHal2Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

puranik_pCommented:
You are in for some ugly string manipulation here. Can be achieved by using a combination of RIGHT() and SUBSTRING() functions. Tell me if you need help.
0
puranik_pCommented:
Just wrote something to give you an idea. In the script below, I'm considering only time values. For date, add similar code for year, month and date parts.
CREATE TABLE #mydate (datetimeinstring VARCHAR(32))
INSERT INTO #mydate
SELECT
    CASE len(mycolumn)
        WHEN 1 THEN convert(varchar(32),'00000') + convert(varchar(32),mycolumn)
        WHEN 2 THEN convert(varchar(32),'0000') + convert(varchar(32),mycolumn)
        WHEN 3 THEN convert(varchar(32),'000') + convert(varchar(32),mycolumn)
        WHEN 4 THEN convert(varchar(32),'00') + convert(varchar(32),mycolumn)
        WHEN 5 THEN convert(varchar(32),'0') + convert(varchar(32),mycolumn)
        ELSE convert(varchar(32),mycolumn)
    END
FROM
    mytable

SELECT CONVERT(datetime,
    SUBSTRING(datetimeinstring,1,2) + ':' +
    SUBSTRING(datetimeinstring,3,2) + ':' +
    SUBSTRING(datetimeinstring,5,2)
    )
FROM
    #mydate

DROP TABLE #mydate

Open in new window

0
Alpesh PatelAssistant ConsultantCommented:
Hi,

for date manipulation it is easy as "YYYYMMDD". Get forur char as year next two as month as last two as day.

However in Time, bit tough to get.
First check length if less then add leading zero.
and do as same as date.
0
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

AmmarRCommented:
hi

check code below will return date and time values in the format you wanted, and so now you can do your comparisons
-- this returns the date in th YYYYMMDD format
convert(varchar(12),getdate(),112)

-- this returns difference between days in the format mentioned above
select DATEDIFF(d,convert(varchar(12),getdate(),112),'20110408')

--this returns the time in the format you wanted
select datepart(HOUR,getdate()) * 10000 + datepart(MINUTE,getdate()) * 100 + datepart(SECOND,getdate())

Open in new window

0
AlHal2Author Commented:
Thanks for your responses, but I need something a bit more specific.  

How do I work out how many seconds between 01:23:45 on 3rd April and 0:00:01 on 6th April?
Raising points to 500.  Below is the format in integers (rather than strings).

          Table 1      Table 2
Date   20110403  20110406
Time         12345                1
0
puranik_pCommented:
Did you look at the code I posted? Using that, once you get the values in datetime format, you can easily do things like...
DATDIFF(second,datetimevalue1,datetimevalue2)
0
SharathData EngineerCommented:
try this.
DECLARE  @Date1 INT, 
         @Time1 INT, 
         @Date2 INT, 
         @Time2 INT 

SELECT @Date1 = 20110403, 
       @Time1 = 12345, 
       @Date2 = 20110406, 
       @Time2 = 1 

SELECT DATEDIFF(SECOND,CONVERT(VARCHAR,Date1 + ' ' + Time1), 
                CONVERT(VARCHAR,Date2 + ' ' + Time2)) 
  FROM (SELECT CONVERT(VARCHAR,@Date1)     Date1, 
               CASE 
                 WHEN @Time1 / 3600 < 10 THEN '0' + CONVERT(VARCHAR(10),@Time1 / 3600) 
                 ELSE CONVERT(VARCHAR(10),@Time1 / 3600) 
               END + ':' + CASE 
                             WHEN (@Time1%3600) / 60 < 10 THEN '0' + CONVERT(VARCHAR(10),(@Time1%3600) / 60)
                             ELSE CONVERT(VARCHAR(10),(@Time1%3600) / 60) 
                           END + ':' + CASE 
                                         WHEN @Time1%60 < 10 THEN '0' + CONVERT(VARCHAR(10),@Time1%60)
                                         ELSE CONVERT(VARCHAR(10),@Time1%60) 
                                       END Time1, 
               CONVERT(VARCHAR,@Date2)     Date2, 
               CASE 
                 WHEN @Time2 / 3600 < 10 THEN '0' + CONVERT(VARCHAR(10),@Time2 / 3600) 
                 ELSE CONVERT(VARCHAR(10),@Time2 / 3600) 
               END + ':' + CASE 
                             WHEN (@Time2%3600) / 60 < 10 THEN '0' + CONVERT(VARCHAR(10),(@Time2%3600) / 60)
                             ELSE CONVERT(VARCHAR(10),(@Time2%3600) / 60) 
                           END + ':' + CASE 
                                         WHEN @Time2%60 < 10 THEN '0' + CONVERT(VARCHAR(10),@Time2%60)
                                         ELSE CONVERT(VARCHAR(10),@Time2%60) 
                                       END Time2) t1

Open in new window

If functions are allowed, You can create a function with this code and pass the parameters Date1,Time1,Date2 and Time2 and get the difference in seconds.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AlHal2Author Commented:
Thanks Sharath.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.