MS Access SQL Server DB - Query Syntax for CAST Function - sql

I have converted an Access db (.mdb) to SQL Server. In the meantime I still need to use Access as a front end until new application forms are constructed. Can someone tell me what I might do to fix the situation where:
In Access 2007, a query such as:
SELECT *
FROM TransactionTotals
WHERE TransactionTotals.[Date]= Date()
ORDER BY TransactionTotals.EntryID DESC;
worked, however since the Date() function will not work with SQL Server, with help in a previous post the correct syntax is:
SELECT *
FROM TransactionTotals
WHERE TransactionTotals.[Date]= CAST(GETDATE() AS DATE)
ORDER BY TransactionTotals.EntryID DESC;
BUT! Although the code above will work in a direct SQL Server query (SQL Management Studio), it will be tossed out in Access with a Syntax Error response on the WHERE clause.
Can something be done in Access so I can still run my query bound forms.

I usually do exactly what you do, Access first before migration to SQL server. Access has some really weird syntax compared to server type databases especially when it comes to JOIN clauses and dates, GETDATE() only works on SQL Server, in Access, try this...
SELECT *
FROM TransactionTotals
WHERE TransactionTotals.[Date]= Format(NOW(),"General Date")
ORDER BY TransactionTotals.EntryID DESC;
Feel free to change the format of the date, with or without time.

Related

Pulling a SQL Server date to be used in Oracle query within SSIS environment is ignored in the Oracle query

