-1

How to add parameters in a SQL select query?

string time = 2013-09-25 00:00:00;

I wish to use the time variable in the below mentioned SQL query

Select LastUpdated from Employee where LastUpdated > time;
m_alpha
  • 134
  • 13
  • 1
    Pass it in as a parameter with a `datetime` data type. – Gordon Linoff Aug 26 '19 at 10:28
  • 5
    Is there a specific reason why the variable is of type `string` and not of type `DateTime` or `DateTimeOffset`? – Klaus Gütter Aug 26 '19 at 10:30
  • why GetUpdatedTime() returns string. change it to return DateTime. Don't bother to convert here – Md. Tazbir Ur Rahman Bhuiyan Aug 26 '19 at 10:39
  • Possible duplicate of [DateTime format to SQL format using C#](https://stackoverflow.com/questions/17418258/datetime-format-to-sql-format-using-c-sharp) – Sheraz Ahmed Aug 26 '19 at 10:51
  • @SherazAhmed That link is for converting datetime to string, I think you haven't understood the at all. – m_alpha Aug 26 '19 at 11:08
  • @KlausGütter That's a part of the existing code and the string variable is used multiple times, I'm just trying to add a new feature so I wish to have the conversion from string to datetime. – m_alpha Aug 26 '19 at 11:14
  • Please let me know why you people are down voting this question so that next time I can improve – m_alpha Sep 17 '19 at 10:27
  • @GordonLinoff Thanks a lot for your suggestion. I was trying my hands on on a C#(new to me) project so couldn't figure out what exactly you meant. – m_alpha Nov 18 '19 at 08:27

4 Answers4

0

Try this:

string sqlDate = time.ToString("yyyy-MM-dd HH:mm:ss.fff");
Sheraz Ahmed
  • 405
  • 1
  • 4
  • 20
  • Kindly have a look at the question once again, your answer is no where close to what the question is. – m_alpha Aug 26 '19 at 11:05
  • @m_alpha, Help me understand it then. string time = GetUpdatedTime(); time = 2013-09-25 00:00:00 is this C# code or SQL ? – Sheraz Ahmed Aug 26 '19 at 11:11
0

You can convert your string in C# code to DateTime using DateTime.TryParse() or Convert.ToDateTime()

OR

Convert VARCHAR to DATETIME in SQL using Convert(datetime, time)

Aakanksha
  • 329
  • 2
  • 7
0

It appears what the OP is asking is how to convert a VARCHAR to a DATETIME in SQL Server not actually a String to DateTime in C#.

You will need to use the following to convert to a DATETIME:

SELECT LastUpdated 
FROM Employee 
WHERE LastUpdated > CONVERT(datetime, varTime, 121);

See the following MS Reference for more information.

To echo others though, you should just pass the parameter in as a datetime and let the database provider factory handle the conversion of appropriate types, or add a new method that actually returns a DateTime. In the future, I wouldn't name a method GetUpdateTime unless it actually returns a type of Time.

gmiley
  • 6,531
  • 1
  • 13
  • 25
  • Hi, I tried the same i.e `CONVERT(datetime, varTime, 121)` but compiler in this case assumes `varTime ` as a column in the bd and thus throws error. In my case I have varTime as a variable of type string in C# code and I wish to use the value of that variable to be used in my SQL query. i.e In `CONVERT(datetime, varTime, 121)` I want the value of variable `varTime` to be converted to type datetime. – m_alpha Aug 27 '19 at 07:10
  • @m_alpha How are you preparing/executing your query within your C# application? Can you provide the code as an edit to your question, and let me know when you have? – gmiley Aug 27 '19 at 11:13
0

I just framed my question in a wrong, the query remains the same though. I just wanted time to be added as a paramter in my SQL-query. The code for the same looks like

String commandText = "Select LastUpdated from Employee where LastUpdated > :time;";
OracleConnection connection = new OracleConnection(connectionString);
OracleCommand command = new OracleCommand(commandText, connection);
command.Parameters.Add("time", time);

Thanks a lot for your help! My bad that I couldn't frame the question properly.

m_alpha
  • 134
  • 13