Converting PostgreSql timestamp query to MS SQL/Azure SQL - sql-server

I'm currently in the process of converting some basic sql scripts from Postgresql to Azure Sql. I'm newbie to sql, but I really can't understand epoch/unix time in Azure SQL / SQL Server. My application is using bigint epoch time as event timestamp and I want to make a housekeep script for the database and for the table where events are stored.
How do you work with epoch time intervals in MS or Azure SQL? What would be the equivalent in Azure SQL to the following query?
SELECT count(*) FROM info_event WHERE event_time < (SELECT cast(EXTRACT(epoch FROM current_timestamp - INTERVAL '1 MONTH') AS bigint) * 1000);

The solution was.
SELECT * FROM table WHERE epoch_column < (SELECT cast(DATEDIFF(second,'1970-01-01 00:00:00',(DATEADD(month,-1,GETDATE())))AS bigint)* 1000 );
Thanks for the help!

Related

cast MS Access DATE() to Getdate()

I have an ODBC linked table in MS Access to SQL Server. In a query in MS Access, I have the following expression on a field:
DATE() - [DATE] (a field).
I need to change MS Access function DATE(), i.e. current date to SQL Server GETDATE(). How can I cast this in the expression builder in MS Access?
You can't really use the GetDate() t-sql server side value.
However, what you could do, is in place of using linked table to the sql server table?
You could create a view and have this
SELECT *, GETDATE() as MyServerDate FROM tblInvoices.
Now, in your client side query, you have a column called MyServerDate.
And thus you could do this:
SELECT *, (DATE() - [MyServerDate] as MydueDate from MyView
Of course the other way would be to use a pass-though query, but they are read-only. So if the sql is only for a report, or some screen display, then you could consider using a 100% server side query. So, you create the query in Access, but set it as a PT query. As a result, the raw SQL syntax in that Access query you build will have to be t-sql sql, not access sql.

Would like to use this SQL Server date exactly in Oracle query

2017-11-17 06:35:41.0000000 - this is a value of a column FileProcessDate in a SQL Server table. I am trying to build an incremental logic and I would like to use this date (directly) in Oracle to build that logic.
I am using the following query in Oracle
SELECT *
FROM EVENT
WHERE TIMESTAMP > TO_DATE('2017-11-17 06:35:41.0000000', 'MM/DD/YYYY HH:MI:SS')
But I am getting an error
not a valid month
You need to use correct timeformat:
SELECT *
FROM EVENT
WHERE TIMESTAMP>TO_TIMESTAMP('2017-11-17 06:35:41.0000000','YYYY-MM-DD HH24:MI:SS.FF')
DBFiddle Demo

how to identify "unused" Azure SQL database

We have approximately 2 dozen SQL Database in our Azure portal. I have been asked to evaluate these and see if any are no longer being used and can be dropped. I know how to query their original Quotas and current sizes but I am baffled as to how to query DTUs.
How can I query each DB and see when someone last logged into or initiated any queries?
Thank you!
The following query should give you an idea if the database has been used based on resource consumption over the last 7 days:
SELECT *
FROM sys.dm_db_resource_stats
WHERE --database_name = 'AdventureWorksLT' AND
end_time > DATEADD(day, -7, GETDATE())
ORDER BY end_time DESC;

Copy data from a SQL Server table into a historic table and add a timestamp of the time copied?

I'm trying to work out a specific way to copy all data from a particular table (let's call it opportunities) and copy it into a new table, with a timestamp of the date copied into the new table, for the sole purpose of generating historic data into a database hosted in Azure Data Warehousing.
What's the best way to do this? So far I've gone and created a duplicate table in the data warehouse, with an additional column called datecopied
The query I've started using is:
SELECT OppName, Oppvalue
INTO Hst_Opportunities
FROM dbo.opportunities
I am not really sure where to go from here!
SELECT INTO is not supported in Azure SQL Data Warehouse at this time. You should familiarise yourself with the CREATE TABLE AS or CTAS syntax, which is the equivalent in Azure DW.
If you want to fix the copy date, simply assign it to a variable prior to the CTAS, something like this:
DECLARE #copyDate DATETIME2 = CURRENT_TIMESTAMP
CREATE TABLE dbo.Hst_Opportunities
WITH
(
CLUSTERED COLUMNSTORE INDEX,
DISTRIBUTION = ROUND_ROBIN
)
AS
SELECT OppName, Oppvalue, #copyDate AS copyDate
FROM dbo.opportunities;
I should also mention that the use case for Azure DW is million and billions of rows with terabytes of data. It doesn't tend to do well at low volume, so consider if you need this product, a traditional SQL Server 2016 install, or Azure SQL Database.
You can write insert into select query like below which will work with SQL Server 2008 +, Azure SQL datawarehouse
INSERT INTO Hst_Opportunities
SELECT OppName, Oppvalue, DATEDIFF(SECOND,{d '1970-01-01'},current_timestamp)
FROM dbo.opportunities

Execute query once in SQL Server despite being called mutiple times

I have a situation where a query might be called multiple times from multiple users, but I only want it to run once (per week) against the database. The environment is SQL Server Express so scheduling via SQL Server Agent is not an option. It needs to be 2005 compatible. I'd like to make it as lightweight as possible too, so I'm asking for suggestions. Ideally a database wide declared variable - but I don't think that SQL Server supports such a beast? Thanks
Try something like this:
IF NOT EXISTS ( -- Check if you have the current week content
SELECT *
FROM WeeklyTable
WHERE
DATEPART(YEAR, DateCr) = DATEPART(YEAR, GETDATE())
AND
DATEPART(WEEK, DateCr) = DATEPART(WEEK, GETDATE())
)
BEGIN
-- delete old content
DELETE WeeklyTable
-- insert new content
INSERT INTO WeeklyTable (MyID, MyField1, ... , MyFieldN, DateCr)
SELECT
MyID, MyField1, MyField2, GETDATE()
FROM MainTable
END
You can create indexes you need for the WeeklyTable.
One option would be SQL Scheduler as a add-on to SQL Server Express.
The other option would be to create a small command-line utility that does the querying and schedule that using the Windows Scheduler on the machine where SQL Server Express is installed.
With either of the two setups, you could select the values / numbers you need into a result table once a week, and any requests during the week would be satisfied from that one result table. SQL Server doesn't have "server-wide" variables - but you can always define a table for that purpose...

Resources