Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top