• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

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.
0
AlHal2
Asked:
AlHal2
1 Solution
 
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
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
 
AlHal2Author Commented:
Thanks Sharath.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now