Access: convert numerical date - sql

I have table where the date field (TimeStringID ) is in numerical format represented as the number of seconds that have past since 12:00am March 1,1980.
In SQL Server the command is DATEADD(s, CAST(TimeStringID AS numeric), '1980-03-01') AS 'StartTime'
The field 1289260817.0000000501 converts to 2021-01-07 00:00:17.000 using the above SQL Server statement.
How would I convert this for Access queries?

I believe this is the equivalent:
select dateadd("s", TimeStringId, #03/01/1980#)

Related

Convert decimal to date format SQL

I am trying to convert a column FC_FROM_DATE containing decimal values (ex. 20,200,721) into a date format 2020/07/21.
I have tried this code
SELECT TO_DATE(CHAR(CAST(FC_FROM_DATE AS DECIMAL(8,0))), 'YYYY/MM/DD')
FROM MARKETS.FORECAST
I get an error
Argument for chr should be between 0 and 127
Would much appreciate your help!
If you are using Oracle then you can use the following query:
SELECT TO_DATE(CAST(FC_FROM_DATE AS VARCHAR(8), 'YYYY/MM/DD') FROM Table
Seeing as the persisted format is YYYYMMDD you could convert the value to a varchar(8) and then use CONVERT to get a Date instance.
SELECT CONVERT(DATE, CAST(FC_FROM_DATE AS VARCHAR(8))) FROM MARKETS.FORECAST
Ideally Dates are stored as Date and a Date with a time component is stored as DATETIME2 (or equivalent if not Sql Server).
Test of the code above
DECLARE #FC_FROM_DATE decimal(8,0) = 20200721
SELECT CONVERT(DATE, CAST(#FC_FROM_DATE AS VARCHAR(8)))
2020-07-21
Disclaimer: This works in MS Sql Server.

SQL Server table's Column has date time value but in unrecognizable format

I am using SQL Server 2016 and have a table with one of the column datatype as BIGINT but its value is 1586862000000
This is not directly recognizable as DateTime value as datatype is also BIGINT, but the name of the column is PREV_EXEC_TIME which gives a hint that value is in DateTime but somewhat in encrypted form plus my guess is 15 stands for 3 pm as I had executed query at 15:00
So my concern is how can I convert this value to a standard DateTime format whenever I query (SELECT)on it
Is there some function like cast or convert to get expected output?
This looks like a Unix timestamp in milliseconds. If you are content with second-level precision, you can use:
select dateadd(second, 1586862000000 / 1000, '1970-01-01')
Unfortunately, SQL Server doesn't support dateadd_big(), but you can add the milliseconds separately if those are needed:
select dateadd(millisecond, 1586862000000 % 1000, dateadd(second, 1586862000000 / 1000, '1970-01-01'))

Oracle RR date format in MS SQL Server

I have an Oracle SQL script that I'm converting to run in MS SQL Server. The script has the to_date function in it and uses the RR date format. Here's the function:
to_date('07-AUG-14','DD-MON-RR')
I'm going to use the MS SQL Server function CONVERT like this
CONVERT(DATETIME, '07-AUG-14', num)
where num is the code of the format I need.
What code should I use in MS SQL Server to get the same type of functionality as the Oracle RR format?
For a U.S. datetime conversion, you can use:
CONVERT(DATETIME, '07-AUG-14', 10)
-- ^^
For other cultures, style (the 3rd argument) will differ; but style needs to be a Without century (yy) value for your purpose.
The semantics of Oracle's RR date format apply to SQL Server's yy date styles too. CAST and CONVERT (Transact-SQL) (for SQL Server 2008) on MSDN explains...
By default, SQL Server interprets two-digit years based on a cutoff year of 2049. That is, the two-digit year 49 is interpreted as 2049 and the two-digit year 50 is interpreted as 1950.
...and lists the numeric values for the Without century (yy) date styles.
(For reference, another SO answer explains the equivalent semantics of Oracle's RR date format.)
You can more broadly confirm the default semantics of SQL Server's (U.S.) yy date style with the following queries...
SELECT CONVERT(DATETIME, '07-AUG-14', 10)
SELECT CONVERT(DATETIME, '07-AUG-49', 10)
SELECT CONVERT(DATETIME, '07-AUG-50', 10)
SELECT CONVERT(DATETIME, '07-AUG-51', 10)
..., which yield...
2014-08-07 00:00:00.000 -- This century assumed.
2049-08-07 00:00:00.000 -- This century assumed.
1950-08-07 00:00:00.000 -- Last century assumed.
1951-08-07 00:00:00.000 -- Last century assumed.
...with a default two-digit year cutoff configuration. (yy date styles for other cultures should behave similarly.)

Sql server convert date to 10 digit integer for comparison

I'm working with a database where dates are stored as 10 digit integers and where the user can query information within certain calendar date ranges. I was wondering what are the steps for converting a calendar date to a 10 digit integer for comparison with dates stored in a database.
I'm working with sql server 2000.
You need SELECT DATEADD(second, 1240494225, '19700101') to convert the number to a date and SELECT DATEDIFF(second, '19700101', #some_date) to go the other way.

converting Epoch timestamp to sql server(human readable format)

I have a problem in converting the Unix timestamp to sql server timestamp.
I have a data in excel sheet and I will import that data through a tool. So I am looking for a code or syntax which can convert that Epoch timestamp to sql server timestamp.
I have 3 different columns with the same format. How can I change the values in those columns.
For Example:
Epoch timestamp ---1291388960
sql server timestamp--- 2010-12-03 15:09:20.000
I have 3 different columns with the same format. How can I change the values in those columns.
To update 3 columns in a table, you can pair DATEADD seconds to the epoch (1 Jan 1970) with the column name, i.e.
update tbl set
datetimecol1 = dateadd(s, epochcol1, '19700101'),
datetimecol2 = dateadd(s, epochcol2, '19700101'),
datetimecol3 = dateadd(s, epochcol3, '19700101')
You can't update in place since a bigint column cannot also be a datetime column. You have to update them into 3 other columns.
Use the DATEADD function:
SELECT DATEADD(ss, 1291388960, '19700101')
...specifying a date of January 1st, 1970. In this example, it was provided in the YYYYMMDD format.
DATEADD will return a DATETIME data type, so if you have a table & column established -- you can use the function to INSERT/UPDATE depending on your needs. Provide details, and I'll clarify. Once you have a DATETIME to work with, you can use CAST or CONVERT to format the date in TSQL.