简体   繁体   中英

Convert timestamp (int) to smalldatetime (t-sql)

I have a MySQL -Database with several rows. In that environment, I stored the current time as a Timestamp ( int ).

Now I need to migrate my data from a MySQL to a T-SQL-Database. I'm running SQL-Server 2008 .

I checked several approaches, but couldn't come up with a way which transforms my int into a smalldatetime format.

Is there a build-in function for this? Is this even doable alone in a statement? I really don't want to write a PHP-snippet, which converts the timestamp to the desired format.

Thanks in advance

As per the documentation , smalldatetime uses the following format:

DECLARE @smalldatetime smalldatetime = '1955-12-13 12:43:10';

So, we need to convert the MySQL timestamp into date and format it in the above format to get smalldatetime . This can be done by using FROM_UNIXTIME and DATE_FORMAT functions of MySQL , eg:

DATE_FORMAT(FROM_UNIXTIME(timestamp_column), '%e %b %Y') AS 'smalldatetime';

Here's the SQL Fiddle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM