cast MS Access DATE() to Getdate() - sql-server

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.

Related

SQL Server 2008 R2: Query MS Access from SQL Server

Want to retrieve records from Access through linked server in SQL Server and need to convert/cast the column with VARCHAR for some constraint.
My attempt:
SELECT Cola, Colb
FROM MSAccess_LinkedServer...TableName
WHERE CAST([Cola] AS VARCHAR) = 'A123'
Unable to get the result from above query.
But when I remove CAST then I will get the result, but I want to know how to put the cast/convert.
No casting should be needed for text. How about simply:
WHERE [Cola] = 'A123'

How to pass SSIS variables in ODBC SQLCommand expression?

I'm trying to load incremental data from ODBC server to SQL server using common table expression.
When running the query in the Dbeabver application, is executed correctly:
with test as
(
SELECT userid,sum(goldbalance)
FROM Server.events_live
where eventTimestamp>=DATE '2016-01-01' + INTERVAL '-100 day'
group by userid
order by sum(goldbalance) desc)
)
select * from test
when running it from an sql command expression of the ODBC source, it fails due to wrong syntax. It looks as follow:
with test as
(
SELECT userid,sum(goldbalance)
FROM deltadna.events_live
where eventTimestamp>=DATE '"+#[User::datestring]+"' + INTERVAL '-100 day'
group by userid
order by sum(goldbalance) desc)
)
select * from test"
the datestring variable is getting the server date and convert it to string in the format yyyy-mm-dd. I'm usually use this method to pull data from ADO.NET and it works properly.
Is there any other way to pull incremental data from ODBC server using ssis variables?
With OLE DB
Try this code, it works for me with my own tables with SQL Server :
SELECT userid,sum(goldbalance) AS SUMGOLD
FROM deltadna.events_live
WHERE eventTimestamp >= DATEADD(DAY, -100,CONVERT(DATE,?))
GROUP BY userid
ORDER BY SUMGOLD desc
You have to click on Parameters in the OLEDB Source Editor to configure what you need. Use the '?' to represent a variable in your query.
If you query if too complicated, stored it in a stored procedure and call it like this:
EXEC shema.storedProcedureName ?
And map the '?' to your variable #user::DateString
With ODBC
The expressions are outside the data flow in Data Flow Properties.
Select the expression property and add your dynamic query.
And your expression will be
"SELECT userid,sum(goldbalance) AS SumGold
FROM deltadna.events_live
where eventTimestamp>=DATE "+#[User::datestring]+" +INTERVAL '-100 day'
group by userid
order by SumGold desc"

Inserting date from Access DB into SQL Server 2008R2

It seemed to be an easy task but I fail and do not find a solution for my problem: I have a local table in Access 2010 with a date/time column and I want to update a column in a SQL Server table with a datatype date.
Sending the date/time values direct to the SQL Server table fails, same with converting the date/time column with this VBA function:
Function DateForSQL(dteDate) As String
DateForSQL = "'" & Format(CDate(dteDate), "yyyy-mm-dd") & "'"
End Function
which gives
DateForSQL(Date()) = '2016-01-14'
and should work, I assumed.
The update command is this:
UPDATE SQL_table
INNER JOIN local_table ON SQL_table.ID = local_table.ID
SET SQL_table.DateField = DateForSQL(local_table.DateField)
But it fails again in Access with a type conversion error.
Even when changing the SQL Server table column to datetime I get the same error.
Same with sending to SQL a string like '14/01/2016' or '01/14/2016'.
The only thing I could do - eventually - is to change the datetime to text in Access and try again, but this could not be the only solution.
Any help?
Thanks
Michael
First of all, I recommend to use the ISO-8601 format for your date - which is YYYYMMDD (no dashes, nothing) - this works for all regional & language settings in SQL Server.
Next, I'm not sure about Access' SQL syntax, but in SQL Server, your UPDATE statement would be to be something like this:
UPDATE sql
SET sql.DateField = DateForSQL(local_table.DateField)
FROM local_table local
INNER JOIN SQL_table sql ON local.ID = sql.ID
First UPDATE, then SET, then FROM and INNER JOIN ...

Convert Access query to SQL

I am converting Access query to SQL Server.
I want to convert below lines to SQL
1. Format (210.6, "Standard")
2. Format (210.6, "#,##0.00")
How do i convert it to SQL query.
I have tried with below, but still not able to find the solution.
For the first query, i found below solution, which is correct
1. CONVERT(varchar, CAST(tSRO.OutputF11 AS money), 1)
Now, for second query, i do not know what i have to do.
From SQL Server 2012+ you can use FORMAT:
SELECT FORMAT(210.6, '#,##0.00') -- 210.60
SELECT FORMAT(1210.6, '#,##0.00') -- 1,210.60
LiveDemo
SQL Server before 2012:
SELECT REPLACE(CONVERT(VARCHAR,CONVERT(MONEY, 1210.6),1),'.00','') -- 1,210.60
LiveDemo2
Warning:
This operation is pure for presentation layer and should be done in application.

combining sql server spatial and NHibernate with CreateSqlQuery

I have not mapped geography table in sql server 2008, and I want to query it with a polygon instance in c# using nhibernate.
In the beggining I was trying to use sql server spatial directly but encountered this problem:
Using SQL Server 2008 Geography types with nHibernate's CreateSQLQuery
My second try was this:
session.CreateSQLQuery("select [shape] from [table] where (:codeShape).STIntersects([shape]) = 1").SetParameter("codeShape", codeShape, NHibernateUtil.Custom(typeof(MsSql2008GeographyType)));
however this try also raise an exception:
Could not execute query [select [shape] from [table] where (?).STIntersects([shape]) = 1")] Name:codeShape - Value:POLYGON((30 40, ...))
and the inner exception is:
The specified input does not represent a valid geography instance.
That's despite codeShape.IsValid returns true.
When I run this query directly in sql server I get the expected result.
any ideas or solutions?
Thanks.
problem resolved. I had to reverse the points of the polygon.

Resources