Friday, March 30, 2012
How to trim off the time part of a DateTime field
The data is migrate from another databse,
There is a field called birthday, it stores the data with the formate like:
1994/11/03 AM 02:09:00
When I bind the field to datagrid, how can I suppress the time part , or
how can I trim the time part form database?Hi,
You can try as
select convert(varchar(10), getdate(), 102)
thanks and regards
Chandra
"ad" wrote:
> I have a tabls of SQL 2000, about the data of student.
> The data is migrate from another databse,
> There is a field called birthday, it stores the data with the formate like
:
> 1994/11/03 AM 02:09:00
> When I bind the field to datagrid, how can I suppress the time part , or
> how can I trim the time part form database?
>
>|||You can also check for available types on
2f3o.asp" target="_blank">http://msdn.microsoft.com/library/d...br />
2f3o.asp
thanks and regards
Chandra
"Chandra" wrote:
[vbcol=seagreen]
> Hi,
> You can try as
> select convert(varchar(10), getdate(), 102)
> thanks and regards
> Chandra
>
> "ad" wrote:
>|||I have study the article you mentioned, but I still cann't figure out how to
do.
If the field name is Birthday, could you give me a exmaple?
"Chandra" <Chandra@.discussions.microsoft.com> glsD
:BD3DB546-9F3D-491B-8596-6F2E0ABCA705@.microsoft.com...
> You can also check for available types on
>
http://msdn.microsoft.com/library/d..._ca-co_2f3o.asp[v
bcol=seagreen]
> thanks and regards
> Chandra
> "Chandra" wrote:
>
like:[vbcol=seagreen]
or[vbcol=seagreen]|||E.g.
SELECT convert(varchar(10), birthday, 102) FROM YourTableName
Regards
Steen
ad wrote:[vbcol=seagreen]
> I have study the article you mentioned, but I still cann't figure out
> how to do.
> If the field name is Birthday, could you give me a exmaple?
>
> "Chandra" <Chandra@.discussions.microsoft.com> glsD
> o_2f3o.asp" target="_blank">http://msdn.microsoft.com/library/d... />
o_2f3o.asp
How to trim off the time part of a DateTime field
The data is migrate from another databse,
There is a field called birthday, it stores the data with the formate like:
1994/11/03 AM 02:09:00
When I bind the field to datagrid, how can I suppress the time part , or
how can I trim the time part form database?
Hi,
You can try as
select convert(varchar(10), getdate(), 102)
thanks and regards
Chandra
"ad" wrote:
> I have a tabls of SQL 2000, about the data of student.
> The data is migrate from another databse,
> There is a field called birthday, it stores the data with the formate like:
> 1994/11/03 AM 02:09:00
> When I bind the field to datagrid, how can I suppress the time part , or
> how can I trim the time part form database?
>
>
|||You can also check for available types on
http://msdn.microsoft.com/library/de...ca-co_2f3o.asp
thanks and regards
Chandra
"Chandra" wrote:
[vbcol=seagreen]
> Hi,
> You can try as
> select convert(varchar(10), getdate(), 102)
> thanks and regards
> Chandra
>
> "ad" wrote:
|||I have study the article you mentioned, but I still cann't figure out how to
do.
If the field name is Birthday, could you give me a exmaple?
"Chandra" <Chandra@.discussions.microsoft.com> glsD
:BD3DB546-9F3D-491B-8596-6F2E0ABCA705@.microsoft.com...
> You can also check for available types on
>
http://msdn.microsoft.com/library/de...ca-co_2f3o.asp[vbcol=seagreen]
> thanks and regards
> Chandra
> "Chandra" wrote:
like:[vbcol=seagreen]
or[vbcol=seagreen]
|||E.g.
SELECT convert(varchar(10), birthday, 102) FROM YourTableName
Regards
Steen
ad wrote:[vbcol=seagreen]
> I have study the article you mentioned, but I still cann't figure out
> how to do.
> If the field name is Birthday, could you give me a exmaple?
>
> "Chandra" <Chandra@.discussions.microsoft.com> glsD
> http://msdn.microsoft.com/library/de...ca-co_2f3o.asp
How to trim off the time part of a DateTime field
The data is migrate from another databse,
There is a field called birthday, it stores the data with the formate like:
1994/11/03 AM 02:09:00
When I bind the field to datagrid, how can I suppress the time part , or
how can I trim the time part form database?Hi,
You can try as
select convert(varchar(10), getdate(), 102)
thanks and regards
Chandra
"ad" wrote:
> I have a tabls of SQL 2000, about the data of student.
> The data is migrate from another databse,
> There is a field called birthday, it stores the data with the formate like:
> 1994/11/03 AM 02:09:00
> When I bind the field to datagrid, how can I suppress the time part , or
> how can I trim the time part form database?
>
>|||You can also check for available types o
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_ca-co_2f3o.asp
thanks and regards
Chandra
"Chandra" wrote:
> Hi,
> You can try as
> select convert(varchar(10), getdate(), 102)
> thanks and regards
> Chandra
>
> "ad" wrote:
> >
> > I have a tabls of SQL 2000, about the data of student.
> > The data is migrate from another databse,
> > There is a field called birthday, it stores the data with the formate like:
> > 1994/11/03 AM 02:09:00
> >
> > When I bind the field to datagrid, how can I suppress the time part , or
> > how can I trim the time part form database?
> >
> >
> >|||I have study the article you mentioned, but I still cann't figure out how to
do.
If the field name is Birthday, could you give me a exmaple?
"Chandra" <Chandra@.discussions.microsoft.com> ¼¶¼g©ó¶l¥ó·s»D
:BD3DB546-9F3D-491B-8596-6F2E0ABCA705@.microsoft.com...
> You can also check for available types on
>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_ca-co_2f3o.asp
> thanks and regards
> Chandra
> "Chandra" wrote:
> > Hi,
> > You can try as
> > select convert(varchar(10), getdate(), 102)
> >
> > thanks and regards
> > Chandra
> >
> >
> > "ad" wrote:
> >
> > >
> > > I have a tabls of SQL 2000, about the data of student.
> > > The data is migrate from another databse,
> > > There is a field called birthday, it stores the data with the formate
like:
> > > 1994/11/03 AM 02:09:00
> > >
> > > When I bind the field to datagrid, how can I suppress the time part ,
or
> > > how can I trim the time part form database?
> > >
> > >
> > >|||E.g.
SELECT convert(varchar(10), birthday, 102) FROM YourTableName
Regards
Steen
ad wrote:
> I have study the article you mentioned, but I still cann't figure out
> how to do.
> If the field name is Birthday, could you give me a exmaple?
>
> "Chandra" <Chandra@.discussions.microsoft.com> ¼¶¼g©ó¶l¥ó·s»D
>> BD3DB546-9F3D-491B-8596-6F2E0ABCA705@.microsoft.com...
>> You can also check for available types on
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_ca-co_2f3o.asp
>> thanks and regards
>> Chandra
>> "Chandra" wrote:
>> Hi,
>> You can try as
>> select convert(varchar(10), getdate(), 102)
>> thanks and regards
>> Chandra
>>
>> "ad" wrote:
>>
>> I have a tabls of SQL 2000, about the data of student.
>> The data is migrate from another databse,
>> There is a field called birthday, it stores the data with the
>> formate like: 1994/11/03 AM 02:09:00
>> When I bind the field to datagrid, how can I suppress the time
>> part , or how can I trim the time part form database?
Wednesday, March 7, 2012
How to store OR get Time value in a column
MasterG wrote:
How can i store the time value in a column. DateTime stores dd/mm/yyy hh:mm:ss , i want only hh:mm:ss , and i also may compare the the values. And also how can i get current time to a column value, something like getdate() ?
Hi MasterG,
You can use the following T-SQL to get hh:mm:ss from a datetime value;
CONVERT(CHAR(8), GETDATE(), 108)
To insert the time into a column, you could use;
INSERT
MyTable (TheTime)
VALUES
CONVERT(CHAR(8), GETDATE(), 108)
You may want to check out Books Online for further information, I also found this article which should be really interesting to you; http://sqljunkies.com/Article/6676BEAE-1967-402D-9578-9A1C7FD826E5.scuk
|||
Thanks for great solution. This one is very helpfull
Happy Coding...
Friday, February 24, 2012
How to store duration in database?
Hi all,
I want to find working duration between two datetimes in c#.
i'm using following code...
DateTime starttime = Convert.ToDateTime(Session["StartTime"]);
DateTime endtime = DateTime.Now;
TimeSpan duration = endtime - starttime;
DateTime period = new DateTime(duration.Ticks);
i want to store this duration in database through stored procedure, i've give datetime datatype to duration but it is giving error in conversion of TimeSpan to DateTime..
Please help... Thanks
You don't need to store this information in your database. You can retrieve this value by using DateDiff between two dates in SELECT statement, for example in seconds.
Datediff(second,startdate, enddate) as duration
Here is a link to how to get the duration and format it afterwards.
http://forums.asp.net/t/1185449.aspx
How to store actual millisecond in datetime?
Hello. I've seached the this forum and others for this question of mine but couldn't get any thing. Everyone know that in MSSQL, the precision of datetime is currently limited to 1/300th of a second. My question is, is there any way to store the actual millisecond in the a table? Also, later, able to pull it up from the table? What are the options?
Please advise.
Thanks,
Rick..
Running the following script worked for me in storing milliseconds. You can review BOL under "Date and Time Formats" for a bit more clarity.
create table test1..t2(c1 datetime, c2 dec(17,12));
insert t2(c1)
select convert(DATETIME, '12/21/07 03:04:05.300');
insert t2(c1)
select getdate();
select c1, convert(dec(17,12), c1) from t2;
update t2
set c2 = convert(dec(17,12), c1);
Hi Zach. Thank for replying. Actually, the problem is still there. If you replace the date you are storing to '10/25/2006 23:59:59.991' and run the script. You will get '10/25/2006 23:59:59.990'. I need to be able to store the exact millisecond being written. This rounding being done to the millisecond is causing use a lot of headache.
Any other idea?
Thanks again.
Rick..
|||Your problem is the Windows clock, which is what you are reading, is only updated every 55 ms. You will never get the EXACT ms something ran. The time is not that acurate.Whatever you are trying to do, you will need to find another method to determine the order or timing.|||
Hi. I am not sure if your statement was true. I don't think it has anything to do with windows clock. Reason I am saying that was, the date time value I am saving into the table is hardcoded. Everyone know its how Microsoft implemented it. But I need to save the actual value without MSSQL truncating the value before storing it in the database.
Thanks for your comment though.
Rick..
|||I think that it is more of an issue of how Sybase originally defined datetime in SQL Server -and Microsoft has never corrected the 'mistake'.|||The problem is that you never see the clock value but only the value converted to the binary format of a SQL datetime (which has the level of precision inherent in its structure).
If you really want to get at the unaltered clock value then I think that you need to use code to access it directly. If you are using SQL Server 2005 then you could write CLR code to access the System.DateTime structure. You could probably even create a new data type. Of course you need some way to hold and retrieve the information you want (possibly the ISO 8601 string format yyyy-mm-ddThh:mm:ss.mmm). I don't know the details of attempting this but those in the ".NET Framework inside SQL Server" forum could probably help.
If you are using an earlier version of SQL Server then you probably need to look at extended stored procedures.
|||Open a query window and repeat this statement 500 times:PRINT CONVERT(varchar(30),getdate(),109)
Run this and see what you get.
You will get the exact same ms until it instantly changes to another set.
It is also a problem, someone mentioned, the significant digits of the time does not allow you to get proper ms.
You still need to find another way to do what you are trying to do.|||
Unfortunately, SQL Server is doing rounding with the datetime data type. Therefore, you won't be able to use this data type. Even if you store the value in a decimal(p,s) data type, you probably won't be able to cast it into a datetime data type without incurring problems.
Oracle implemented true precision, user configurable up to nine (9) decimal places, when it released its TIMESTAMP data type in 9i.
|||Oddly enough try running the following entries:
select convert(DATETIME, '12/21/07 03:04:05.302')
select convert(DATETIME, '12/21/07 03:04:05.303')
select convert(DATETIME, '12/21/07 03:04:05.304')
select convert(DATETIME, '12/21/07 03:04:05.305')
You will find the following results:
2007-12-21 03:04:05.303
2007-12-21 03:04:05.303
2007-12-21 03:04:05.303
2007-12-21 03:04:05.307
So the next question is, what is going on? This is some mighty weird rounding problems. What we believe to be happening is milliseconds are translated into ticks at 3 ms increments.
Moral of the story, if you really need to save that low then you need to create an int column separate of the datetime column and save the milliseconds there.
|||That is not strange at all. That is caused by the precision of the time in the datetime field.See this very good explanation:
http://www.sql-server-performance.com/bs_date_time.asp
You are NEVER going to get 1 ms resolution in a datetime field.|||
This MSDN article specifically states an accuracy of 3.33 milliseconds. I only meant it was strange in that it happens because of converting from varchar like '3/28/2007 11:30:00:001' to a datetime value.
http://msdn2.microsoft.com/en-us/library/ms187819.aspx
How to store actual millisecond in datetime?
Hello. I've seached the this forum and others for this question of mine but couldn't get any thing. Everyone know that in MSSQL, the precision of datetime is currently limited to 1/300th of a second. My question is, is there any way to store the actual millisecond in the a table? Also, later, able to pull it up from the table? What are the options?
Please advise.
Thanks,
Rick..
Running the following script worked for me in storing milliseconds. You can review BOL under "Date and Time Formats" for a bit more clarity.
create table test1..t2(c1 datetime, c2 dec(17,12));
insert t2(c1)
select convert(DATETIME, '12/21/07 03:04:05.300');
insert t2(c1)
select getdate();
select c1, convert(dec(17,12), c1) from t2;
update t2
set c2 = convert(dec(17,12), c1);
Hi Zach. Thank for replying. Actually, the problem is still there. If you replace the date you are storing to '10/25/2006 23:59:59.991' and run the script. You will get '10/25/2006 23:59:59.990'. I need to be able to store the exact millisecond being written. This rounding being done to the millisecond is causing use a lot of headache.
Any other idea?
Thanks again.
Rick..
|||Your problem is the Windows clock, which is what you are reading, is only updated every 55 ms. You will never get the EXACT ms something ran. The time is not that acurate.Whatever you are trying to do, you will need to find another method to determine the order or timing.
|||
Hi. I am not sure if your statement was true. I don't think it has anything to do with windows clock. Reason I am saying that was, the date time value I am saving into the table is hardcoded. Everyone know its how Microsoft implemented it. But I need to save the actual value without MSSQL truncating the value before storing it in the database.
Thanks for your comment though.
Rick..
|||I think that it is more of an issue of how Sybase originally defined datetime in SQL Server -and Microsoft has never corrected the 'mistake'.|||The problem is that you never see the clock value but only the value converted to the binary format of a SQL datetime (which has the level of precision inherent in its structure).
If you really want to get at the unaltered clock value then I think that you need to use code to access it directly. If you are using SQL Server 2005 then you could write CLR code to access the System.DateTime structure. You could probably even create a new data type. Of course you need some way to hold and retrieve the information you want (possibly the ISO 8601 string format yyyy-mm-ddThh:mm:ss.mmm). I don't know the details of attempting this but those in the ".NET Framework inside SQL Server" forum could probably help.
If you are using an earlier version of SQL Server then you probably need to look at extended stored procedures.
|||Open a query window and repeat this statement 500 times:PRINT CONVERT(varchar(30),getdate(),109)
Run this and see what you get.
You will get the exact same ms until it instantly changes to another set.
It is also a problem, someone mentioned, the significant digits of the time does not allow you to get proper ms.
You still need to find another way to do what you are trying to do.
|||
Unfortunately, SQL Server is doing rounding with the datetime data type. Therefore, you won't be able to use this data type. Even if you store the value in a decimal(p,s) data type, you probably won't be able to cast it into a datetime data type without incurring problems.
Oracle implemented true precision, user configurable up to nine (9) decimal places, when it released its TIMESTAMP data type in 9i.
|||Oddly enough try running the following entries:
select convert(DATETIME, '12/21/07 03:04:05.302')
select convert(DATETIME, '12/21/07 03:04:05.303')
select convert(DATETIME, '12/21/07 03:04:05.304')
select convert(DATETIME, '12/21/07 03:04:05.305')
You will find the following results:
2007-12-21 03:04:05.303
2007-12-21 03:04:05.303
2007-12-21 03:04:05.303
2007-12-21 03:04:05.307
So the next question is, what is going on? This is some mighty weird rounding problems. What we believe to be happening is milliseconds are translated into ticks at 3 ms increments.
Moral of the story, if you really need to save that low then you need to create an int column separate of the datetime column and save the milliseconds there.
|||That is not strange at all. That is caused by the precision of the time in the datetime field.See this very good explanation:
http://www.sql-server-performance.com/bs_date_time.asp
You are NEVER going to get 1 ms resolution in a datetime field.
|||
This MSDN article specifically states an accuracy of 3.33 milliseconds. I only meant it was strange in that it happens because of converting from varchar like '3/28/2007 11:30:00:001' to a datetime value.
http://msdn2.microsoft.com/en-us/library/ms187819.aspx