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
 General SQL Server Forums
 New to SQL Server Programming
 mssql set time portion to zero on date type?

Author  Topic 

REDTAIL_CODEGUY
Starting Member

17 Posts

Posted - 2011-09-04 : 12:12:34
MS-SQL 2008 R2 is defaulting a "DATE" type field to
01/01/2008 12:00:00AM and I want to set the time to
"00:00:00".

The "01/01/2008" is being passed via @date_field parm to a
stored-procedure. So, in the stored-procedure I would like
to convert that date_field to date-portion + 00:00:00, instead
of what it's defaulting to.

Is there an easy convert statement that will do that?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-04 : 12:54:18
is the target field varchar? if not, it will be displayed as 2008-01-01 00:00:00.000

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2011-09-04 : 15:16:04
Why are you concerned about the visualization of the date and time values?



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

REDTAIL_CODEGUY
Starting Member

17 Posts

Posted - 2011-09-05 : 00:41:18
quote:
Originally posted by visakh16

is the target field varchar? if not, it will be displayed as 2008-01-01 00:00:00.000

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





No, there is no "target" field here. It's being displayed in a msgbox as 12:00:00 AM; And this one doesn't come
from a database either. It's a field generated by a datetimepicker
screen object in vb.net. It's going to be used in a comparison
with dates that have 00:00:00 times on them and I don't want any problems. It's defined as "date" (in ms-sql) as a parm for input to an SQL stored procedure.

So, is there a convert statement or something that I can use to zero out the hh:mm:ss portion of the field? It's okay if I create another field in the process.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-05 : 02:17:21
no need of convert then as 12:00 AM corresponds to 00:00:00 in sql server when storing dates

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

REDTAIL_CODEGUY
Starting Member

17 Posts

Posted - 2011-09-05 : 09:40:17
quote:
Originally posted by visakh16

no need of convert then as 12:00 AM corresponds to 00:00:00 in sql server when storing dates

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





I thought I explained myself pretty well. Let me ask the question again:

Is there a convert statement or something that I can use to zero out the hh:mm:ss portion of a date field?

It's okay if I create another field in the process.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2011-09-05 : 10:43:36
Best to format your date in your application.

But if there is no other way you can do it in SQL Server by converting it to a string and using a format-parameter

CONVERT(varchar(99), MyDate, 999)

were 99 is the width you want, and 999 is the formatting style (see documentation for a list of available types)

You should NOT then pass the date to your application in that format, as it will be a String and not a Date / Datetime object.

But from what you have said your date already has 00:00:00.000 time element - when you view it you are using seeing that as "midnight" in whatever the default display format is. Anything you use, in your application, to display just the date part will not have any time because "midnight" is "zero time"
Go to Top of Page

REDTAIL_CODEGUY
Starting Member

17 Posts

Posted - 2011-09-05 : 11:19:46
quote:
Originally posted by Kristen

Best to format your date in your application.

But if there is no other way you can do it in SQL Server by converting it to a string and using a format-parameter

CONVERT(varchar(99), MyDate, 999)

were 99 is the width you want, and 999 is the formatting style (see documentation for a list of available types)

You should NOT then pass the date to your application in that format, as it will be a String and not a Date / Datetime object.

But from what you have said your date already has 00:00:00.000 time element - when you view it you are using seeing that as "midnight" in whatever the default display format is. Anything you use, in your application, to display just the date part will not have any time because "midnight" is "zero time"



Hi Kristen,

Thanks for the reply. This is not a display issue. This is a comparison issue. In my sql testing (date portions equal) a date from the database with a 00:00:00 time will not compare correctly with a my screen date of 12:00:00 AM. They're not equal. But, I'm working on a VB.net solution right now. I agree, it would be better to fix it on the vb side; it's vb that's producing the midnight dates in the first place...And it's probably easier to do upstream on the vb side.

Best Regards,

- Red
Go to Top of Page

gwilson67
Starting Member

42 Posts

Posted - 2011-09-05 : 11:23:29
Have you tried convert(date, getdate())


Greg
http://www.freewebstore.org/tsqlcoderepository
Powerful tool for SQL Server development
Go to Top of Page

REDTAIL_CODEGUY
Starting Member

17 Posts

Posted - 2011-09-05 : 11:37:40
quote:
Originally posted by gwilson67

Have you tried convert(date, getdate())


Greg
http://www.freewebstore.org/tsqlcoderepository
Powerful tool for SQL Server development



No, but I'll keep that in reserve to try. Right now, I think I've got it solved and I was able to do it in the actual vb screen without any code! Can't do much better than that
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2011-09-05 : 12:22:34
"In my sql testing (date portions equal) a date from the database with a 00:00:00 time will not compare correctly with a my screen date of 12:00:00 AM"

Something wrong with your "screen date" then:

DECLARE @datetime1 datetime, @datetime2 datetime, @date3 date

SELECT @datetime1 = '01/01/2008 12:00:00AM',
@datetime2 = '20080101 00:00:00',
@date3 = '20080101'

SELECT @datetime1, @datetime2, @date3

SELECT [1 and 2] = CASE WHEN @datetime1 = @datetime2 THEN 'Same' ELSE 'Different' END
SELECT [1 and 3] = CASE WHEN @datetime1 = @date3 THEN 'Same' ELSE 'Different' END

Results are that 1&2 are the same, and 1&3 are the same ...
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-05 : 13:02:05
i hope your date comparisons are done in native sql and not on your front end app using date functions available there

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2011-09-05 : 13:11:19
Mine are
Go to Top of Page

REDTAIL_CODEGUY
Starting Member

17 Posts

Posted - 2011-09-05 : 13:33:05
quote:
Originally posted by Kristen

"In my sql testing (date portions equal) a date from the database with a 00:00:00 time will not compare correctly with a my screen date of 12:00:00 AM"

Something wrong with your "screen date" then:

DECLARE @datetime1 datetime, @datetime2 datetime, @date3 date

SELECT @datetime1 = '01/01/2008 12:00:00AM',
@datetime2 = '20080101 00:00:00',
@date3 = '20080101'

SELECT @datetime1, @datetime2, @date3

SELECT [1 and 2] = CASE WHEN @datetime1 = @datetime2 THEN 'Same' ELSE 'Different' END
SELECT [1 and 3] = CASE WHEN @datetime1 = @date3 THEN 'Same' ELSE 'Different' END

Results are that 1&2 are the same, and 1&3 are the same ...



My test had slight differences. I used date in sql, but the database is defined as datetime, which doesn't seem that it should
matter. Anyway, I made a fix in the screen and it's working correctly now... Still not sure exactly where the problem was now. I have to assume the root problem was in the screen.
Go to Top of Page
   

- Advertisement -