Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Storing date & time values separately.

Total Hit ( 1669)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


This example shows how date & time values can be stored separately using the SQL data types.

Click here to copy the following block
/*
    This table stores the date & time values as character strings.
    The ISO format is used for the date values & the military format
    for the time values. This will ensure correct ordering of the values too.
*/

CREATE TABLE #DateAndTime (
DateOnly char( 8 ) not null
    CHECK ( ISDATE( DateOnly ) = 1 And DateOnly LIKE REPLICATE( '[0-9]' , 8 ) ) ,
TimeOnly char( 8 ) not null
    CHECK( ISDATE( TimeOnly ) = 1 And LEN( TimeOnly ) = 8 And
        TimeOnly LIKE '[0-9][0-9]:[0-9][0-9]:[0-9][0-9]')
);

-- Sample data:
INSERT INTO #DateAndTime VALUES( '20001009' , '09:00:00' );
INSERT INTO #DateAndTime VALUES( '20001008' , '19:00:00' );
INSERT INTO #DateAndTime VALUES( '20001008' , '03:00:00' );
INSERT INTO #DateAndTime VALUES( CONVERT( varchar, CURRENT_TIMESTAMP , 112 ) , '18:00:00' );

SELECT * FROM #DateAndTime
ORDER BY DateOnly , TimeOnly;
/*
DateOnly TimeOnly
-------- --------
20001008 03:00:00
20001008 19:00:00
20001009 09:00:00
20001009 18:00:00
*/

GO
SELECT CONVERT( datetime , DateOnly ) AS DateVal ,
    CONVERT( datetime , TimeOnly ) AS TimeVal
FROM #DateAndTime
ORDER BY DateVal , TimeVal
/*
DateVal         TimeVal        
----------------------- -----------------------
2000-10-08 00:00:00.000 1900-01-01 03:00:00.000
2000-10-08 00:00:00.000 1900-01-01 19:00:00.000
2000-10-09 00:00:00.000 1900-01-01 09:00:00.000
2000-10-09 00:00:00.000 1900-01-01 18:00:00.000
*/


-- Get only rows between 9a-10a:
SELECT * FROM #DateAndTime
WHERE DATEDIFF( hh , TimeOnly , '10:00:00' ) = 1
ORDER BY DateOnly , TimeOnly
/*
DateOnly TimeOnly
-------- --------
20001009 09:00:00
*/


-- Get only rows with today' date:
SELECT * FROM #DateAndTime
WHERE DateOnly LIKE CONVERT( varchar , CURRENT_TIMESTAMP , 112 )
/*
DateOnly TimeOnly
-------- --------
20001009 09:00:00
20001009 18:00:00
*/


-- Get only rows with today' date. This one uses the DATEDIFF function
-- directly on the dateonly column.
SELECT * FROM #DateAndTime
WHERE DATEDIFF( dd , DateOnly , CURRENT_TIMESTAMP ) = 0
/*
DateOnly TimeOnly
-------- --------
20001009 09:00:00
20001009 18:00:00
*/

GO
DROP TABLE #DateAndTime;
GO


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.