How to convert a SQL field stored as a tick into a date - sql

I am exporting info from a database that has field with a birthdate stored as a tick. I need to convert this to a regular date. I can either do it in the SQL statement (if there is a way?) or convert it into excel since I will import my data there?
EDIT: Sorry, not very familiar with ticks, so wasn't sure what info to include. The database is postgreSQL. Just putting it in a format mm/dd/yyyy is fine (excel should understand that).

You need to convert the .NET ticks to UNIX Epoch and then to a postgreSQL timestamp.
to_timestamp((("date" - 621355968000000000) / 10000000))
This code will work with version 9 and above

Related

I try to convert date format in sql but it still doesn't work

I use the CONVERT() function to trying convert the date format like DD/MM/YYYY with code 103 when I query database, and nothing happens. The data field still displays default format with YYYY/MM/DD.
UPDATE STAFF
SET BIRTH = CONVERT(smalldatetime,'26/08/1900',103)
WHERE ID = 'SF01'
How can I fix this problem ? I'm a newbie so i don't know lots of SQL
Avoid formatting dates in your database, it should be done in the application that queries the data. But IF you still need to do it in your database for whatever reason:
If you want to store the date in the DD/MM/YYYY format in your table then you can do 2 things,
Change your column data type to varchar(not ideal and you should try avoiding this method)
Change the regional settings on the machine running SqlServer and keep it as a date type (this is somewhat useless if you plan on querying data from another app as the app will probably format the date to its local format).
In case you decide to store it as varchar you will need to use the Format function when inserting or updating like this: Format(MyDate, 'DD/MM/YYYY').

Are there SQL datatypes that don't work with R?

I am trying run an sqlQuery in Rstudio which seems to crash the program. I want to use the RODBC package to import a name called package name and elapsed time from a Oracle database. When I try to do an sqlQuery such as the following
dataframe <- sqlQuery(channel,
"select package_name, elapsed_time from fooSchema.barTable")
When I run this with just the package_name or other fields in the table, it works fine. If I try to run this with the elapsed_time, RStudio crashes. The datatype of elapsed_time is INTERVAL DAY (3) TO SECOND (6) so one record for example looks like this, "+000 00:00:00.22723"
Are there certain data types, such as Interval Day to Second, from Oracle that don't work in RStudio or R in general?
The problem isn't R, Rstudio, or even RODBC. The problem is that Oracle doesn't support interval data types for ODBC connections.
It is under section E.1
https://docs.oracle.com/cd/B28359_01/server.111/b32009/app_odbc.htm#CIHBFHCG
To get back to your question in a more general sense. Base R supports Date, POSIXct, and POSIXlt objects.
Dates and POSIXct objects are stored as the number of days/seconds respectively since 1/1/1970 whereas POSIXlt is a list of elements.
Whatever SQL connector you're using will need to coerce the SQL version of a date and time into one of the above. Sometimes it'll just convert to a character string. For instance with RPostgreSQL it'll take columns stored as Postgre's Date type as a character but Postgres timestamp columns will be coerced into POSIXct directly.

How do you convert SQL mm/dd/yy to mm/dd only?

How do you convert SQL mm/dd/yy datetime to mm/dd only? On Microsoft server.
Thanks all.
With dates and times it is an extremely common mistake to believe that what you see is what is stored. If the field is date, datetime, smalldatetime or datetime2 then what is stored are integers, not strings. So if the field is one of these, then:
convert(varchar(5),[date_field],1)
or
format([date_field],'MM/dd') -- mssql 2012 onward
If the information is a string already then left() will do the job.
Since you have specified an input format, the input must already be a string. Simply truncate with
cast(dateIn as char(5)).
You can use LEFT to just return the day and month:
SELECT LEFT('12/12/2000', 5)
I realize this isn't directly answering your question the way you asked it, but the best advice I can give is: Don't.
Instead, send back the field in its native datetime type. The database is not the place to be doing formatting. Instead, format the date in your application code.
For example, if you are calling SQL Server from a C#/.NET application, you could retrieve the value from a DataReader like this:
DateTime dt = (DateTime) reader["YourDateTime"];
Then you would format it as a string like this:
string s = dt.ToString("MM/dd");
This will ensure that the date is formatted correctly. If you are using a different language to call SQL Server, there are probably similar methods in that language.
One of the problems with the other approach mentioned (trunacating the string) is that the original value might not be formatted in mm/dd/yyyy to begin with. That all depends on the environment settings where the SQL Server is running. If you run the same code on an environment with dd/mm/yyyy settings, you would have unexpected results. This is avoided by using the native data type, the way I described.

Formatting strings into dates in mySQL

I am migrating an Access db to mySQL. I am receiving the tables as CSVs and using Excel to drop and format some columns. The dates are coming to me as m/d/yyyy and go as far back as 1702. Since Excel brilliantly won't format any date before 1900 I am going to have to transform them some other way. Now it would be super simple to write a PHP script to iterate over rows and use date('Y-m-d', strtotime($date)) but I was wondering if there was a function or functions in mySQL that would be the equivalent?
Try the STR_TO_DATE function. e.g.
SELECT STR_TO_DATE('4/1/1960', '%m/%d/%Y');

Convert Coldfusion Error.DateTime to a normal format?

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!