I have select query in SQLite Database. There is a LogInTime field that datatime datatype.
Here is AccessDate variable passing date "11/16/2016" like format
string sql = "SELECT * FROM Tble_Login where LogInTime = '" + AccessDate + "'";
The SQLite Tble_Login looking like this,
After excute this query, no data? How can I get data?
Referring to the SQLite documentaion you should use following format for AccessDate
YYYY-MM-DDor YYYY-MM-DD HH:MM or YYYY-MM-DD HH:MM:SS. On the documentation page you can find more formats in the section Time Strings
You can also try to use the BETWEEN statement like in this question
string sql = #"SELECT * FROM Tble_Login where LogInTime BETWEEN ""2016-11-16"" AND ""2016-11-17""";
Related
I am trying to retrieve some data from MariaDB database to a datagridview in a C# form application. If I am adding a date condition in the query string, it seems like not returning anything. Is it the problem with the date format? Or is it the problem with my query string? Thank you
// selected date is chosen through date time picker
// format is 24/01/2020 10:10:10
selectedDate = dateTimePicker1.Value;
string query = "SELECT Voltage, Current FROM data_table WHERE device_id='" + Device_ID + "' AND data_date >'"+ selectedDate +"'";
//MariaDB date format is 2020-01-24 09:09:09
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;
Try this:
string sqlDate = time.ToString("yyyy-MM-dd HH:mm:ss.fff");
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.
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)
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.
I am currently trying to do a Select in my SQL Server database using a parameter with Datetime type. But I need this parameter only has the format YYYY-MM-DD date because of the following query in SQL I'm using and it's working :
select
idCliente, DescCliente, DescAnalista , StatusChamado , DataAtendimento
from
Atendimento
where
cast ([DataAtendimento] as date) = '2016-04-27';
I read several posts indicating that I use DbType or .ToString but when running it is generating error alleging failure to convert the string to the date / time format.
This is how I use the SqlParameter:
DateTime date;
date = Data;
try
{
sqlClient.Command.Parameters.AddWithValue("#adate", date);
sqlClient.Command.CommandText = #" select idCliente, DescCliente, DescAnalista , StatusChamado , DataAtendimento from Atendimento where cast ([DataAtendimento] as date) = '#adate';";
I need a help from you guys , I'm not finding any means that can perform this select
You have to remove the '' in = '#adate'. With the '' in place, it effectively turns your query into:
select
idCliente, DescCliente, DescAnalista , StatusChamado , DataAtendimento
from Atendimento
where cast ([DataAtendimento] as date) = '#date';
This means that cast([DateAtendimento] as date) is compared against the string '#date', thus the conversion to date error.
I am using mvc and I want to insert a date to an oracle database. I stored the value
in an object then add it to a collection (processed_date). I then used the insert statement
to write to the database.
All I get it invalid date format. Do you have any idea how I can fix this? I need to write the exact date "31/12/2099" to the Oracle database.
object col14Value = "31/12/2099";
processed_date = (col14Value).ToString()
string sqlIns = "insert into price_line (processed_date) values (to_date(:processed_date, mm/dd/yyyy)
The property of the processed_date looks like this
public string processed_date { get; set; }
Now I am begining to the the error below
[Oracle.DataAccess.Client.OracleException] = {"ORA-01843: not a valid month"}
mm/dd/yyyy should be encased in quotes - 'mm/dd/yyyy'. It is an Oracle string. Also if your date is 31/12/2099 then your format string should be 'dd/mm/yyyy'
I am using an Odbc driver with Paradox. I have a table with a date in it. I am trying to query by the date in that column. I can't seem to get the where clause to work. I can get the record searching by ints, but I don't know how to use the date time.
OdbcCommand comm= new OdbcCommand("SELECT * FROM [Journal] WHERE" +
"[Date] = 04/02/2009 ",
new OdbcConnection(#"Driver={Microsoft Paradox Driver (*.db )};DriverID=538;Fil=Paradox 5.X;DefaultDir=d:\\ics\\tables\\HISTORY;Dbq=d:\\ics\\tables\\HISTORY;CollatingSequence=ASCII"));
OdbcDataAdapter adapt = new OdbcDataAdapter(comm);
DataTable table = new DataTable();
adapt.Fill(table );
Ok I figured it out.
...where [Date] = {d 'yyyy-MM-dd'} AND...
In odbc you have to use the {} to define an object, and the d defines the format of the date.
You can use the following.
{d 'value'} yyyy-mm-dd
{t 'value'} hh:mm:ss
{ts 'value'} yyyy-mm-dd hh:mm:ss
Look for Date/Time functions in Paradox that will convert a string to a date. It is very tricky to use a String as a date like in your SQL. There must be some functions like TO_DATE or similar that will properly format it for you.