Entity Framework and SQL Server datetime - sql-server

I'm using EF with SQL Server and when I'm fetching a DateTime field that is DateTime on the MSSQL and on EF, I would get something like this:
"UpdateTime":"\/Date(1358716865533+0200)\/"
I have no idea what format is this (only that it shows some time with GMT+2 offset).
Any suggestions?
Second, I've tried to use ToString("") on that fields inside the LINQ that fetches the records and got an error that SQL doesn't support formatting this way - so I have no easy way to format that date as I want.
Is there any way to apply formatting?
Thanks!

As said in the comment, this is a Unix format. You'll get a similar string when you serialize a DateTime with JavaScriptSerializer (JSON).
As for your second question. Yes it can be a nuisance that EF does not allow ToString in linq queries. On the other hand, EF is for data access, formatting is for UI. So you better do the formatting after you've fetched data from the database into memory display them somewhere.

Related

Why can I not use format function while Jet-SQL to T-SQL?

I was trying to convert one of my MS Access query containing format to a SQL Server view. I have my view connected to MS Access as linked tables. I was looking at this MS Access to SQL server cheat-sheet to convert Jet-SQL to T-SQL.
The cheat sheet says:
Access: SELECT Format(Value, FormatSpecification) (note: this
always returns a string value)
T-SQL: Do not do this in T-SQL;
format data at your front-end application or report
I cannot format data at my front end because the SQL Server view is linked as linked tables. I cannot have format function in tables.
The cheat sheet does not provide any explanation on why not to do this in T-SQL.
What is the reason behind not using format when converting Jet-SQL to T-SQL?
Obviously, you can format values in T-SQL using the Format function, which only has minor differences with the Access format function.
Generally, though, you shouldn't.
There are multiple reasons why it's discouraged:
Formatted strings are nearly always larger than unformatted dates/numbers, causing additional overhead when transmitting results
If you format in the application layer, the unformatted value is available to you in the application layer to validate/do calculations with/use for conditional formatting/etc. If you format in the data in the database layer, you can't do this without casting back to a date (which is a really bad practice).
If you want variable formatting based on things like locale settings, it's way easier to format in the application layer.
It's certainly not a limitation of SQL Server. It's just a bad practice to use it.

SQL datatype to use for text data which keeps formatting

In T-SQL (SQL Server 2008 and higher), what is the datatype to use for a column which stores text data keeping formatting like breaklines / enters, bullets, tabs etc. in the text? The length of the text can vary substantially.
I have tried NVarchar(max), but a select result set removes all the formatting. Or is this caused by SSMS like on this post here and this post?
Is it also possible to store the text data including the html code?
Sorry for the newbie question, but I saw a few posts on Stackoverflow that confuse me somewhat about this subject in respect which datatype is best to use: Ntext vs Nvarchar; and how to get the data out of the DB but still keep the formatting.
Thanks!
When you save information into SQL Server the information is stored at text, so when you try to insert HTML it is stored as raw HTML code.
From my knowledge there is no way to allow HTML formatting in a SQL Server result set.

ColdFusion 8 + MSSQL 2005 and CLOB datatype on resultset

The environment I am working with is CF8 and SQL 2005 and the datatype CLOB is disabled on the CF administrator. My concern is, will there be a performance ramification by enabling the CLOB datatype in the CF Administrator.
The reason I want/need to enable it is, SQL is building the AJAX XML response. When the response is large, the result is either truncated or returned with multiple rows (depending on how the SQL developer created the stored proc). Enabling CLOB allows the entire result to be returned. The other option I have is to have SQL always return the XML result in multiple rows and have CF join the string for each result row.
Anyone with some experience with this idea or have any thoughts?
Thanks!
I really think that returning Clob data is likely to be less expensive then concatenating multiple rows of data into an XML string and then parsing it (ick!). What you are trying to do is what CLOB is designed for. JDBC handles it pretty well. The performance hit is probably negligible. After all - you have to return the same amount of character data either way, whether in multiple rows or a single field. And to have to "break it up" on the SQL side and then "reassemble" it on the CF side seems like reinventing the wheel to be sure.
I would add that questions like this sometimes mystify me. A modest amount of testing would seem to be able to answer this question to your own satisfaction - no?
I would just have the StoredProc return the data set, or multiple data sets, and just build the XML the way you need it via CF.
I've never needed to use CLOB. I almost always stick to the varchar datatype, and it seems to do the job just fine.
There are also options where you could call the Stored Proc, which triggers MSSQL to generate an actual xml file (not just a string) and simply return you the file name. Then you can use CFFILE action="read" to grab the xml string and parse it accrodingly. Assuming your web server and db have a common file storage area.

SQLBulkCopy and Dates (1/1/1753)

I've got an application which has been working fine for quite a while, but there is an annoying item that continues to get in the way on occasion.
Let's say that I use an object such as OracleDataReader or MySQLDataReader to pass the data to the sqlbulkcopy object for insert. Let's assume that all the columns maps just fine and for the most part, it all works well.
Granted, I don't have control over the source application or database (which is either MySQL or Oracle). So some goof goes into a different application and puts in a date on the invoice table of 5/31/0210. He really meant to put in 5/31/2010, but the application he's using is not validating the data very tightly and the Oracle database accepts it. For all intensive purposes, the data of 5/31/0210 is a valid date for the Oracle db. It might be stupid in terms of data entry, but it is what it is at this point.
Now our OracleDataReader comes along and is transferring this invoice table over to SQL Server via the SQLBulkCopy. It is passing the data to perfectly matched table with the right column names and data types. You can see what is going to happen. This date of 05/31/0210 from Oracle is not accepted by the SQL Server db engine, as the DATETIME field only allows dates from 1/1/1753 to 12/31/9999.
When it encounters this record, it simply fails and gives an overflow error. It doesn't skip the record, it kills the feed. So if it happens a thousand records in on a million record table, you don't get the remaining 999,000 records.
Is there anyway to get around this issue so that the feed will continue?
Ideally, I'd like to move the receiving SQL Server DB to 2008 and use DATETIME2, which would allow for these goofy dates, but unfortunately not all my clients are ready to move to this version yet, so I'm stuck with DATETIME in SQL 2000/2005/2008.
Any ideas on how to get around this without changing the SQL? Ideally, I wouldn't mind if it just skipped the record. I know that I could do this in the SQL for the datareder, but this would be extremely complicated when you have twenty date fields in a single query. It would be maintenance nightmare.
Any thoughts would be appreciated.
One option would be to change the datetime column type to varchar. Then add a derived column for converting the string to datetime. The trick would be to use a function in the derived column to validate the date and put an arbitrary datetime if the coversion will fail. If you do heavy date comparisons, persist the computed column and/or index it.
I say all of this under the impression that sqlbulkcopy is not able to do transforms. Maybe you can. Hopefully, someone will chime in with a way to.
SSIS would be great in this situation, as you could do the transform and also get the performance benefits of the bulk update lock.

Converting strings to DATE in SQL Server

i have an old database that I'm converting from Access to MS SQL Server 2008. In the Access database, dates were written as 02/2008 so MM/YYYY. I want to convert this to a DATE type, where I can show something like MM/DD/YYYY. Is there any way to do that using SQL Server? When I tried CONVERT, it fails beacuse there are some irregular dates, such a
1/2008
01/2004
/2001
/4
etc
so my thought is that I will just write up some code to go through each date and use regex to convert it, and then save it back to the database in a new table.
Any thoughts on how to make this easier?
Looks like your only choice here is some sort of string manipulation like you've proposed. What part of the process would you like to make easier?

Resources