Castle ActiveRecord - can I use the SQL Server 2008 "time" data type? - castle-activerecord

I'm working on a project where I need to track times, such as "2:15 PM" without dates. I'm using Castle ActiveRecord. Is there a way I can specify using the Castle attributes that I want my field to be a time data type in SQL Server 2008? And if so, what .NET type would I make the field? DateTime?
In addition, I may need to attach time zones to these times. What's the best way to do this using Castle ActiveRecord and SQL Server 2008?

According to the MSDN docs the time datatype corresponds to a TimeSpan .net type, and the NHibernate type is "TimeAsTimeSpan". In ActiveRecord you'd use the ColumnType property to define this, see this example.
If you want to include time zones I recommend using DateTimeOffset instead. I don't see how time zones would make sense in a TimeSpan... Or store the timezone in a separate field and do the conversions yourself...

Related

How to store date and time in UTC in SQL Server datetimeoffset with JDBC

My Spring Boot application requires to store and express all dates and times in UTC, so the POJOs in the application use OffsetDateTime data type and the SQL Server 2016 database use datetimeoffset(7) column type. The JDBC library is ms-sql 6.1.0.0, a JDBC 4.2 compliant driver. Version of JVM is Oracle Java 8.
I am using Spring JDBC and NamedParameterJdbcTemplate to implement the DAO implementation class. No JPA, ORM, etc.
When inserting a record, I use the jdbcTemplate.update(String, Map<String,Object>). This appears to be working, except that values saved in the database are not the same as what I supplied via the Map. For e.g. namedParameters.put("record_datetime", someEntity.getRecordDatetime()); where that object's recordDatetime property holds the value 2018-05-31T13:20:01Z is saved in the database as 2018-05-31T20:20:01.0000000+00:00. I happen to be in a GMT-7 timezone, but the property uses a datatype of OffsetDataTime with no offset, i.e. it is GMT essentially.
I have a bigger issue on hand when trying to retrieve the value from the database. I use jdbcTemplate.queryForObject(String, Map<String,?>, RowMapper<T>) and in the RowMapper, I use resultSet.getObject("record_datetime", OffsetDateTime.class). This results in an exception:
java.sql.SQLFeatureNotSupportedException: This operation is not supported.
I tried using OffsetDateTime.ofInstant(resultSet.getObject("record_datetime", Instant.class), ZoneId.of("UTC")). Same exception as above is thrown.
This has to be a very common requirement, to be able to store values in UTC, and/or with offset values. In other applications, I employed java.util.Date and either java.sql.Date (or java.sql.Timestamp) in the DAO to persist date or date-time values, and that generally worked well. I am somehow finding this much harder to implement.

Playframework evolutions + SQL Server. Generate correct schema script?

I've been trying with no success to run evolutions + slick to generate schema for a MSQLServer database.
I am using Play 2.3.x, scala 2.11.6, slick, and SQLServer 2014.
I could make it connect as well, but the script which is generated contains lots of "errors" relates to data types, like the use of BOOLEAN and TIMESTAMP which are types that SQLServer does not use.
The script should use the types BIT instead of BOOLEAN, DATETIME instead of TIMESTAMP, and UNIQUEIDENTIFIER instead of UUID.
Does anyone know a workaround for that?
These datatypes and all specific to database so there is no workaround here.
You have change the data types based on the selected database otherwise you will get the same error in another database as well.

Create datetime2 as custom datatype in SQL Server 2005

Many have come across the fact that SQL Server 2005 doesn't support datetime2. I was wondering if I can add it as a custom datatype instead.
I created a custom type with the name datetime2 so that's done.
Now I need to set the min date value, but is that even possible, since the custom type is based on the datetime type?
Short answer: No you can't.
Furthermore, DATETIME2 has additional properties, regarding fractional precision. I'd highly recommend any SQL Server DBA to migrate their server to at least 2008, to open up the rich features that are not available in 2005. I don't see any advantage to using something that is largely deprecated and over 10 years old. Especially since you're resorting to creating UDTs that will potentially create all kinds of RDBMS havoc in the future.
To get over this I changed the datatypes in the database to varchar. next I modified the stored procedure to do the conversion between the date entered in the search form and the values in the database. It's an ugly solution and I have to 'manually' compare year, month and day values, but it works in the end with a minimal effort. Customer happy, developer... on the fence :-)

Force a default datetime format in ms access

I'm currently porting a Access 2003 app to use a SQL Server 2005 back-end, and I'm having trouble with the datetime representations.
As I understand it, the default Access behavior is to use the datetime format defined on the local machine's regional settings, as do SQL-Server. Is there a way to force Access to use another default format (other than those available in the "Format" property dropdown list), something like Format = "dd/mm/yyyy"?
My problem is that a good many forms in the app have sub-forms whose data is linked to the parent via relation implying datetime and numeric values (terrible design, I know.)
Now, when retrieving the data, the date will print ok, using a yyyy/mm/dd hh:mm:ss format, but I cannot make new entries, or inserts from the forms as SQL server will complain that the text-data overflowed the capacity for a datetime, or that the engine cannot find the parent record.
I'm using a file-based DSN to connect to the backend.
Thanks for any insight in the matter,
Pascal
Dates are stored in MS Access as numbers. You can set custom formats for controls, such as dd/mm/yyyy, but it nearly always means a deeper problem.
More info: http://office.microsoft.com/en-ie/access-help/format-property-date-time-data-type-HA001232739.aspx

SQLBulkCopy can't convert Time to DateTime

I am writing a small utility to copy a database from a proprietary ODBC database into a SQL Server database. Everything is working great, except when I use SQLBulkCopy to copy over the data. It works in most cases, but not when it's trying to copy a TIME field into a SQL DATETIME. I get this error:
The given value of type TimeSpan from the data source cannot be converted to type datetime of the specified target column.
When I create the schema in SQL Server I have to make the DATE and TIME fields DATETIME types in SQL Server, so there is no way around this. I wanted to use SQLBulkCopy so I didn't have to manually read through every record in every table and wrap logic around the special cases. Before I go down that road, is there another way I can do this? I have no control at all on the source database schema or content.
I assume you are dealing with pre-SQL Server 2008. SQL Server 2008 has DATE and TIME data types.
I think you would have to use a DataTable which matched the SQL Server schema and load this from your source reader, appropriately changing any TIME to a DATETIME by adding date information (e.g. 1/1/1900). Then use WriteToServer(DataTable). You might want to do it in batches, since you may use a bunch of memory reading it all into a DataTable.
Any particular reason you can't use SSIS?

Resources