Convert multiple gridview date fields into one format - asp.net

I'm spinning my wheels right now and what I'm pretty sure is an easy answer, I just can't see at the moment. What I'm attempting to do take two sets of converted dates in a database and I want to present them in a grid view as one single date format.
Right now the two date formats are as follows:
YYYYMMDD
mon dd yyyy hh:miAM/PM
I want to convert them all into one format. For giggles, lets just convert them all to 112 (YYYYMMDD).
Now I want to present this all in the same SELECT statement that populates my gridview with other columns of data of various forms lots of text, data entry, long characters, etc.
The thing I haven't been able to wrap my head around is how to put this into a SELECT statement that includes all the rest of the information that populates the gridview.
So a normal SELECT statement (into all nvarchar(max) fields...the table is formed this way because we deal with lots of non standard data importing like Unix) would look like this (this is just a sample database):
SELECT [record_number], [price], [product], [description], [date_of_order], [customer_id] [comments]from sample_database
When presenting the 'date_of_order' field, I figured I need to use a CAST or CONVERT with the proper conversion numbers. However, after looking online I'm scratching my head on how to do this as most of the somewhat relevant information is focused around converting with the GETDATE option instead of leveraging the date that's already written to the database (the information is entered on a different part of the website).
I'm just looking for a pointer or a suggestion in the right direction to use a SELECT statement in my gridview (along with all of the other fields) that can convert any/all of the entries into one common date format. I know I'm missing something simple here but I don't know what.
I should note that I don't want to change them in the Database just when presenting them in the Gridview.

Use the following Convert SQL Function around all of the date fields you require, be warned that this can cause errors if the value is not a correctly formatted date.
CONVERT(datetime, [OldDate], 112)
Found here.
Also look here for more information on the Convert Function

Related

Access 2010 Query with Parameter and Sort

I have a problem that I've been going round and round with in Access 2010. Imagine a table with these columns:
Name Date Time
Now, I have a query that asks the user to input a begin date and an end date and returns all records that are between those two dates. This works fine. However, as soon as I add a sort to the Date column things go awry. Once you put a sort on a column with a parameter the user gets asked to enter the parameter twice. From what I've been able to find out this is normal (although annoying) behavior in Access.
If I add the Date column in a second time and show the column with the sort and don't show the column with the parameter it works fine. The query would look something like:
Name Date (shown & sorted) Date (not shown & parameters) Time
Now when I run the query it all works well and comes out the way I want it to. This would obviously be a great solution then. However, there's another problem. When I save the query, leave, and reopen the query the two columns are merged back into each other. Thus, the change is lost and the user again sees two inputs.
My question is this: what can I do differently to achieve the desired results?
Some possible things I've thought about but don't know the answer to are:
Is there a way to make it so the columns don't merge? Do I have to use a form with the input boxes and take the data from that (I'd prefer not to do that as it will require a lot of additional work to handle the various things I am doing in the database). Is there some obvious thing I'm missing?
Thanks for any suggestions.
FYI: Here is the SQL from the query
SELECT Intentions.Intention, Intentions.MassDate, Intentions.[Time Requested], Intentions.[Place Requested], Intentions.[Offered By], Intentions.Completed
FROM Intentions
WHERE (((Intentions.MassDate) Between [Enter start date] And [Enter end date]))
ORDER BY Intentions.MassDate, Intentions.[Time Requested];
It is true that sometimes the Query Designer in Access will "reorganize" a query when you save it. However, I don't recall an instance where such a reorganization actually broke anything.
For what it's worth, the following query seems to do what you desire. After saving and re-opening it looks and behaves just the same:
For reference, the SQL behind it is
PARAMETERS startDate DateTime, endDate DateTime;
SELECT NameDateTime.Name, NameDateTime.Date, NameDateTime.Time
FROM NameDateTime
WHERE (((NameDateTime.Date) Between [startDate] And [endDate]))
ORDER BY NameDateTime.Date DESC , NameDateTime.Time DESC;
I have had the same problem and I have discovered the reason:
If, after you have run your query, sort a collumn in the result grid and the say yes to save changes to the query the sort action will be stored with the query. This will actually cause the query to run twice. First to create the result and then one more time to sort. You'll therefore be asked twice for the parameters.
SOLUTION: Run the query (entering your parameters twice ;-) ). Then remove the Sorting by clicking on the AZ-eraser symbol in the task bar above (in the sorting compartment).
Then open your query in design-mode and add the sorting order to the appropriate collumn.
Your are then good to go.
Regards
Jan

