SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 isnull on datetime column
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

patshaw
Posting Yak Master

177 Posts

Posted - 10/27/2006 :  08:47:39  Show Profile  Reply with Quote
Hi,

The following code:
SELECT ISNULL(s.LogOnTime,'00:00:00) as LogOnTime
FROM users

returns:
1900-01-01 00:00:00.000
for the column where it is null. Any other value I use just returns an error. Is there any way I can get the column to return '00:00:00' where it is null?

khtan
In (Som, Ni, Yak)

Singapore
17586 Posts

Posted - 10/27/2006 :  08:53:57  Show Profile  Reply with Quote
SELECT ISNULL(s.LogOnTime,'00:00:00) as LogOnTime
FROM   users


It's correct. Leave it as it is and format the LogOnTime in your front end.

alternatively use convert with style 108


KH

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30113 Posts

Posted - 10/27/2006 :  09:00:00  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Yes. But you have to cast all values to varchar, which is a BAD idea.
"Zero" date for SQL server is January 1, 1900.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22744 Posts

Posted - 10/27/2006 :  09:00:30  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
>>Is there any way I can get the column to return '00:00:00' where it is null?

Where do you want to show data?
If you use '' or '00:00:00', you will get the same result
If you use front end application, do this there

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

patshaw
Posting Yak Master

177 Posts

Posted - 10/27/2006 :  09:28:09  Show Profile  Reply with Quote
I've been told by a colleague that this can be done as a UDF. Is this true and if so how can do I write the UDF. I have no experience with them.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30113 Posts

Posted - 10/27/2006 :  09:36:17  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Tell your colleague he's/she's yanking your chains.
This is so easy that no UDF is needed.

First, have you read and understood all previous answers here?
Second, use this
SELECT	ISNULL(CONVERT(varchar, s.LogOnTime, 108), '00:00:00') as LogOnTime
FROM	users


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

patshaw
Posting Yak Master

177 Posts

Posted - 10/27/2006 :  10:37:29  Show Profile  Reply with Quote
Hi Peso,

Yes, I see now and it works great. This now presents me with another problem.

The SELECT is also returning some non-NULL dates. These non-NULLs should be represented as '2006-10-27 10:11:19.177' format but since applying your solution above they are now returning as '27 Oct 2006 10:11:19:177' which I have been told is not acceptable for the client. I have tried various formatting options but cannot get the non-NULLS to show as '2006-10-27 10:11:19.177'. Can you advise me further on this please.
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17586 Posts

Posted - 10/27/2006 :  10:46:15  Show Profile  Reply with Quote
1. If you use Peter's code, the non-NULL dates will not return as '2006-10-27 10:11:19.177' but '10:11:19'. That's the formatted result of style 108.

2. if the acceptable format is YYYY-MM-DD HH:MM:SS then you should use convert with style 121
SELECT	ISNULL(CONVERT(varchar(23), s.LogOnTime, 121), '00:00:00') as LogOnTime
FROM	users


EDIT :
Please refer to the Books Online on CAST and CONVERT

KH


Edited by - khtan on 10/27/2006 10:47:06
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30113 Posts

Posted - 10/27/2006 :  10:48:20  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Books Online is your friend
SELECT	ISNULL(CONVERT(varchar, s.LogOnTime, 121), '00:00:00') as LogOnTime
FROM	users


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30113 Posts

Posted - 10/27/2006 :  10:49:07  Show Profile  Visit SwePeso's Homepage  Reply with Quote


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

patshaw
Posting Yak Master

177 Posts

Posted - 10/27/2006 :  11:00:32  Show Profile  Reply with Quote
Much apprciated guys, Thanks.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.07 seconds. Powered By: Snitz Forums 2000