Using all the dates in a given table - c#

I've been currently working on a stored procedure that grabs all the clients I have in the database that have loans. How I'm filtering the results is by the start and end dates given when generating the data. The way my data works is that the returned table will have multiple date fields, 9 date fields to be precise. Each of these date fields are used depending on the LoanStatus field that is also a part of this table. LoanStatus can have values such as Settled, Approved, Cancelled.. What I want to do is to filter the results using this start and end date but applying it to all of the date fields
In my head I've got a couple of pseudo ways to possibly work this issue out:
First Possible Solution
Need some code that'll allows me to essentially do this:
allDatefields > #startDate AND allDatefields < #endDate.
Second Possible Solution
Is to only take into consideration the date field that matches with the loan status. For example, if the LoanStatus is Settled, then look at the DateSettled column of the table, or if the LoanStatus is Approved, then look at the ApprovalDate column of the database.
Personally, I would love to be able to filter through and apply the #startDate and #endDate to all date fields rather than have to look at each date seperately.
Is there a way in SQL Server to apply a WHERE clause to all date fields in a table?

To my knowledge, the where clause does not support your first scenario. I think you might have to settle for something like this:
(LoanStatus = 'Approved' and ApprovalDate > #startDate AND ApprovalDate < #endDate) OR (LoanStatus = 'Settled' and SettleDate > #startDate AND SettleDate < #endDate) OR ... same for the rest of loan status values

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.

Select Only the Date part and display

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

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.

How do I automatically get date when I create a new row in my SQL table?

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.

Categories

Resources