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

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, e.g.:
DATE_FORMAT(FROM_UNIXTIME(timestamp_column), '%e %b %Y') AS 'smalldatetime';
Here's the SQL Fiddle.

Related

How to get Time from Mysql Date Format DD/MM/YYYY?

I want to get time from mysql dd/mm/YYYY H:M:S format.
I have tried,
SUBSTRING_INDEX(field, 'delimiter', index)
but am looking for a better solution.
have tried, DATE_FORMAT(field, "%H:%i:%s") but it returns NULL because my date format was not native (YYYY-mm-dd)
it was 02/05/2019 19:38:27
How to get time from this above format in a better way?
NOTE: I am storing date like above.. this fetching form SQL Server
I guess you can first use STR_TO_DATE followed by CAST(... AS time). Casting instead of formatting allows you to use the result in date/time calculations.
SELECT CAST(STR_TO_DATE('02/05/2019 19:38:27', "%d/%m/%Y %H:%i:%s") AS TIME)
Ideally you should teach SQL Server to export dates in yyyy-MM-dd hh:mm:ss format.
This is how i Resolved,
TIME(STR_TO_DATE(d.in_punch, "%d/%m/%Y %H:%i:%s"))
also as per #Salman A
CAST(STR_TO_DATE('02/05/2019 19:38:27', "%d/%m/%Y %H:%i:%s") AS TIME)
this also worked.

Trouble Converting varchar to date

I'm using MAMP's phpMyAdmin as my database.
I'm having troubles using the CONVERT sql function. Is this a common problem?
SELECT CONVERT(varchar(10), Date, 103)
This will give me the following syntax error. Which is one of the usual errors given but I've been trying a lot of different ways.
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'varchar(10), Date, 103)' at line 1
If the above can't be solved can I bypass this issue by adding a new column with a specific date format style?
You are trying to use sql-server syntax in mysql database.
mysql does not have 3rd parameter for CONVERT function.
CONVERT(expr,type), CONVERT(expr USING transcoding_name)
The CONVERT() and CAST() functions take an expression of any type and
produce a result value of a specified type.
http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html#function_convert
to do the get same format in mysql you need to use DATE_FORMAT() function
this should give you the same resultDATE_FORMAT(date,'%d/%m/%Y')
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
select DATE_FORMAT(Current_Date(),'%d/%m/%Y')
here is SQL Fiddle
http://sqlfiddle.com/#!2/d41d8/36014
The date is already in the correct format but it is stored as varchar(10) Current looks like: 21/04/2014
Yes, I have a column that has dates but is set to varchar as it wasn't being used now I need to convert these into dates.
If you are trying to get a date form from a string form of date, then
select str_to_date( date_column, '%d/%m/%Y' ) as date from table_name
This will return 21/04/2014 as 2014-04-21, regular default date format in MySQL.
It depends on what you want to do.
If you're trying to convert an integer that represents a Unix timestamp to a date with MySQL, you may want to use FROM_UNIXTIME()
See also: MySQL: Convert INT to DATETIME
If you're trying to convert a string that represents a date into a date, you can use STR_TO_DATE()
After that, if you perform some processing on the date and want to convert it back to a string, you'll need DATE_FORMAT()
See also: mySQL convert varchar to date

how do I convert date type to hh:mm in mysql?

I got in my table two types:
hour_Event in type date
date_Event in type datetime
I would like hour_Event to be formatted to hh:mm
and date_Event to be dd/mm/yyyy
I use PHPMyAdmin and im new with MySQL so I dont know how to change the format and for what.
How do I do it?
**DATE_FORMAT(hour_Event,'%h:%i')** will give you hour_Event in hh:mm
and
**DATE_FORMAT(date_Event,'%d/%m/%Y')** will give you date_Event in dd/mm/yyyy
You could change the format using php before you insert data or after retrieving themfrom your database according to your needs.You could use DateTime / strtotime() & date(). In most cases a single timestamp field in your table is enough for an event.
Have a look here:
Convert one date format into another in PHP
Also you could format your field according to your needs with mysql for example:
DATE_FORMAT(tables.field, '%d-%m-%Y %H:%i:%s') AS 'new_name'
Have a look about it here:
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format

Format date in mysql query that uses cast

I've got this as the select part of my query:
SELECT cast(cast(exp_channel_titles.edit_date as char(14)) as datetime) AS Edit_Date
That takes data from a db in this format 20130501092128 and returns it in this format 2013-05-01 09:21:28
I can only assume it is some kind of magic as i don't fully understand how this works tbh.
But, i need to change the format of the date that it spits out to this format: %d/%m/%Y %k:%i:%s
I can honestly say i have no idea how to do this in that query, i've tried adding it as a param to datetime (is that even a mysql function?!?) but no joy and many other poor attempts that i wont go into.
If anyone can help, i'd be hugely grateful!
MySql automatically converts 20130501092128 to a date and time field, even if it is a VARCHAR or a INT, and you can just use this:
SELECT DATE_FORMAT(exp_channel_titles.edit_date, '%d/%m/%Y %k:%i:%s')
Please see fiddle here.
You can change output format using DATE_FORMAT() function from MySQL. Here is the documentation post about it.
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
You can change the output format into whatever format you want, but if you recieve that data into an application, modifies it and return that data to server (editing a row for example). Remember to reformat it into a valid date for MySQL.
If you dont know how to do it, just have to do this into your query:
SELECT DATE_FORMAT(cast(cast(exp_channel_titles.edit_date as char(14))
as datetime), '%e/%m/%Y %k:%i:%s') AS Edit_Date

Converting date format using str_to_date

In one of my Mysql database table the dates are stored in the format 31-Jan-05.
I'm trying to convert this format to 2005-01-31 before inserting them in other tables.
I've tried in this way str_to_date(exam_date, '%d%M%Y'), but i encounter the following error
Incorrect datetime value: '31-Jan-05' for function str_to_time
Can't i change the date format from 31-Jan-05 to 2005-01-31 using str_to_date?
Thanks in advance.
Yes. But you have two problems.
The second parameter is the current date format. (i.e. of the string)
You need to have the proper format (i.e. %b instead of %M).
Read the docs the for str_to_date().
str_to_date(exam_date, '%d-%b-%y')
Note: If you don't have a zero padded day, then you need to use %e instead of %d.