I am having trouble in my Oracle query that uses a variable stored in SSIS which has a date that is pulled from sql server.
I am using an execute sql task that simply gets a max date from a sql server table and stores it in a variable. E.g.
SELECT MAX(t.Date) FROM table t;
I then want to use that variable in my Oracle query which is an ADO.NET source connection. I noticed you can't parameterize in those connections and found the work around where you use the sql expression with your user variable in it. So now my Oracle source query looks something like this:
"SELECT DISTINCT t.* FROM table t WHERE TO_CHAR(t.LastUpdateDate, 'YYYY-MM-DD') > " + "'#[User::LastUpdateDate]'"
The query syntax itself is fine, but when I run it, it is pulling all rows and seems to be completely ignoring the where clause of the date.
I've tried removing the TO_CHAR from LastUpdateDate.
I've tried adding a TO_CHAR to my user variable #[User::LastUpdateDate].
I've tried using the CONVERSION() function from sql server on #[User::LastUpdateDate].
Nothing seems to work and the query just runs and pulls in all data as if I don't have the WHERE clause on the query.
Does anyone know how to rectify this issue or point out what I might be doing wrong?
Thank you for any and all help!
**EDIT:
My date being pulled from SQL Server is in this format: 2022-09-01 20:17:58.0000000
This is not an answer, just troubleshooting advice
You do not say what data type #[User::LastUpdateDate] is, I'll assume it's a datetime
Ideally all datetime data should be kept in datetime data types, then format becomes completely irrelevant. However since it's difficult to parameterise Oracle queries in SSIS, you have to concoct a string to be submitted. Now date format does become important.
On to something a little different, it is a very good habit performancewise, to not put functions around columns that you are searching on. This is called sargability - look it up.
Given these things, I suggest that you concoct your required SQL query bit by bit and troubleshoot.
First, format your date parameter as an Oracle date literal. Remember this is normally a bad and unecessary thing. We are only doing it because we have to concoct a SQL string.
So create another SSIS variable called strLastUpdateDate and put this hideous expression in it:
RIGHT("0" + (DT_STR,2,1252)DATEPART( "dd" , #[User::LastUpdateDate] ), 2) + '-' +
(DT_STR,3,1252)DATEPART( "mmm" , #[User::LastUpdateDate] ) + '-' +
(DT_STR,4,1252)DATEPART("yyyy" , #[User::LastUpdateDate] )
Yes this is ludicrously long code but it will turn your date variable into a Oracle string literal. You could simplify this by putting it into your original max query but lets not go there. Use whatever debugging technique you have to confirm that it works as expected.
Now you should be able to use this:
"SELECT t.*, '"+#[User::LastUpdateDate]+"' As MyStrDate FROM table t WHERE
t.LastUpdateDate > '" #[User::strLastUpdateDate] + "'"
You can try running that and see if it makes any difference. Make sure you use this https://dba.stackexchange.com/questions/8828/how-do-you-show-sql-executing-on-an-oracle-database to monitor what is actually being submitted to Oracle.
This is all from memory and googling - I haven't done SSIS for many years now
I suspect after all this you may still have the same problem because I recall from many years having the same mysterious issue.

PROGRESS SYNTAX USING OPENQUERY FOR DATEADD EQUIVALENT

I have an open query I am using to connect to a PROGRESS ODBC, I cannot get the syntax equivalent from SQL of
SELECT DATEADD(DAY, -1, GETDATE())
The Progress SQL documentation is here:
https://documentation.progress.com/output/OpenEdge117/openedge117/#page/dmsrf%2Fadd-months.html%23wwID0EGGFR
There is no DATEADD() function although there is ADD_MONTHS(). I'm not much of a SQL coder but I expect that you could probably cobble something together with the other date related functions.
I came up with the following to get records where the date is greater than two days prior:
where tr_date =
to_date({ fn convert (month(curdate()), sql_varchar)} + '/' + { fn convert(dayofmonth(curdate()) -2, sql_varchar)} + '/' + { fn convert (year(curdate()), sql_varchar)})
It heavily relies on curdate() which you apparently can't use in a select statement, only in a where clause.
In SQL Server I would have done all of this in front of the query, put the results in a variable and then used the variable in the query's where clause, but Progress also apparently doesn't have the concept of variables.
The problem with the above is that it doesn't account for the beginning of the month where substracting 2 from 1 would result in -1 and therefore an invalid date. So the algorithm to account for this becomes more complicated. All more doable if you had variables but trickier when you have to do it all on the fly.
My purpose was to generate a new query each day for an incremental data integration process using SSIS. I switched over to dynamically generating the query in SQL Server and inserting the results into an SSIS configuration table that the package reads. With that method you end up using good ol' reliable dateadd in SQL Server. That unfortunately doesn't help much if you're trying to do this in Progress with SQL there.

Why is MS Access 2010 SQL choking on this query?

The following query will not show up in design view and if you trying to make it show it locks up MS Access and you have to use the Task Manager to stop MS Access. The query actually runs and produces the correct results. If there is a better way I will certainly accept that.
SELECT
log_metric_N.metric_title,
log_metric_N.metric_N
FROM
(
SELECT
tref_log_metrics_weights_and_factors.metric_title,
[metric_base].[metric_count],
[metric_base].[metric_sum],
(([metric_base].[metric_count]*[tref_log_metrics_weights_and_factors].[metric_weight])/[metric_base].[metric_sum]) AS metric_N
FROM
tref_log_metrics_weights_and_factors,
(
SELECT
Count(tref_log_metrics_weights_and_factors.metric_weight) AS metric_count,
Sum(tref_log_metrics_weights_and_factors.metric_weight) AS metric_sum
FROM
tref_log_metrics_weights_and_factors
WHERE (((tref_log_metrics_weights_and_factors.metric_weight)<>0))
) as metric_base
) AS log_metric_N;
#HansUp you were exactly right on. I forgot all about the Domain functions and they work perfectly without the complexity. Below is the resultant SQL statement.
SELECT
tref_log_metrics_weights_and_factors.metric_title,
DCount("[metric_weight]","tref_log_metrics_weights_and_factors","[metric_weight]<>0") AS metric_count,
Dsum("[metric_weight]","tref_log_metrics_weights_and_factors") AS metric_sum,
(([metric_count]*[tref_log_metrics_weights_and_factors].[metric_weight])/[metric_sum]) AS metric_N
FROM
tref_log_metrics_weights_and_factors

Using a query that is stored in a table

My database has two tables: t_computers and t_queries.
This query shows me which computers are laptops
select *
from t_computers
where type = 'Laptop'
In the table t_queries I have stored dynamic SQL queries.
SELECT QuerySQL
from t_query
where QueryName = 'Clients that have not been started in 30 days'
The first result is the SQL query that would give me this information.
Now for the complicated part, I want to only select computers that have the type 'Laptop' and are returned if I run the query that is stored in the table.
So something like this
select *
from t_computers
where type = 'Laptop' and
(computer is returned for (SELECT QuerySQL
from query
where QueryName = 'Clients that have not been started in 30 days'))
Is this even possible? I am using SQL Server 2008 R2
I have used a very simplified example.
Some background information on why I want to use the query saved in the table: With our Client Management System (similar to SCCM) Administrators can easily create "views" of Clients. For example Filtering out all Computers that have an IP starting with 10.*. As soon as they save the view, a SQL query is created and saved in the table t_queries. This one query that I want to compare against changes quite often.
Yes it is possible, but as said by the commenters, I strongly unadvise you to execute arbitrary code coming from your users, no matter how much you trust them. You would have very little possibilities to enforce security rules and may open yourself to devastating security breaches.
The way it is properly done in other systems is to use a specific query language (custom or not) that you interpret and "translate" to SQL if needed. That allows you to limit the possible operations to what is strictly necessary.
After that disclaimer, here is an answer to your question (untested, I don't have SQL Server on this laptop so I may have messed up a bit with the quotes) :
exec('select *
from t_computers
where type = ''Laptop'' and
(computer is returned for ('+SELECT TOP(1) QuerySQL
from query
where QueryName = 'Clients that have not been started in 30 days'+'))');

Is There a Way to Convert 'datetime' Format to 'timestamp' in Sql Server CE?

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)