How to show timestamp's value as datetime - sql

please tell me how to show the value of timestamp field of a table in datetime format (human understandable). and also how to use it in comparison while querying it.
Thanks

This can't be done. The TIMESTAMP datatype is misnamed - the name has been deprecated in favour of ROWVERSION in SQL 2008, which is a much clearer name.
TIMESTAMP stores an incrementing value which changes every time a row is updated.
If you want last updated dates stored against each row, you will need to add a DATETIME or SMALLDATETIME to your table and update it with the current date on each update.

You mean timestamp data type I think - http://msdn.microsoft.com/en-us/library/ms182776%28v=SQL.90%29.aspx
You can't convert it to datetime. According to the documentation in the link above :
> The timestamp data type is just an incrementing number and does not preserve a date or a time. To record a date or time, use a datetime data type.

Related

Converting all data in a Varchar column to a date format

I'm working on a table with a column, 'Expiry Date', as a varchar with all data formatted as DD/MM/YYYY.
The creator of the table has used the wrong type for this expiry date column and now the client needs to filter and show all records before and after the current date as the time. This means the type needs to be changed to date or datetime type to be able to use the CURDATE() function.
However, the current format of the values does not satisfy and wont allow the type to change unless the format is changed to YYYY-MM-DD (or similar).
Is there any way to mass format the values in this column and this column alone as there are thousands of entries and formatting one by one would be extremely time consuming.
Let me assume that you are using MySQL.
Perhaps the simplest method is to add a generated column that is a date:
alter table t add column expiry_date_date as
(str_to_date(expiry_date, '%d/%m/%Y'));
You can also fix the data:
update t
set expiry_date = str_to_date(expiry_date, '%d/%m/%Y');
This will implicitly convert the result of str_to_date() to a date, which will be in the YYYY-MM-DD format.
More importantly, you can then do:
alter table t modify column expiry_date date;
Here is a db<>fiddle.
You can do similar operations in other databases, but the exact code is a bit different.
What you need is an update on that column, but before doing it I suggest you to check if the result is what you want.
select replace(expiry_date, '/', '-') new_expiry_date
from table_name
If this returns the results you want you can run the following update:
update table_name
set expiry_date = replace(expiry_date, '/', '-')
Of course you will need to replace expiry_date and table_name with the names of your column and table.

What is difference between datetime and timestamp

What is difference between datetime and timestamp datatype in Sql Server?.
One is a date and time, the other is a column type that is updated every time a row is updated.
[Note timestamp is being deprecated; use rowversion instead]
Timestamp (deprecated synonym for rowversion) :
Is a data type that exposes automatically generated, unique binary numbers within a database. rowversion is generally used as a mechanism for version-stamping table rows. The storage size is 8 bytes. The rowversion data type is just an incrementing number and does not preserve a date or a time. To record a date or time, use a datetime2 data type.
http://msdn.microsoft.com/en-us/library/ms182776.aspx
Normally time-stamp used when ever you inserted new record into database automatically system would take default date time
ex : transaction like bank deposit or with draw
data-time datatype used at the movement of inserting the user defined date into the record
ex : date of birth
timestamp is the synonym for the rowversion data type and is subject to the behavior of data type synonyms. In DDL statements, use rowversion instead of timestamp wherever possible. In simple way to tell, It means the updating time of row. datetime means the time of creation of row.
DateTime is constant and other is changeable as the real time and local time.

Whiel Update ,datetime value is rounded to seconds.! i want milliseconds too

While updating a datatime column in a table from another table, i noticed that mnilliseconds value are not shown.. instead it is rounded and the value is updated to nearest seconds.
Example :
Original Value: 2008-06-26 14:06:36.643
Updated Value : 2008-06-26 14:07:00
Please help me getting the actual value including milliseconds
In the case where you're doing a straight update of a datetime in one table with one from another table (i.e. no fiddling with the value), then it sounds like the datatype in the table being updated is not the same.
i.e. in SQL Server world, it could be that you are using SMALLDATETIME column in the table being updated, but a DATETIME field in the table being copied from. SMALLDATETIME is only accurate to the second and so would show this behaviour
In SQL Server;
SELECT CAST('2008-06-26 14:06:36.643' AS SMALLDATETIME)
> 2008-06-26 14:07:00
So the destination table column is probably SMALLDATETIME (or your casting in the query).

inserting only date in a column in sql server

Is there a way to insert only date in a datetime column in sql server without the time?
for example
date (datetime)
===============
12-01-2000
16-02-2000
or i should store this as a varchar and cast it when retriving so that i can convert to whatever form i need.
my solution is to store it as varchar and convert it to datetime whenever needed
SELECT CONVERT(VARCHAR(10),GETDATE(),111) -- get datepart only
or
also check this post about creating date type :
create user defined data types:
create type Date from dateTime
http://weblogs.sqlteam.com/jeffs/archive/2007/10/31/sql-server-2005-date-time-only-data-types.aspx
If you are using SQLServer 2008 you can use the date data type.
The following SQL will strip out any time values and set them all to zero. So you won't need to worry whether a time value is there or not.
Select Cast(Floor(Cast(MyDateColumn as float)) as DateTime) as MyDateColumn
From dbo.MyTable
Just use smalldatetime or date. Convert your dates to your format before you update your date values or after you select date values in your app.
You can change format of date format in sql queries or in your app.
Here is a list on date formats in sql
http://www.sql-server-helper.com/tips/date-formats.aspx
Here's a link on date data types
http://databases.about.com/od/sqlserver/a/date_time.htm
Good Luck!

extract date from datetime stamp

Anyone know how to extract the date from a datetime stamp as part of the where clause?
eg.
select *
from tableA
where date between '01/08/2009' and '31/08/2009'
(Date is a timestamp!)
Many thanks,
Fiona
If this is sql server, it's not possible. The timestamp data type's name is misleading, as it does not store any date information of any kind. All it holds is a sequential value that allows you to establish record order (eg, item A was created before item B), and therefore you don't have enough information in that column alone to know on what day the row was created.
Since the link I provided is Sql Server 2000 specific, also check this link for information on SQL Server 2008:
http://msdn.microsoft.com/en-us/library/ms182776.aspx
timestamp is the synonym for the rowversion data type and is subject to the behavior of data type synonyms. In DDL statements, use rowversion instead of timestamp wherever possible.
To build a real timestamp column in Sql Server, use a DateTime (or DateTime2) data type and set it's default value to getdate() or current_timestamp.
If a real datetime value, not TIMESTAMP/ROWVERSION which is binary(8)...
SELECT DATEADD(DAY, DATEDIFF(DAY, 0, #MyValue), 0)