dbTalk Databases Forums  

MS SQL getDate() Function remove Time

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss MS SQL getDate() Function remove Time in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Yas
 
Posts: n/a

Default MS SQL getDate() Function remove Time - 07-04-2007 , 09:53 AM






Hi,

I am creating creating a table with a Date column dd-mm-yyyy. But I
cant seem to find a SQL function that just returns today's date.
getDate() returns the time as well so I cant use it.

The reason is simply that I want to update/overwrite over and over
again all records from current day but not touch the ones from
yesterday etc and with the timestamp in there I just end up adding
more and more rows for the same day.

In other words I only want to preserve rows are from yesterday or
older but overwrite ones from today.


Any help will be appricated.

Thank you!

Yas


Reply With Quote
  #2  
Old   
Roy Harvey
 
Posts: n/a

Default Re: MS SQL getDate() Function remove Time - 07-04-2007 , 10:14 AM






To remove the time from a datetime such as getdate():

SELECT dateadd(day,datediff(day,0,getdate()),0)

I strongly suggest not storing a date column as a string. Use a
datetime and just set the time to zeroes if you only need the date.

I even more strongly suggest not using the format dd-mm-yyyy if you do
store a date as a string. YYYYMMDD is much more versatile. For one
thing it will always convert to the datetime type correctly regardless
of country, and for another it will sort in calendar order.

Roy Harvey
Beacon Falls, CT

On Wed, 04 Jul 2007 07:53:34 -0700, Yas <yasar1 (AT) gmail (DOT) com> wrote:

Quote:
Hi,

I am creating creating a table with a Date column dd-mm-yyyy. But I
cant seem to find a SQL function that just returns today's date.
getDate() returns the time as well so I cant use it.

The reason is simply that I want to update/overwrite over and over
again all records from current day but not touch the ones from
yesterday etc and with the timestamp in there I just end up adding
more and more rows for the same day.

In other words I only want to preserve rows are from yesterday or
older but overwrite ones from today.


Any help will be appricated.

Thank you!

Yas

Reply With Quote
  #3  
Old   
Stephen2
 
Posts: n/a

Default Re: MS SQL getDate() Function remove Time - 07-04-2007 , 10:41 AM



On Jul 4, 3:53 pm, Yas <yas... (AT) gmail (DOT) com> wrote:
Quote:
Hi,

I am creating creating a table with a Date column dd-mm-yyyy. But I
cant seem to find a SQL function that just returns today's date.
getDate() returns the time as well so I cant use it.

The reason is simply that I want to update/overwrite over and over
again all records from current day but not touch the ones from
yesterday etc and with the timestamp in there I just end up adding
more and more rows for the same day.

In other words I only want to preserve rows are from yesterday or
older but overwrite ones from today.

Any help will be appricated.

Thank you!

Yas
AFAIK a there is no DATE type in MS SQL, only DATETIME so you cannot
store only the date part.
You can use SELECT CONVERT(VARCHAR(8),datevalue,112) to return the
datetime in YYYYMMDD format without the time but it's stored as a
VARCHAR not a DATETIME.



Reply With Quote
  #4  
Old   
Yas
 
Posts: n/a

Default Re: MS SQL getDate() Function remove Time - 07-04-2007 , 10:55 AM



On 4 Jul, 17:14, Roy Harvey <roy_har... (AT) snet (DOT) net> wrote:
Quote:
To remove the time from a datetime such as getdate():

SELECT dateadd(day,datediff(day,0,getdate()),0)

I strongly suggest not storing a date column as a string. Use a
datetime and just set the time to zeroes if you only need the date.
Hi I am storing the coumn as datetime and not string. However, using
the above suggestion (day,datediff(day,0,getdate()),0) I get a column
with Date+Time set to Zeros. OK, but the problem is when I run the
update/insert records command again It doesn't overwrite the columns
with today's date, its as if sql is secretly inserting the time by it
self and even though to my eyes the rows is exactly the same SQL adds
a new row thinking it is distinct.

I would like that if the table had a rowOld with: (ColValue1,
ColValue2,2007-07-04 00.00.00.000)
If I use the above suggestion and insert a rowNew with same values
(ColValue1, ColValue2, 2007-07-04 00.00.00.000)
....It should overwrite rowOld with rowNew, not insert rowNew as a new
row.
....and only insert as a new rowNew2 when this row has a different date
eg. 2007-07-05 00.00.00.000

I thought it would as time is now set to Zeros, but it doesn't. Is SQL
marking each row in the examples above with a time stamp? even though
it is not shown in the row value?


Thanks again :-)

Yas




Reply With Quote
  #5  
Old   
Roy Harvey
 
Posts: n/a

Default Re: MS SQL getDate() Function remove Time - 07-04-2007 , 12:01 PM



On Wed, 04 Jul 2007 08:55:49 -0700, Yas <yasar1 (AT) gmail (DOT) com> wrote:

Quote:
I would like that if the table had a rowOld with: (ColValue1,
ColValue2,2007-07-04 00.00.00.000)
If I use the above suggestion and insert a rowNew with same values
(ColValue1, ColValue2, 2007-07-04 00.00.00.000)
...It should overwrite rowOld with rowNew, not insert rowNew as a new
row.
...and only insert as a new rowNew2 when this row has a different date
eg. 2007-07-05 00.00.00.000
You can write an INSERT for a new row, or an UPDATE for an existing
row, but you have to choose which it is to be. In your case you have
to find out if the row exists and then run INSERT or UPDATE depending
on what you find.

Microsoft is adding MERGE to the next release of SQL Server, which
would allow you to write one command to accomplish both functions, but
it is not available today.

Roy Harvey
Beacon Falls, CT


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.