Scala, Slick, and Dates Before 1970 - mysql

I'm trying to represent dates in my DB (MySQL) that can be anywhere from the year 1900 until today. So obviously, timestamp-like values won't cut it since that only covers dates from 1970 until now. So in MySQL, the datetime data type can work for this (although for some weird reason, using a datetime feels dirty to me). However, for Typesafe's Slick library, the only date types supported are java.sql.Date, java.sql.Time and java.sql.Timestamp, all of which can't handle dates before 1970.
I'm thinking of craziness such as finding a way to cast the datetime to a string, and having Slick pretend it's a string, and do a conversion to an appropriate type (ex. org.joda.time.LocalDate).
Has anyone encountered this problem before, and if so, how did you deal with it?

Use DATETIME instead of TIMESTAMP.

Somebody wrote a joda mapper for Slick: https://github.com/tototoshi/slick-joda-mapper

Related

how to strip timestamp of +00:00 in sequelize

I am using Sequelize to map objects to my already existing database, in this database they didnt use the auto-generated timestamps, they did their own datetime work, the current way the database stores the date is in YYYY-MM-DD HH:MM:SS.MS however using sequelize, i am returning a date that looks like YYYY-MM-DD HH:MM:SS.MS +00:00 and i believe this is whats tripping me up. as i am receiving an error Conversion failed when converting date and/or time from character string. I tried even accessing the Date() object and building my own date string, and i got the same error. even though it follows the correct formatting.
for my model i have this at the CreateDateTime (This is the name of the field in the database.
CreateDateTime: {
type: DataTypes.DATE,
defaultValue: `${date.getFullYear()}-${date.getMonth()}-${date.getDay()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}.${date.getMilliseconds()}`,
},
UpdateDateTime: {
type: DataTypes.DATE,
},
I have also, renamed the timestamps that sequelize provides to the appropriate names above. I am not sure much more of how to get around this issue besides maybe using the timestamps provided by sequelize and dropping the ones in the existing.. though id rather not do this. even though eventually ill move this database to a better schema in which the timestamps are autogenerated.
is there a different way around this error? No matter what i seem to do the sql insert query always seems to tack on the +00:00 to the end, is this some kind of default value of using the DataType DATE? is there a different data type i should be using?
So I figured out that the previous team likely wasn't storing an actual datetime timestamp, because once i changed the DataType from DATE to STRING it worked just fine, i was no longer receiving this error, but it still strikes me as strange, because using my DB exporer (i use Razor) when i was having the table described to me, it clearly stated that these fields where of datetime and not varchar, so im not sure why this works, only that it follows the appropriate pattern and it does indeed work.

MySQL timestamp format and datediff

Hi I'm writing queries for MySQL, and now my database has a column containing the timestamp in this format: 7/14/2015 7:57:49 AM, but I need to use the DATEDIFF function, so how can I convert the timestamp into the format like: 2015-7-14 (or 2015-07-14, I'm not sure which one is correct; just the date)?
This should convert your string to just the date in a date format, then you can use DATEDIFF on the date fields in question:
SELECT STR_TO_DATE(LEFT(t,LOCATE(' ',t) - 1), '%m/%d/%Y') FROM my_table;
The LEFT function will take the substring to the left of the space, which is just your date, then STR_TO_DATE will convert that substring to a date the system can use.
(Not knowing your field and table names, I used t and my_table.)
You don't need to. The way MySQL displays timestamps has nothing to do with the way they're stored internally; as long as it's TYPE TIMESTAMP or some compatible type, the DATEDIFF() function will know what to do with it.
TIMESTAMPs are actually stored as a really huge integer representing (I think) milliseconds from Midnight UTC, January 1st, 1970. The display format is determined by a system global variable, and has nothing to do with the actual value.
Converting from a string to a DATETIME or TIMESTAMP is actually also fairly straightforward using the STR_TO_DATE() function; in your case the format string would be something like
STR_TO_DATE('%c/%e/%Y %l:%i:%s %p', datecol)
although you might have to experiment a bit to make it work reliably.

Converting MySQL dates (from what I think are seconds)

I am pretty new to MySQL, and am looking at a table (through a query) that has three date fields. However, they appear to be in seconds (but I could be wrong), but ultimately, I need to convert them to a valid date/time.
The numbers are:
1366272682
1366239600
1366272682
I think one of these dates is 18th April 2013.
Can someone let me know how I can convert them within the query (or indeed if I am right).
Thank you.
Those "numbers" are actually Unix Timestamps. Use FROM_UNIXTIME() to convert them into human friendly formats:
Returns a representation of the unix_timestamp argument as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context.
For example:
SELECT FROM_UNIXTIME(1366272682, '%e%D %M %Y')

Converting a non-standard datetime field in SQL server

I'm using SQL Server 2008 R2. I've been given data that includes numerous varchar fields in the format mm/dd/yyyy hh:mm:ss.mmmmmm AM. Here are some examples:
1/16/2013 10:31:38.000000 AM
11/12/2013 3:42:12.000000 PM
12/6/2013 2:42:46.000000 PM
I'd like to convert into a datetime format so I can work with them, but am having problems... I've found various sites that list the different date time formats (such as this one), but this format isn't listed anywhere.
I've tried casting and converting in various different ways, including:
select CAST (field as datetime) from table;
select CONVERT (datetime, field, 101)
from table;
But I keep getting this error:
"Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string."
I presume because the format my field is in isn't standard so SQL can't recognize it?
Any help would be appreciated. Apologies if this has been covered before - I've spent 4 hours searching this and other sites but can't find the answer...
Shouldn't you be casting to Datetime2 type instead? The Datetime has precision only as high as to milliseconds.

SQL Server (T-SQL) datetime conversion

A silly question maybe but I wanted clarification. I've created a script that has a date parameter like so:
DECLARE #dateparam as datetime
SET #dateparam = '01-01-2013 00:00:00'
This looks like it is working when I test it even if the date string is not in "correct" format yyyy-MM-dd hh:mm:ss. I changed my computer regional settings to English and the script still did what it was supposed to do.
Is this because of SQL Server 2008 R2 that I have in my computer that it knows how to convert the date or can I ran into trouble with using a dateformat like I have used?
Converting 01-01-2013 won't expose issues such as which 01 is the month, and which is the day.
It's not a safe format.
The safe formats (for converting to datetime, rather than to datetime2) are:
YYYYMMDD 20121201
YYYY-MM-DD'T'hh:mm:ss 2012-12-01T10:43:29
YYYY-MM-DD'T'hh:mm:ss.mil 2012-12-01T10:43:29.337
Stick to those and only those. (The examples all represent the 1st December 2012)
Or, better yet, don't treat dates as strings at all, if you can avoid it. If you're, for example, calling SQL Server from .NET code, keep that dates as DateTimes in your code, and let ADO.NET and SQL Server deal with any required translations to make them become datetimes - without translating them to and from strings.
You're making an implicit conversion from something that looks like a date, but inf fact is a string ( '01-01-2013 00:00:00'). Rather than trusting on SQL Server to make the correct guess in what format the string is in, you should make the conversion explicit by specifying the format.
This can be done by using CONVERT (not CAST) and specify a 'style'. The different styles are listed here: http://msdn.microsoft.com/en-us/library/ms187928.aspx.