0

I'm trying to figure out if it's possible to format a string from a sourceColumn when adding a parameter to a SqlCommand.

So something like this

cmd.Parameters.Add("@sdate", SqlDbType.VarChar, 25, "start_date");

I don't know how the syntax would be if this would be possible. Something like:

cmd.Parameters.Add("@sdate", SqlDbType.VarChar, 25, string.Format("{0} {1}", "start_date", "12:00 AM"));

I'm sure this wouldn't work, because then it would just look for a column name of "start_date 12:00 AM"

Hopefully it makes sense what I'm trying to do.

Thanks!

5
  • To what are you adding that parameter (a query-based command, executing a sproc, ...)? I'm guessing it's typed varchar(25) (since that's what you say). Are you just asking "can I put an expression containing a function like string.Format in as a value for a parameter?" If so, "yes". I don't understand your statement that starts "I'm sure this wouldn't work". Why wouldn't it? Have you tried? Commented Jul 19, 2018 at 15:32
  • This is a column sourced parameter for a SqlAdapter. In the first line of code, it is done properly, with the assumption that the source for the query is a DataTable with a column named "start_date". In that DataTable, there is no column named "start_date 12:00 AM" which is what it would look for if I were to do it the second way, which would certainly not work. Commented Jul 19, 2018 at 15:35
  • Sorry, I got the parameters to Parameters.Add wrong in my head. Do you want to set the value of "start_date" to "start_date 12:00 AM"?. Then add the parameter and immediately set the value to your string.Format expression Commented Jul 19, 2018 at 15:39
  • I may be misunderstanding your question, but to clarify, I'm trying to add the time component to the value that's contained within the start_date column. For example, if the value was "7/19/2018", I'd want to append the time to become "7/19/2018 12:00 AM" Commented Jul 19, 2018 at 15:50
  • Look at this: stackoverflow.com/questions/293311/… Commented Jul 19, 2018 at 15:51

1 Answer 1

0

I haven't found an answer to the question, but one solution that I went with was to just format the date before putting it into the DataTable. That way, I can just do a straight insert of the column value as is, and don't need to do any formatting during the insert.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.