Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

How to pass 'GETDATE()' as SqlParameter

 
   Database Forums (Home) -> Client RSS
Next:  Long Transaction  
Author Message
Clay

External


Since: May 26, 2006
Posts: 2



(Msg. 1) Posted: Sun Dec 30, 2007 4:52 pm
Post subject: How to pass 'GETDATE()' as SqlParameter
Archived from groups: microsoft>public>sqlserver>clients (more info?)

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("@currentdatetime",
"GETDATE()")

 >> Stay informed about: How to pass 'GETDATE()' as SqlParameter 
Back to top
Login to vote
Ekrem_Önsoy

External


Since: Aug 28, 2007
Posts: 179



(Msg. 2) Posted: Mon Dec 31, 2007 1:00 am
Post subject: Re: How to pass 'GETDATE()' as SqlParameter [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

If you write your codes as the following:

sqlp(1) = New System.Data.SqlClient.SqlParameter("@currentdatetime",
"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" wrote in message

> 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("@currentdatetime",
> "GETDATE()")
>

 >> Stay informed about: How to pass 'GETDATE()' as SqlParameter 
Back to top
Login to vote
Clay

External


Since: May 26, 2006
Posts: 2



(Msg. 3) Posted: Mon Dec 31, 2007 7:27 am
Post subject: Re: How to pass 'GETDATE()' as SqlParameter [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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:

> If you write your codes as the following:
>
> sqlp(1) = New System.Data.SqlClient.SqlParameter("@currentdatetime",
> "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" wrote in message
>
> > 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("@currentdatetime",
> > "GETDATE()")
> >
>
 >> Stay informed about: How to pass 'GETDATE()' as SqlParameter 
Back to top
Login to vote
Hugo Kornelis

External


Since: Jan 31, 2006
Posts: 335



(Msg. 4) Posted: Wed Jan 02, 2008 7:00 pm
Post subject: Re: How to pass 'GETDATE()' as SqlParameter [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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

>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
 >> Stay informed about: How to pass 'GETDATE()' as SqlParameter 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
User Pass. - I would appreicate if any one can let me know how I can findout the database user password. I don't wanna change the password, I have a system administrative rights in SQL Server, is there any way to findout that. Thanks

problem deploying access project - I have given a copy of my access project to user. This was developed in Access 2003 in Access 2000 file format. User PC is running Access 2000 ( 904402 SR-1). The problem seems to be that Functions are not visible to thic client so queries and forms..

Why the table is locked ? - Dear all, I have Access Font End to SQL server Back end. In an Access form, I use combobox, based on a stored procedure, which based on 1 table (SELECT LocationID, LocationDescription FROM Locations). When I open the form and check in the SQL server....

SQL problem pls help - I have installed Visual Studio 2005 beta 2 that include the SQL Express. Then I installed the SQLClient to my pc too. And when I click on the sql group An error message pops up "A connection could not be established to Computername\sqlexpress. ....

enterprise manager error - Hi, When trying to modify or add a table using enterprise manager I got this message error: Enterprise manager: not enough storage is available to complete this operation. I have a lot off space available. I don't have a memory leake. My server is..
   Database Forums (Home) -> Client All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]