Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7894661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:27:10+00:00 2026-06-03T07:27:10+00:00

(Related to this post , but not the same.) Is there any way to

  • 0

(Related to this post, but not the same.)

Is there any way to use a SqlParameter to specify that a value should be obtained by calling a function at the server? E.g.

private void Test()
{
    var cmd = new SqlCommand("update Table set ADateField = @p1 where Id = @id");
    cmd.Parameters.AddWithValue("@id", 42);
    cmd.Parameters.AddWithValue("@p1", "sysutcdatetime()");  // WRONG!!
    cmd.ExecuteNonQuery();
}

I can embed the call in the CommandText string, but sometimes I want to set an explicit value rather than call the function so it gets a bit messy.

I do not want to get the date on the client machine as this may be different than the date at the server due to time zone differences.

From the docs I can’t see how it can be done but thought I would check with you guys before giving up. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:27:12+00:00Added an answer on June 3, 2026 at 7:27 am

    I don’t believe this can be done. Two options are to either rewrite your SQL statement depending on whether you will be passing an exact date or not, or embed the SQL inside a stored procedure which can then check the passed in parameter to see if it is null or something and substitute the correct function calls:

    var cmd = new SqlCommand("update Table set ADateField = @p1 where Id = @id");
    cmd.Parameters.AddWithValue("@id", 42);
    if( useFunction )
    {
      cmd.CommandText = cmd.CommandText.Replace("@p1", "sysutcdatetime()");
    }
    else
    {
      cmd.Parameters.AddWithValue("@p1", exactDate);  
    }
    cmd.ExecuteNonQuery();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(This question is related - but not the same - to this one) This
I looked at previous post based on this but they do not relate. I
This question is related to a previous post . Is there something comparable to
This question is related with one of my earlier questions.. Previous Post In there
Author post-edit: Chosen solution (Original question remains below this box) SUMMARY: You SHOULD NOT
First off I apologize if there is another post out there that answers this,
This is a related post to this one as it deals with the same
Not sure if this is related to this question (seems to be but I
This is related to this post . I think I am having problem with
This question is related to this SO post Rather than using a recursive CTE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.