dbTalk Databases Forums  

How to pass 'GETDATE()' as SqlParameter

microsoft.public.sqlserver.clients microsoft.public.sqlserver.clients


Discuss How to pass 'GETDATE()' as SqlParameter in the microsoft.public.sqlserver.clients forum.



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

Default How to pass 'GETDATE()' as SqlParameter - 12-30-2007 , 06:52 PM






Is it possible to pass the function GETDATE() as a SqlParameter?

I would like to change:
"UPDATE tablename SET [deleted]=1, [date_modified]=GETDATE(),
[user_modified]=@user WHERE id=@id"

To:
"UPDATE tablename SET [deleted]=1, [date_modified]=@currentdatetime,
[user_modified]=@user WHERE id=@id"

With:
sqlp(1) = New System.Data.SqlClient.SqlParameter("@currentdateti me",
"GETDATE()")


Reply With Quote
  #2  
Old   
Clay
 
Posts: n/a

Default Re: How to pass 'GETDATE()' as SqlParameter - 12-31-2007 , 09:27 AM






Thanks for the response Ekrem. I had considered that as a solution but I
would like to ensure that date/time is accurate by using the date/time of the
server. I don't have control of all the clients environment so it's possible
for the client date/time to be incorrect or in a different time zone.


"Ekrem Önsoy" wrote:

Quote:
If you write your codes as the following:

sqlp(1) = New System.Data.SqlClient.SqlParameter("@currentdateti me",
"GETDATE()")

Then "GETDATE()" will be a string and your app (probably) raise an error as
@currentdatetime will not be a valid datetime (but your app probably expects
a datetime data type) value but a string. It'll be a string when you put
something in quotation marks. Instead, if you write your codes using VB.Net
you should use the following:

("@currentdatetime", Date.Today)

Then, @CurrentDateTime parameter's value will be the date of today.
Getdate() is a function in SQL Server, not in VB.Net for instance...

--
Ekrem Önsoy



"Clay" <Clay (AT) discussions (DOT) microsoft.com> wrote in message
news:00FD93BE-7FD2-42AB-8EF8-B6A8E6CAE0CC (AT) microsoft (DOT) com...
Is it possible to pass the function GETDATE() as a SqlParameter?

I would like to change:
"UPDATE tablename SET [deleted]=1, [date_modified]=GETDATE(),
[user_modified]=@user WHERE id=@id"

To:
"UPDATE tablename SET [deleted]=1, [date_modified]=@currentdatetime,
[user_modified]=@user WHERE id=@id"

With:
sqlp(1) = New System.Data.SqlClient.SqlParameter("@currentdateti me",
"GETDATE()")



Reply With Quote
  #3  
Old   
Hugo Kornelis
 
Posts: n/a

Default Re: How to pass 'GETDATE()' as SqlParameter - 01-02-2008 , 05:15 PM



On Mon, 31 Dec 2007 07:27:01 -0800, Clay wrote:

Quote:
Thanks for the response Ekrem. I had considered that as a solution but I
would like to ensure that date/time is accurate by using the date/time of the
server. I don't have control of all the clients environment so it's possible
for the client date/time to be incorrect or in a different time zone.
Hi Clay,

In that case, you have to make sure that GETDATE() (or rather,
CURRENT_TIMESTAMP), runs at the server.

Best way is to create a stored procedure that you call from the client.
Give it an optional paramter and set it to CURRENT_TIMESTAMP if not
filled from the client:

CREATE PROCEDURE MyProc
@TheDate datetime
AS
BEGIN;
IF @TheDate IS NULL
BEGIN;
SET @TheDate = CURRENT_TIMESTAMP;
END;
-- Do something with @TheDate
END;

If you have to send a query string (please don't if you can avoid it!!),
then you can use COALESCE to replace a null value with CURRENT_TIMESTAMP

SET date_modified = COALESCE(@currentdatetime, CURRENT_TIMESTAMP)

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis


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.