Finding Difference in dates in SQL Query - asp.net

I have a varchar column in sql database which stores in the format (dd//mm/yyyy). I want to find difference between the System Date(ie. when a website is hit)and the column where i have stored date in (dd/mm/yyyy) and display the results of 2 columns of a table in a grid view. Can you please help in this.?

You can use DATEDIFF() function.
Syntax
DATEDIFF(datepart,startdate,enddate)
In your query
SELECT DATEDIFF(day,CAST(dateColumn as datetime),CAST(TodaysdateColumn as datetime)) as dateDiffColumn
Edit
SELECT DATEDIFF(day,CONVERT(datetime,dateColumn,103),CONVERT(datetime,TodaysDateColumn,103)) as dateDiffColumn
This gives difference in dates in terms of number of days.
Then you can bind calculated column to Gridview column.
<asp:BoundField Text='<%#Eval("dateDiffColumn")%>' runat="server">

Parse the date string into a DateTime variable using DateTime.ParseExact. You can then compare the two dates as DateTimes and get the difference.

Related

OUTPUT <NULL> WHEN SELECTING DATE FROM DATETIME - SQLITE

I'm new to the SQL world and im going crazy trying to figure out how to SELECT date from a datetime field in SQLITE.
Example: value <11/11/2005 14:56>, i just want to select <11/11/2005> for EVERY ROW.
I tried strftime(), date(), CAST() and other functions but the output its always NULL.
For example i tried querying SELECT strftime('%d/%m/%Y' , columnname) AS date FROM tablename;
OUTPUT: "NULL" in every row
Can someone help me understand what im doing wrong and how can i fix it? Thank you!!!
It always returns NULL because MM/DD/YYYY is not a valid sqlite date format. Treat the column as a string and use substr and instr to drop off the time portion. Something like (no guarantees, check the doc!)
SELECT substr(columname,0,instr(columnname,' '))
Re comment "how to order by the date in descending order"
This problem is a good argument (the best argument?) for storing the date in a sqlite date/time format. There is a strategy in this post for converting MM/DD/YYYY to YYYY-MM-DD (which sorts dates correctly).
If it's not too late, it would be advisable to change the date storage to a valid sqlite date format. strftime can be used to present the date as desired, and sorting will be accurate.

Convert integer column to date column using sqlite query

I am using SQLite.
In my database, I have a integer column value like 20050819. I would like to convert this as date column as like 2005-08-19.
I can achieve my requirement when using the function CONVERT(columnName, DATETIME) in MySQL server. But I need the same result in SQLite.
Is this possible in SQLite query?
You basically have to break apart the date:
select printf('%4d-%02.2d-%02.2d',
columnname/10000, columnname%10000/100, columnname%100)
from tablename;
P.S.
If you have the choice, you will be far better off storing the date in ISO standard format ('YYYY-MM-DD') in the database in the first place.

Error with dates when filling a datatable

I am trying to fill a datatable in vb using SQLiteDataAdapter from a SQLITE database. There are 3 fields in the table that contain dates and they appear as either recent dates or "1899-12-30" . The Fill command generates a "String was not recognized as a valid DateTime." error. I cannot find a date entry that does not look valid, except "1899-12-30", is this a valid date?
Any other thoughts would be appreciated.
Brad
The short answer is 'yes'. I built a table with the three possible ways of storing dates, thus:
CREATE TABLE `JustDate` ( `Date_1` INTEGER, `Date_2` TEXT, `Date_3` REAL )
Then I populated each of those fields in one record with the value you suspect.
And then I did a trial query against the fields to see if sqlite would treat them as proper dates.

SQLite: select all rows made in a specific month

i want to get all entries from a SQLite table, which have the timestamp from the same month.
For example, the user can type in "July" and then i want to get all entries made in the 7. month.
The current "time"-column is a simple string and in the Format (DD.MM.YYYY HH:MM:SS)
Is there a way to do this with SQLite or will i need to use code in my program?
Assuming that your time strings have a fixed length, you could use a query like this:
SELECT * FROM MyTable WHERE time LIKE '__.07%';
However, you should always stored dates in one of the supported date/time formats so that you are able to use the built-int date/time functions.

Combobox column in Devexpress ASPxGridView shows Datetime instead of Date

I made that one column id GridView is lookup from stored procedure. Stored procedure is joining two tables and date in queston is date, not date time, but in GridView it's shown as Date + 0:00:00.
Any idea how to fix this?
(Ordinary columns dates are fine, but this lookup column is wrong).
Try with DisplayFormatString: documentation and demo. And for general date and time format strings documentation look here.

Resources