How do I insert into a datetime column minus the seconds? - c#

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

Related

mysql DATE range BETWEEN Query

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.

Bulk update column in Data Table based on another column

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)";

Complex sql statement for showing the table names

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.

Query on Identity Column on MS SQL DB

I have created table in MS SQL 2008 with one identity column(Start Value - 1 and Increment is also 1) and 4 other columns. I am accessing this DB from C# ASP.NET. Used to push data only for the non identity column. Identity column will auto increment itself.
As of now i am manually querying the column value with the remaining for columns. But I am facing problem if all the other four column values are equal i am not getting the exact value which i am looking for
Now my query is, Is there any why in C# where I can get the value of the newly created identity column whenever new record is created.
Thanks.
You can use
SCOPE_IDENTITY()
Which will returns the primary key value of the recently inserted row
The answer to your question actually lies in SQL Server. You can run:
SELECT ##identity
after your insert to get the last inserted row identity.
http://technet.microsoft.com/en-us/library/aa933167(v=sql.80).aspx
EDIT BASED ON COMMENTS:
Consider using SCOPE_IDENTITY() as referenced here:
http://technet.microsoft.com/en-us/library/aa259185(v=sql.80).aspx
In SQL terms you can output the records back if you wish it. But how you might apply this to C# is up to you. Example:
INSERT INTO TABLE_A (SOMETHING, SOMETHINGELSE, RANDOMVAL3)
OUTPUT inserted.A_ID, inserted.SOMETHING, inserted.SOMETHINGELSE, inserted.RANDOMVAL3
SELECT 'ASD','DOSD', 123
But unless you're using merge, you can't use OUTPUT to print out any values from joining tables from an INSERT. But that's another matter entirely, I think.
Also, it's hardly good practice to bounce this data between the application and the DB all the time, so I'd look to alternatives if possible.

How to include an 'order' or 'index' column in a SELECT query?

I'm using Access SQL. I want to add a column to my query that acts like a row number for each record, but because I'm using an aggregate function, the results have not ids themselves. Is there any function that generate some row numbers for this ? even like Autonumber or index or just the order. So my dummy SQL syntax is like:
SELECT [wanted autonumber column], product,Sum(amount) FROM Invoices_Items GROUP BY product
I guessed maybe it would be good if I create a temporary table for this query with an autonumber column but I don't know how to that.
If you save the GROUP BY SQL as a named query in Access, you can use that as the data source for another SELECT statement which uses a correlated subquery to generate a row number.
So with this SQL saved as qryInvoices_Items1 ...
SELECT i.product, Sum(i.amount) AS SumOfamount
FROM Invoices_Items AS i
GROUP BY i.product;
This query will add a dynamic row number --- the row number for a given product can be different from one run to the next if the underlying Invoices_Items data changes.
SELECT
(
SELECT Count(*)
FROM qryInvoices_Items1 AS q2
WHERE q2.product <= q1.product
) AS row_number,
q1.product,
q1.SumOfamount
FROM qryInvoices_Items1 AS q1;
I tested that SQL in Access 2007, and it returns the result I think you're looking for. However, if I'm wrong about that point, please include Invoices_Items sample data in your question (as text, not a screen capture image) and show us the output you want based on that sample data.
Note a correlated subquery requires the db engine run that subquery separately for each row of the parent query's result set. That would be a big performance challenge with a huge data set. However, if your data set is small enough, the perform impact could be tolerable, or maybe not even noticeable.

Categories

Resources