How to sort data in asp.net according to varchar datetime field in SQL? - sql

I have a table with a column DateTime with datatype VARCHAR(500).
I'm storing date and time info in it. I'm getting the datetime value using C#
DateTime.Now.ToLongDateString() + "," + DateTime.Now.ToLongTimeString()
This gives me the format like this: 14 July 2015,12:56:04.
I want to sort data in gridview by this column, but since it is of VARCHAR this gives incorrect result. I have understood that there is something about conversion, but haven't been able to do that.
Any suggestion regarding dis will be appreciated.

If you are using boundfield for the datetime column in the Gridview then this will help you in the design of your Gridview:
<asp:boundfield datafield="DateColumn" dataformatstring="{0:MMMM d, yyyy}" htmlencode="false" />

Related

Telerik RadGrid Date column is not formatted to the right format

I have the following column in the Telerik Grid:
<Columns>
<telerik:GridDateTimeColumn DataField="Boardingdate" DataType="System.DateTime" DataFormatString="{0:MM/dd/yyyy}" SortExpression="Boardingdate" UniqueName="Boardingdate"
HeaderStyle-Width="10%"/>
</Columns>
In my sql logic I get this column' date the following way:
cast(convert(varchar(30),meredsopen,101) as date)
And in the sql it's displayed as follows: 2010-10-04
However, when the Grid renders on the page the date's columns have the values like that:
8/23/2018 12:00:00 AM and I cannot sort it as well.
I can convert the date value to string to show 8/23/2018', but in that case, I think, the sorting will be performed on the string and not on the date` value
How can I fix that?

SSRS Report Formatting Date time

How do I write the vb expression for "mm/yy" of a datetime?
I've tried this with no success
=Fields!myValue.Value & Format(Fields!myDateTime.Value,"MM/yy")
It's not clear what your datatypes are. Assuming myValue is numeric abd myDateTime is a DateTime type then you will probably need to convert the numeric value to a string.
Something like.
=CStr(Fields!myValue.Value) & Format(Fields!myDateTime.Value,"MM/yy")
If this does not help, post some sample data, including the datatypes and expected result

convert TEXT dd/mm/yyyy in SQL column to DATE YYYY-MM-DD

I would love to know the best way to handle data that has been inputted incorrectly as dd/mm/yyyy into a sql database as TEXT and to have it converted into a new column of the table with the datatype as DATE so it is actually stored as yyyy-mm-dd.
Existing text date column name is called "olddate" with an empty column created called "truedate" to house the new data. Each row has the date field, but none are able to be sorted correctly because of this issue.
Any ideas how I can slice and dice the current date into a new DATE field friendly version?
Thanks in advance :-)
That is style 103. So use:
select convert(date, col, 103)
Are you using Oracle? If so, TO_DATE is what you want. You can take in a string that represents a date and convert it to a date using the format you pass it.

How do I convert a string of format dd/mm/yy into datetime in SQL Server 2008?

I have table named PTBV with two columns: Dateptbv varchar(50) and [Column 1] varchar(50).
I'm trying to use the CONVERT() function to get actual datetime values from the string data stored in the Dateptbv column.
Here are examples of the data in that column:
"10/01/13"
"11/01/13"
"14/01/13"
"15/01/13"
"16/01/13"
"17/01/13"
"18/01/13"
"21/01/13"
"22/01/13"
"24/01/13"
"25/01/13"
"28/01/13"
"29/01/13"
"30/01/13"
"01/02/13"
"04/02/13"
Note that the quotation marks in this sample are part of the data and are stored in the column. Otherwise, data is stored in dd/mm/yy format.
Unfortunately, every date style I've tried has resulted in an error message. How can I convert this data into DateTime values?
If I understand your picture correctly, your column Dateptbv is VARCHAR(50) and stores the date as yy/mm/dd wrapped in " characters. First of all: Change this, if ever possible!. Try to store values in appropriately typed columns!
Try
SELECT CONVERT(DATE,SUBSTRING(Dateptbv,2,8)),3) --3 for dd/mm/yy
Read this link to find more details about the abilities of CONVERT.
Try the below script
SELECT CAST(SUBSTRING(Dateptbv,2,8) AS DATE)
FROM PTBV

remove time from sql

Please can any ove suggest me how i can remove time part from sql dates. I have already read 100s of articles and most of them remove the time but leaves 00:00:00 behine which i don't want also i know that i can remove these ZERO's by doing a convert to varchar but unfortunately i cannot change the type of the date column it has to be date type instead of string
Please advise
Thanks
A datetime column in the database will always contain both a date portion and a time portion. SQL Server 2008 introduces types that store only date or only time. But, so long as the column is of type datetime, you'll have to accept that the time portion exists, and you can just ignore it in your application (by applying suitable formatting).
You can add a check constraint to the table to ensure that all entries in the column always have the same time portion (e.g. 00:00:00).
You need to apply a format string to your gridview column to only display the date part. An example of such a string would be dd MMMM yyyy
Is this an ASP.NET gridview? If so there is example code here.
A SQL server DateTime type is a date and a time in SQL 2005 there is no date only field type, you cannot remove the time component and still have a field that is a DateTime type. Your options are the ones you have outlined: Convert to (n)varchar and remove the time componenet, leave it as a DateTime and accept that it has a time component. Why do you wish to remove the time component, what problem would this solve for you.
Further to your comment below, the database is not where you should be formating you Date strings to display in your GridView. You do display layer things in the display layer, in this case in the gridview. You need to use a format string in your data when data binding to the gridview. Yee examples below.
BoundField:
<columns>
<asp:BoundField headertext="CreationDate" dataformatstring="{0:dd-MM-yyyy}" //rearrange these letters as necessary
datafield="CreationDate" />
</columns>
TemplateField
<itemtemplate>
<asp:Label id="Label1" runat="server" Text='<%# Bind("CreationDate", "{0:M-dd-yyyy}") %>'>
</asp:Label>
</itemtemplate>
Try this
select cast(convert(char(11), getdate(), 113) as datetime)
SELECT convert(varchar, getdate(), 101)
OR
Declare #datetime datetime
set #datetime = getdate()
SELECT convert(varchar, #datetime, 101)
DateTime Formats
EDIT:
SQL server 2008 has this
functionality, if you can upgrade.
Just make sure to save 00:00:00 in
your table and remove time part in
Return datetime and format it in your
grid or in your code.
If you could upgrade to SQL2008, that has a separate time command.
I see you say you want to keep it as a datetime object, so as far as I can see if you can't upgrade to 2008 then you are stuck with the zeros
You can't remove time part from thr datetime datatype field.
Only thing you can do is to cast it to the date or varchar datatype before output or to set time part to 00:00:00 before insert/update.
Example:
the statement:
select cast(getdate() as date)
returns only date
What are you trying to do?
Why do you care if the database is saving the exact time or 00:00:00?
When you query this field(inside or outside the DB) you can ignore the time value and show only the date...
Again, say what you are trying to do... I believe it will be easier to help you.
Update mytable set mydatetime=convert(datetime,convert(char(10),mydatetime,103),103)
This will update your table. Hope that is what your looking for.
103: Format datetime as dd/mm/yyyy.