Control input of SQLite attribute to date format only.

I have been reading all about converting TEXT fields into date formats and ways to use Python to create date objects but my question remains.
My table has a dateTime column that is specified as TEXT, I would like to build a constraint that forces input to be in dateTime format, but as SQLite doesn't do dates (as I would like) I haven't worked out how to do it.
My current ideas: 1. Limit number of characters
2. Set separate attributes for day, month and year and constrain their domains
3. It is silly to do this on the database side just do it in the user interface
I would appreciate your opinions on these or other options.
Thanks :)
I've been trying to solve the same issue and the method I came up with was to use the date/time functions to parse the value and check that it hasn't changed. Essentially the following:
CREATE TABLE dts_test (
dts TEXT CHECK (dts IS datetime(dts))
);
If the default format is not what you want you can use the strftime or similar to design whatever format you want, but it must be something that the built-in date and time functions can parse.

ASP.NET Passing Inconsistent DateTime Format to SQL Server [duplicate]

This question already has answers here:
RDLC - "String Not Recognized As A Valid DateTime" Error
(2 answers)
Closed 2 years ago.
I know this is an age-old question but I'm not exactly new to it and I haven't found an answer which helps me so far!
I've got an ASP.NET site which displays data from an SQL Server 2005 database. At the top of the page are 'Date From' and 'Date To' textboxes which are used to filter the data shown beneath.
The data is displayed using an SqlDataSource, and the two dates are passed as parameters to a Stored Procedure. The textboxes display the dates and accept input in UK date format (dd/MM/yyyy) and it works fine.
Now I've added a new page with exactly the same setup, displaying slightly different data. In the backend I created a new Stored Procedure by copying and pasting my original one, it's almost identical. And yet on this page I get errors with my dates because they're being read as MM/dd/yyyy, meaning that today's date, for example, 15th August 2011, is passed as 15/08/2011 and isn't a valid date.
I've checked over everything and I can't understand why this should work on one page and not another, especially when I've basically just copied all of the original code and tweaked it slightly. Can anyone suggest anything I can check that I might not have thought of?
The text boxes are strings, which are in the thread local which is set from the browser (langauges).
Convert them to aa DateTime object first, using a local aware transformation, then write the dateTime object to the server. NEVER (!) deal with strings to sql server unless you format them in ISO independent form (2011-08-17 23:52:11).
But in general, ASp.NET will show dates, times, numbers in the local langauge of the browser. Either turn that off, or deal with it. It is nice for the user. So, check locales - user, server process. What is the thread current locale?
You need to use the SQL convert in stored procedure like:
convert(varchar, #yourdateParameter, 103) for format dd/mm/yyyy
or
convert(varchar, #yourdateParameter, 101) for format mm/dd/yyyy
Hope this helps.

Reporting Services Sorting by Date issue with TFS Report

Long time reader etc,
I've a TFS report that I want to alter the sorting on by date. The problem is the sort only seems to consider the day element and the rest isn't considered. For example, the following is happening:
1/7/2011
1/7/2011
1/7/2011
2/12/2010
3/03/2011
3/03/2011
I've looked for a way to specify the datatype on the box in the table but to no avail. Any suggestions?
I've realised the field was being treated as text as the date in question isn't guaranteed to be there and replaced with an empty string if it wasn't.
Two steps have fixed the problem:
Added calculated field to dataset and wrapped an iif around to deal with missing dates as being way off in the future.
=CDate(IIf(IsDate(Fields!My_Date.Value), Fields!My_Date.Value, DateAdd(DateInterval.Year, 10,Now)))
This then forces the field to be treated as a date. I then added a sort on the group on this calculated field which isn't shown in the report and gives the impression that those items with a date get ordered and the rest are left to the other layers of sorting which is correct.
Is there a better way of doing this?
I find it a very efficient way!
I did it myself more complex
Group properties of the field you want to sort on
Sorting
expression:
=Datepart("yyyy",Fields!Datum.Value) & Datepart("m",Fields!Datum.Value) & Datepart("d",Fields!Datum.Value)
It will sort first on year, then on month, then on day

Asp.Net Sql Auto-Increment for Wall Post

I have a table that contains three columns.
"UserId" type-nvarchar
"PostAuthorId" type-nvarchar
"Post" type-text
This table will contain "wall" posts like in facebook for each user's page. I am going to use a gridview on each user's page to display the posts. The issue is I want to display them with the latest(most current) post being first and the earliest post being last.
I have never used autoincrement before and I am not sure if that is the answer. If it is, I do not know how to use it. I thought about adding a date posted column and then ordering by date.
If I end up using the date column, I could also display the date on the post. Is there a way to convert the date to a readable format?
What is the best way of implementing this type of ordering?
If you use AutoIcrement the first record will start with 1 and each record will increment from there. (default setting)
If you want to sort them by newest first do an ORDER BY ID DESC
I would suggest making a column called wallPostID then setting that to AutoIncrement and also your Primary Key
Date Formating:
If you are displaying this data in a gridView
Go to Edit Columns on your grid view
CLick on the Date field under "Selected Fields" on the bottom left
Under "BoundField properties" on the right Go to Data -> DataFormatString
{0:d} will display as 1/1/2010
This site has more info in string formatting
http://msdn.microsoft.com/en-us/library/fht0f5be.aspx
A datetime column would definitely work for something like this. Assuming you are using MS-SQL, you can also attach a default value to the column using a built-in function like GETDATE(). That way, you only have to input the data that matters and the database will take care of adding the datetime column.
For converting a datetime to a readable format try:
DateTime postDate;
string value = postDate.ToShortDateString();
You should always use an ID field that auto increments. Can also be used as your PK
I would suggest the DateTime field rather than the autoincrement simply because it will not only serve as an effective Sort field, it also preserves information that you may well want to display. If you want the most recent first you'll sort using the Date and a "DESC" modifier:
Select ... Order By [Date] DESC;
When you retrieve the data, you can retrieve it as a DateTime and modify it using C#. You can use "ToShortDateString()" as suggested by mdresser if you just wish to show the date or ToString("...") if you wish to show the time as well. You can also use SQL to convert it into a string before retrieving it:
convert(Varchar(10), #mydatetime, 101)
If you look in MSDN you'll see the various conversion codes (101 is the code used above) that can be used to translate the date in various ways.
UPDATE: You may want to use an autoincrementing field for your application for reasons other than your expressed need to sort wall entries. They are easy to use - just mark the field as an Identity if using SQL Server (other DBs are similar). As far as using them in your program, just think of the field as an Int field that you never have to set.
Now, why would you use a auto-incrementing field? Perhaps the most straightforward reason is so that they give you have an easy way to identify each record. For example, if you permit people to alter or delete their wall entries, the auto-incrementing field is ideal as it gives you a way to easily look up each record (each record will be assigned its own, unique value). You might put an "x" next to the record like StackOverflow does and make it a call back with the UID (auto-increment) value. Note that you should set up your primary key on the UID field if you'll be doing this.
Now, if you find them useful for this reason then you could also sort by the UID. I would still store the date so that you can provide Date and Time feedback as to when an entry was made on the wall but this would no longer be your indexed or sorted field.

Resources