SQL converting date/time to unix timestamp [duplicate] - sql

This question already has answers here:
SQL Server datetime to bigint (epoch) overflow
(4 answers)
Closed 1 year ago.
im running an SSIS job that is pulling in multiple string columns into a SQL sever 2016 database, however i need to create a row index so that my job knows when to get new data. i have a date/time string column that im pulling in. i want to convert this to a unix timestamp so that i can use that in my code for when to grab new data.
is there a way to do this? my string column with the date/time that im pulling in looks like this:
'2020.07.29 08:43:53' '2021.05.03 12:50:22' etc..
what would be the best way to convert all the strings in my 'datetime' column to unix timestamps?
thanks

You can convert the string to a datetime using:
select convert(datetime, '2021.05.03 12:50:22')
You can then convert this to Unix epoch time (seconds since 1970-01-01) using:
select datediff(second, '1970-01-01', convert(datetime, '2021.05.03 12:50:22'))
Here is a db<>fiddle.

Related

Convert date in string format to date data type [duplicate]

This question already has answers here:
Amazon Athena Convert String to Date
(2 answers)
Closed 3 years ago.
I would like to convert date in string format ‘mmddyy’ (120618) to date and find the max of date in an Athena table. How can write this sql query ?
You can use date_parse() to convert the string into a date:
select date_parse('120618', '%c%d%y')
Therefore, you could use it like this:
SELECT
MAX(date_parse(date_field, '%c%d%y')) as dt
FROM table
See: Date and Time Functions and Operators — Presto Documentation
By the way, you should tell whoever made that original data format that it is a poor way to store dates. ISO format is better (eg 2018-12-06).
Can't you just output your date by using the ORDER BY , plus ASC | DESC (depends on what you mean by "max of date"), command and just use the first output you get? By using LIMIT?

Best way to convert Date Format in SQL from yyyymmdd to yyyy-mm-dd format [duplicate]

This question already has answers here:
How to get a date in YYYY-MM-DD format from a TSQL datetime field?
(25 answers)
Closed 5 years ago.
I have a bunch of dates in the format yyyymmdd without the "-" separated. What is the easiest way to change to yyyy-mm-dd format using only SQL with read access only.
i.e 20111230 -> 2011-12-30
One line or optimal performance solution preferable that works in Microsoft SQL
DECLARE #dtr VARCHAR(25) = '20111230'
SELECT CONVERT(char(10),CAST(#dtr AS DATE),126)
-- sqlserver 2012
SELECT FORMAT(CAST(#dtr AS DATE),'yyyy-MM-dd' )

Convert datetime column value from SQL Server in vb.net

I am trying to convert a datetime column from my SQL Server to date in my vb.net program. I am importing my column with a SQL query from my vb.net source but I don't know how to convert it to date? Any ideas?
adapter_view.SelectCommand.CommandText = "SELECT [action_date] from table_1"
The thing is that my columns has hours minutes etc I want to get only the date from that column.
CONVERT(varchar,b.[Action Date],101) as Action_Date
With that code i convert my datetime to date format
SELECT CONVERT(varchar(10),Action_Date,101) as Action_Date from Table

Oracle timestamp in number format SQL [duplicate]

This question already has answers here:
Convert Unixtime to Datetime SQL (Oracle)
(2 answers)
Closed 9 years ago.
I have my datetime column in table defined as Oracle NUMBER data type which contains data in this format 1363709957 for example.
I want to get these numbers in query so i can execute my insert scripts which will put in current time in there. What is the oracle SQL for that?
Your time stamp appears to be a standard Unix time stamp. You need to use arithmetic to convert this here's a post on OTN about this,
and one for the other direction.

Oracle Number data type to datetime format [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Oracle current_timestamp to seconds conversion
Convert Unixtime to Datetime SQL (Oracle)
I have been given a database wherein the datetime/timestamp column has been defined in NUMBER data type. What oracle function should i use to get the date time out of it?
Ex. In my table : Sample_Table, column time_touched is defined with oracle data type NUMBER. This column is supposed to be date time/timestamp. When I query the DB, I get numbers like 1355218434,1355218851 etc. These are representation of date time. But in query, what function should i use to get it display date time?
It looks like it is a UNIX timestamp (seconds since epoch 1970-01-01) where for example 1355218434 represents Tue, 11 Dec 2012 09:33:54 GMT (see converter here)
EDIT: Apparently the example below only works for MySQL, I thought that it also worked on Oracle because of the domain it is on but I was wrong. The solution is in the answer in this StackOverflow question: Convert Unixtime to Datetime SQL (Oracle)
You could use the FROM_UNIXTIME function to get what you want, see http://docs.oracle.com/cd/E17952_01/refman-5.0-en/date-and-time-functions.html#function_from-unixtime for reference.
Try something like this (untested):
SELECT FROM_UNIXTIME(time_touched) FROM Sample_Table
Your question is somewhat related to:
https://forums.oracle.com/forums/thread.jspa?threadID=2473501&tstart=75
Convert Unixtime to Datetime SQL (Oracle)