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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Better Query?

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 Process
WHERE 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.

Thanks
Lijo

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.
Go to Top of Page

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?

Thanks
Lijo
Go to Top of Page

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 @Sample
select '2010-01-01 12:34:34' union all
select '2010-01-01 10:30:00' union all
select '2010-01-01 00:00:00'

-- show all records
select
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 @Sample
where EndDate >= convert(datetime,'20100101')
and EndDate < convert(datetime,'20100102')

-- show only records with time part not zero
select
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 @Sample
where EndDate > convert(datetime,'20100101')
and EndDate < convert(datetime,'20100102')

-- show only records with time part is zero
select
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 @Sample
where EndDate = convert(datetime,'20100101')



-- result show all

RESULT id EndDate
--------------------- ----------- -----------------------
time part is not zero 1 2010-01-01 12:34:34.000
time part is not zero 2 2010-01-01 10:30:00.000
time part is zero 3 2010-01-01 00:00:00.000

-- result show only records with time part not zero
RESULT id EndDate
--------------------- ----------- -----------------------
time part is not zero 1 2010-01-01 12:34:34.000
time part is not zero 2 2010-01-01 10:30:00.000


-- result show only records with time part is zero
RESULT 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.
Go to Top of Page

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 @Sample
SELECT '20100101 12:34:34' UNION ALL
SELECT '20100101 10:30:00' UNION ALL
SELECT '20100101 00:00:00'

SELECT *
FROM @Sample
WHERE DATEADD(DAY, DATEDIFF(DAY, EndDate, 0), EndDate) <> 0[/code]


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-10-26 : 06:21:00
If using SQL 2008 then



select * from @Sample where convert(time,EndDate)<>'00:00:00'



PBUH

Go to Top of Page

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 Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page

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.
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-10-26 : 15:39:58
Small correction to Peso's solutin:
SELECT	*
FROM @Sample
WHERE DATEADD(DAY, DATEDIFF(DAY, 0, EndDate), 0) <> EndDate
Go to Top of Page
   

- Advertisement -