I have some very odd behaviour from a MySQL query in my C# application which is pulling rows from a Table called project_info. It should retrieve all the Rows which Equal the Customer's Name, and the project start date is BETWEEN the DATE range in the query. The current query I am using works but the date range appears to behave very oddly...
For example if I have a Row in my Table which contains a project for Customer 'WHSmith' and they have a project with a start date of '30/07/2018' when I search with a date range BETWEEN 18/06/2018 AND 01/08/2018 the query returns no results, but if I repeat the query using a date range BETWEEN 18/06/2018 AND 02/08/2018 the Row is returned???
The query I currently have is:
SELECT * FROM project_info WHERE cust_name = "WHSmith" AND proj_date BETWEEN 2018-06-18 AND 2018-08-01;
The date is stored as a DATE field in the table so no Time is included in the query or the value... The cust_name and dates are held in variables within my application but I have checked these are correct and seem OK.
Does anyone have any idea why the BETWEEN query is behaving so oddly? or maybe suggest a better way to look for Row's in a DATE range... many thanks.
Related
So I'm working on a c# project that creates row in a table with a datetime column and then extract that auto generated ID of that column afterwards. My way of extracting the ID is through the code below the problem is the length it takes to execute the first query and then the second query is more than 1 second so I end up getting empty rows. So how do I insert or select rows minus the seconds?
INSERT INTO transactionlog(transactionDate) VALUES(NOW())
AND THEN IMMEDIATELY THIS
SELECT transactionID,transactionDate FROM transactionlog WHERE transactionDate=NOW();
NOW() gives you the date/time when the statement it contains started. While it is guaranteed that several invokations in the same query return the same value, this is not true accross queries.
To get the auto-incremented value generated during an insert, you can use LAST_INSERT_ID() instead:
INSERT INTO transactionlog(transactionDate) VALUES(NOW());
SELECT LAST_INSERT_ID();
You can utilize mysql DATE_FORMAT() function to control to format.
SELECT transactionID,transactionDate FROM transactionlog WHERE transactionDate=DATE_FORMAT(NOW(), '%c/%e/%Y %H:%i');
You can have a look here at: https://www.w3resource.com/mysql/date-and-time-functions/mysql-date_format-function.php
if you want to play with the format specifier
I have a table called Jobs which has several columns, One of the column being ProofDate Column. Now the Column shows the date and as well as time.. I want to display the entire table in a tab of windows form application along with the ProofDate..But The proof Date must show only the Date and not the time..Can anyone help me with it??
Select CONVERT(DATE, ColumnName) From Table
Would give you just the date of a column which is datetime
Applications and databases work best when you leave DateTime as complete DateTime objects, and the best time to change that format is when it is time to display the information
Leave the SQL query alone, use one of the DateTime.ToString methods to just the Date portion. There are plenty of canned methods as well; such as ToShortDateString().
if your ProofDate column is datetime as datatype then you can use below query.
select col1, col2, col3, convert(date,ProofDate ) as colname from table
I have a data table that contains dates in a format of yyyymmdd. I would like to add a datetime column to the table that contains the date value of that column.
Now the problem is that the table contains a lot of records and looping through all the records takes some time. Are there a way to do a bulk update or a use the Expression as from this post?
Something like:
table.Columns.Add("MyDate", typeof(DateTime)).Expression =
DateTime.ParseExact("'strDateField'", "yyyymmdd", new CultureInfo("en-US"));
(Statement above does not compile which is expected, but only posted as to provide an example of what I'm trying to achieve)
Expression use a limit set of SQL functions exposed by .net for operations on data table columns, refer https://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression(v=vs.110).aspx
You can try an arrangement like below
dt.Columns.Add("MyDate", GetType(Date)).Expression = "SUBSTRING(strDateField,5,2)+'/'+SUBSTRING(strDateField,7,2)+'/'+SUBSTRING(strDateField,1,4)";
I have a database with tables named Mark, Mask, Matk, Mauk
Each tables have some columns and a column named date where the current date is stored in the php's date("h:i:sa d/m/Y") format.
Now I want to select the table names whose last column's date is less than 5 mins away from now.
I hope you understand my above statement.
I know I would have to do this for the last part: TIMESTAMP(NOW())-TIMESTAMP(Last columns date) < 300.
However I couldn't do the other parts.
P.S I am using the database in a C# app.
EDIT:I'm guessing the code should be something like this:SHOW TABLES FROM db WHERE NOW()-SELECTdateFROM TABLES ORDER BY date DESC LIMIT 1 <= 300
How about leaving out the timestamp part?
where LastColumnDate >= now() - interval 5 minute
I am assuming you are using MySQL based on your sample code.
I am doing a project in school, I have to create a website tool for salesmen to fill what they have done during the day, i.e. amount of quotes, quote sum, orders, order sum etc. I am using Visual Studio 2010, ASP.NET with C# with a SQL database.
I have to create a table with different columns, that I know how. But what I need is to have a column called Date and it has the datatype date. I need it to be filled automatically without having to input it manually. The same date that the new information was added. I have searched for solution in google and other places but I think I am searching with the wrong keywords, hopefully you can help me.
The format I wish for the date to be is DD-MM-YYYY
When you look for SQL default date on Google, the second result you get is this one.
In there, you have a default date example:
CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
OrderDate date DEFAULT GETDATE()
)
using the DEFAULT keyword.
Create a sql datetime column in the database, and specify a default value of GetDate() or GetUtcDate() depending on which you want. Format is irrelevant on the input side; you will have to use a formatter on the select side (or in your c# code).
You can set the default value for the column as current date time..
create table tblname (
fieldname datetime default getdate()
)
Also see this question
Add default value of datetime field in SQL Server to a timestamp
You can use one of this to insert in the table instead.
String s = System.DateTime.Now.ToString("dd.MM.yyyy");
DateTime now = System.DateTime.Now;
The second one would be your choice because the type specified in yur table is Date.
If don't want to be setting it from the app, specify which database you are using to get a specific answer.