Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Lijo Cheeran Joseph
Posting Yak Master
123 Posts |
Posted - 2010-10-26 : 04:34:08
|
Hi Team,I need to list all records for which EndDate is having a time other than “00:00:00”. The following query works. Is there any better query?SELECT CASE WHEN CONVERT(DATETIME,'1/1/2010 ' + convert(varchar(10), EndDate, 108)) > CONVERT(DATETIME,'1/1/2010 00:00:00') THEN 'Exceeds' ELSE 'Under Limit'END AS Result FROM ProcessWHERE CONVERT(DATETIME,'1/1/2010 ' + convert(varchar(10), EndDate, 108)) > CONVERT(DATETIME,'1/1/2010 00:00:00')Please advise if there is alternative better query.ThanksLijo |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-10-26 : 04:44:16
|
where EndDate > convert(datetime,'20100101')and EndDate < convert(datetime,'20100102') No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
Lijo Cheeran Joseph
Posting Yak Master
123 Posts |
Posted - 2010-10-26 : 05:14:24
|
Sorry, it does NOT work.I need to display the record with date '1/2/2010 12:34:34'.But the record '1/2/2010 00:00:00' should not be displayed.Its the time part (only) that matters; not date.Any other solution?ThanksLijo |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-10-26 : 05:50:22
|
[code]declare @Sample table (id int identity(1,1), EndDate datetime)insert @Sampleselect '2010-01-01 12:34:34' union allselect '2010-01-01 10:30:00' union allselect '2010-01-01 00:00:00'-- show all recordsselect CASE WHEN EndDate > convert(datetime,'20100101') and EndDate < convert(datetime,'20100102') THEN 'time part is not zero' ELSE 'time part is zero'END as RESULT,*from @Samplewhere EndDate >= convert(datetime,'20100101')and EndDate < convert(datetime,'20100102')-- show only records with time part not zeroselect CASE WHEN EndDate > convert(datetime,'20100101') and EndDate < convert(datetime,'20100102') THEN 'time part is not zero' ELSE 'time part is zero'END as RESULT,*from @Samplewhere EndDate > convert(datetime,'20100101')and EndDate < convert(datetime,'20100102')-- show only records with time part is zeroselect CASE WHEN EndDate > convert(datetime,'20100101') and EndDate < convert(datetime,'20100102') THEN 'time part is not zero' ELSE 'time part is zero'END as RESULT,*from @Samplewhere EndDate = convert(datetime,'20100101')-- result show allRESULT id EndDate--------------------- ----------- -----------------------time part is not zero 1 2010-01-01 12:34:34.000time part is not zero 2 2010-01-01 10:30:00.000time part is zero 3 2010-01-01 00:00:00.000-- result show only records with time part not zeroRESULT id EndDate--------------------- ----------- -----------------------time part is not zero 1 2010-01-01 12:34:34.000time part is not zero 2 2010-01-01 10:30:00.000-- result show only records with time part is zeroRESULT id EndDate--------------------- ----------- -----------------------time part is zero 3 2010-01-01 00:00:00.000[/code] No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-10-26 : 06:14:48
|
[code]DECLARE @Sample TABLE ( ID INT IDENTITY(1, 1), EndDate DATETIME )INSERT @SampleSELECT '20100101 12:34:34' UNION ALLSELECT '20100101 10:30:00' UNION ALLSELECT '20100101 00:00:00'SELECT *FROM @SampleWHERE DATEADD(DAY, DATEDIFF(DAY, EndDate, 0), EndDate) <> 0[/code] N 56°04'39.26"E 12°55'05.63" |
 |
|
Sachin.Nand
2937 Posts |
Posted - 2010-10-26 : 06:21:00
|
If using SQL 2008 thenselect * from @Sample where convert(time,EndDate)<>'00:00:00' PBUH |
 |
|
jcelko
Esteemed SQL Purist
547 Posts |
Posted - 2010-10-26 : 12:44:35
|
Play with the new TIME data type:SELECT CAST ('2010-01-01 12:34:34' AS TIME),CAST ('2010-01-01 10:30:00' AS TIME),CAST ('2010-01-01 00:00:00' AS TIME),CAST ('2010-01-01' AS TIME);--CELKO--Books in Celko Series for Morgan-Kaufmann PublishingAnalytics and OLAP in SQLData and Databases: Concepts in Practice Data, Measurements and Standards in SQLSQL for SmartiesSQL Programming Style SQL Puzzles and Answers Thinking in SetsTrees and Hierarchies in SQL |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-10-26 : 13:33:45
|
This is a 2005 forum... No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-10-26 : 15:39:58
|
Small correction to Peso's solutin:SELECT *FROM @SampleWHERE DATEADD(DAY, DATEDIFF(DAY, 0, EndDate), 0) <> EndDate |
 |
|
|
|
|
|
|