I am working on migration of data from an old system to a new system. As part of migration, the data from the legacy system, (stored in files) is pumped into MS SQL Server. Now my app runs on Oracle. I'm having a problem with the date/timestamp.
The timestamp format in MS SQL Server data is:
2008.12.23 00:00:00
Oracle expects:
23/12/2008 00:00:00
or
23-DEC-2008 00:00:00
What would be the best way to import the data? Oracle's to_date() function didn't work as I thought it would.
I assume you're using insert statements?
Convert your dates using:
TO\_DATE(sql\_server\_value,'YYYY.MM.DD HH24:MI:SS')
You can put a second parameter on the to_date function to specify in what format the incoming data is. You will likely have to make SQL server pump the data out as a string.
http://www.techonthenet.com/oracle/functions/to_date.php
Use CONVERT to convert your internal datetime columns to text in whatever format you wish on the SQL Server side.
Related
Today I faced a problem while porting an SQL server database to H2DB. The SQL queries I had written to insert date time, wasn't working on H2DB. i.e '26-Jun-2019 01:00:00' gave exception on H2DB. My question is what is standard format for inserting date time which is database independent?
The standard format as defined by the SQL standard is the ISO format, prefixed with the keyword DATE, e.g.
DATE '2019-06-26'
or for a timestamp:
TIMESTAMP '2019-06-26 17:42:00'
However not all database products support the SQL standard in that regard, so you will most probably not find one single format that will work across all database products.
I know there's a way to do this in regular Sql Server, and if I'm not mistaken, it looks something like this:
SELECT UNIX_TIMESTAMP(ba_trans_entered) * 1000 AS 'dateUTC'
I do admit, however, that I don't get the * 1000 part, but that's beside the point.
When I try to perform this query in SQL Server CE it just tells me (i.e., WebMatrix tells me):
'UNIX_TIMESTAMP' is not a recognized built-in function name.
I'm assuming UNIX_TIMESTAMP is not supported in Sql Server Compact.
Also, I tried Googling and searching here on SE but no data relevant to SQL Server CE shows up, so there may not be a way in the given environment.
Is there any way to convert 'datetime' (example: 7/13/2007 12:00:00 AM) to timestamp (example: 1184302800000)? I know I can do this in JavaScript, but I was told it might be faster to do this in the query itself, and since I am pulling a ton of data...
The UNIX_TIMESTAMP function does not exist in SQL Server on SQL Server Compact, but you can use DATEDIFF:
DATEDIFF(SECOND,{d '1970-01-01'}, ba_trans_entered)
I have SQL Server 2008 R2 and one of my tables has a date field. When an insert from my ASP.NET page happens, the date is automatically inserted by setting the default field value to getdate(). However, this adds the date in the format YYYY-MM-DD HH:MM:SS.0000000. I really only need the time up to seconds and not the trailing zeros after it.
How can I do this?
A DateTime, whether in SQL Server or .NET code does not have a format. It has an internal representation.
When you want to display the value of the DateTime you format it using a format string.
select (convert(char(19), getdate(), 121))
Gives:
2012-04-06 10:16:50
Up to SQL Server 2008 R2, all you can do is convert the datetime (stored as 8 bytes) you're getting back using a list of possible, supported formats - see the MSDN documentation on CAST and CONVERT for the complete list of supported formats.
With SQL Server 2012, you'll be able to use a FORMAT function much like in .NET - but again, that's a new function in the 2012 version, not available in earlier versions.
At my company we store the information gathered from our site wide error template, into the database, but because of the format of the error.datetime it is making hard for me to do any sql queries for different date ranges.
Has anyone used some t-sql or coldfusion code to convert it to a mm/dd/yyyy format?
Here is an example of the format it currently is as.
Sun Jun 13 21:54:32 CDT 2010
But for any queries, I need to do, I have in a better format, I believe.
On the CF side, you should be able to user createOdbcDateTime() to correctly format it for the database or dateformat() to format it as text. If the date is coming back as text instead of a date object, you could use parseDateTime() to convert to a date object.
As an alternative, you could avoid having to convert dates at all if you just use the SQL Server built-in getDate() function to fill out your date column as the error is being inserted into the database.
It may not be exactly the same time (i.e. it might be out by a ms or 10) but it should be pretty close and perhaps good enough for your purposes.
Just make sure that your database server and application server are time synchronised!
i need to check for a specific DateTime value in my table from my code (VB.NET) and i don't know how to format the DateTime as a string. i've read that SQL Server will recognize my string if it's in either date, time, or date and time format. i mean:
'May 15, 2004'
'5/15/2004'
'20040515'
'2004 4 am'
will SQL Server recognize these strings as valid DateTime values? i'm curious because if i check the actual DateTime values in the table they are in this format:
2/2/2006 3:49:33 PM
Don't put the date/time value in the SQL query in the first place - use a parameterized query and then you don't need to know or care what format SQL Server would parse literals as. You put the placeholder in the SQL, and specify the value as a DateTime in the parameter collection.
You should be using parameterized SQL as a matter of course, in fact - not only does it get rid of formatting and parsing problems like this, but possibly more importantly it's the single most effective weapon against SQL injection attacks.
If not using a parameterized query, use CAST/CONVERT to explicitly change a string to a DATETIME:
SELECT CAST('2/2/2006 3:49:33 PM' AS DATETIME)
On my SQL Server 2005, that returns to me:
2006-02-02 15:49:33.000
Mind that the default date format in SQL Server can be different than what you provide.
This has always been safe that I have found:
YYYY-MM-DD HH:MI:SS
If you're comparing DateTime to DateTime, you don't have to worry about conversion, necessarilly, but yes, Sql Server (at least as of 2k8, and I believe 2k5 as well) will automatically parse a DateTime from a string. That is, if you pass '5/15/2004' it will see 5/15/2004 12:00:00 AM or something similar.
a better way, though, is to use SqlParameters in your SqlCommand from Code.