Microsoft SQL Server - Open Query where > date - sql-server

I am trying to run a select using openquery with filtering result by date, but I have problem with using the date after where clause.
Ideally I would like to be able to pass a variable
set #d = dateadd(day, -30, getdate())
but for the sake of example I will try to use specified date :
Example:
select *
from OPENQUERY([Linked_Server], 'select id, name from Users where LastModifiedDate > ''2017-01-01''')
This returns an error:
INVALID_FIELD:
select id, name from Users where LastModifiedDate > '2017-01-01'
value of filter criterion for field 'LastModifiedDate' must be of type dateTime and should not be enclosed in quotes".
It works ok if I use for example istrue = true, but comparing dates seems to be the problem.
Can someone please advise me on this ?

It looks like you're querying a linked server that is not standard SQL Server but is instead Salesforce which uses SOQL which has a specific format for date and datetime literals. The correct format for date filters in Salesforce is:
WHERE LastModifiedDate > 2017-01-01T00:00:00Z
So your full SQL should be:
SELECT *
FROM OPENQUERY(
[Linked_Server],
'SELECT id, name FROM Users WHERE LastModifiedDate > 2017-01-01T00:00:00Z')

WE use a lot of Open queries here and we've stumbled into that kind of scenario. What we have done in the past is this:
CONVERT(VARCHAR(11),#d,101)
OR
CONVERT(VARCHAR(25),#d,126)
This already converts the date into the format DavidG posted there.
Also, to double check if the query is coming out correctly, we assign the query text into a variable and then we use the print to show the variable, that print which is just simple text that will show on your Message tab alongside your resultset tab. As long as the Where clause shows with only two single quotations, the query you have should work, just in case of doubt, copy the message and run it separately by replacing all doubled single quotations into one single quotation only.
What I had in my message tab on WHERE clause was something like that:
WHERE thisdate BETWEEN ''02/27/2017'' AND ''2017-02-27T23:59:59.990''

Related

How I can Get Records of Specific date in Microsoft Access Database like 10/21/2018 dd/mm/yyyy?

Here is Code of My Query Search in MS-ACCESS (.MDB),
I am trying this
Select * from Records where date like '10/21/2018%'
It will be:
select * from Records where [date] = #10/21/2018#
Addendum:
One method to ignore a time part of the date field:
select * from Records where Fix([date]) = #10/21/2018#
Following Query Solve my issue, Thanks #ashleedawg and #Gustav for your suggestions.
SELECT * FROM TABLE WHERE DATE > #10/20/2018# AND DATE < #10/22/2018#
If your date column is of datatype datetime, using the like operator will convert your datetime to text which depends on regional settings. Also, the placeholder for "any string" in Access is *, not %. Assuming that you used that placeholder to ignore the time, you better try something like this:
Select * from Records where DateValue([date]) = #10/21/2018#

How can I select a date from a table and pass it in as a parameter in SSIS?

I have one single date in one table. I am trying to select this date and pass it into a function that pulls data from another system, based on this date parameter. I think it should be something like this.
Declare #dateCurrent as DateTime
Set #dateCurrent = MyDate
SELECT #dateCurrent = DATEADD(Day,-1,CONVERT(VARCHAR(6),#dateCurrent,112)+'01')
Select top 1 ASOFDATE
FROM dbo.fn_ExtractRawData('all', 'all', #dateCurrent)
I set 'MyDate' as a Variable. I'm not sure if this is the right way to do it. Or, should I simply select the date from the table, like this.
Select AsOfDate
FROM DATE_RAW_DATA
No matter what I try, I'm getting an error that SSIS can't execute the query because the date parameter isn't coming in. How can I get this setup correctly? Thanks.
First, create an Execute SQL Task that gets the date value from your table and saves it into your variable.
Next, create another Execute SQL Task that is connected after the first task, and then use your variable in the query. Assuming you have the date saved in a variable named MyDate, your query for the second SQL Task would look like this:
SELECT TOP 1 ASOFDATE
FROM dbo.fn_ExtractRawData('all', 'all', DATEADD(DAY,-1,CONVERT(VARCHAR(6),?,112)+'01'))
The question mark in the query means you're going to be using an input parameter for that value. So once you have the SQLStatement field value set, click on the "Parameter Mapping" option in the left pane of the Execute SQL Task Editor. For "Variable Name", select your variable from the drop down list, and for "Parameter Name" put 0 (which corresponds to the first question mark in the query). Make sure the variable data type and size are set correctly.

SSRS optional drop down parameter passing dates

I currently have a set of drop down parameters that pass dates to a query. There is a display value available to select for each Work Week.
Create Date Parameter:
Display in the drop down: 2016 01 (1/3/2016) P01-16 Q1-16
Value being passed to the SQL is WEEK_BEGIN_DATE: 1/3/2016
New Ship Date Parameter:
Display in the drop down: 2016 01 (12/27/2016) P12-15 Q4-15
Value being passed to the SQL is SHIP_WEEK_BEGIN_DATE: 12/27/2016
I would like to be able to make both of these optional.
I tried the following in the parameter value SQL to get the parameter to allow nulls but I got a data type error. I did start going down the cast as varchar() route but it was getting messy quick.
SELECT 'NULL' AS WEEK_BEGIN_DATE
UNION
SELECT d.WEEK_BEGIN_DATE FROM DATE d
Any suggestions are much appreciated. Let me know if I can provide any other helpful info.
this should work, you don't need the single quotes
SELECT NULL AS WEEK_BEGIN_DATE
UNION
SELECT d.WEEK_BEGIN_DATE FROM DATE d
to make parameters optional you could in where clause write something like
where (WEEK_BEGIN_DATE = #date or #date is null)
If your parameter of dates was a VARCHAR() you would need to do what Kostya stated. But since it's not, you can click "Allow null value" under Report Parameter Properties (rightclick on your parameter and select properties)

SQL Server Query with variables as MS Access subform record source

I'm trying to figure out the best way to go about building a dynamic query and use it as a record source for a MS Access subform. I've got my WHERE clause statically assigned at the moment just so I could get the form built and make sure data was pulling properly. The main form will have 2 text boxes formatted as ShortDate and 2 subforms below with the query results. The first subform is the query grouped by Employee name and the second subform is a sum of department totals.
FWIW this is an Access 2010 ADP/ADE front end, SQL Server 2008 back end. My current SQL for the Dept totals is as follows:
SELECT COUNT(*) AS TotalNumEstimates, SUM(NumPanels) AS TotalNumPanels, SUM(PriceBase) AS TotalBasePrice, SUM(PriceBase) / SUM(NumPanels) AS ValuePricePerPanel
FROM dbo.tblBid
WHERE (Date > CONVERT(DATETIME, '2016-01-01 00:00:00', 102))
HAVING (SUM(NumPanels) > 0)
I plan on changing the WHERE clause to "WHERE Date BETWEEN #FromDate and #ToDate". Then on the Access form when the dates are set and a "Run Report" button is clicked, programatically set the OnClick event to pass the txtFromDate and txtToDate to the #FromDate and #ToDate respectively, but I can't quite figure that part out.
The only other option I can see would be to type out the whole SQL statement as a string with the txtFromDate and txtToDate declared in the OnClick event and change the subform record source to the new string. Is there a better way to go about doing this?
I believe I have figured out my initial problem of passing the textbox to the WHERE clause by creating a stored procedure. I will repost if I can't get the stored procedure to work, but it seems easy enough.
Use a [permanent] temporary table, which you then remove when the form unloads. If multiple users will use the form simultaneously, you'll need to add a session number of some sort to the name. So:
SELECT COUNT(*) AS TotalNumEstimates, SUM(NumPanels) AS TotalNumPanels, SUM(PriceBase) AS TotalBasePrice, SUM(PriceBase) / SUM(NumPanels) AS ValuePricePerPanel
INTO dbo.TempSubFormTable
FROM dbo.tblBid
WHERE Date > '2016-01-01'
HAVING SUM(NumPanels) > 0
Remove the recordsource from your subform, and give it a public interface:
Public Sub SetRecordSource(tbl as string)
Me.RecordSource = "SELECT * FROM tbl" 'ORDER BY ...
End Sub
Then, before the temporary table is created, make your subform invisible. After creating the temporary table,
Me.subformName.Visible = True
Me.subformName.SetRecordSource temporary_table_name 'without the dbo. part of the name

How to set date format for the result of a SELECT statement in SQL Server

I know how to use CONVERT function in SELECT statement to change the format of the Date column:
SELECT
StationID
, CONVERT(varchar, [Date], 101) as Date
, Value
FROM my_table
But I was wondering if I can set the date format in general before running the SELECT statement, when I don't know the name of the date column in the following SELECT statement:
SELECT * from FROM my_table
Is any SET statement or other T-SQL that I can run before my SELECT statement so that I can change the Date format temporarily?
Thank you
No.
In particular, any date columns which you select are not actually formatted at all, but are instead returned down the wire as an actual piece of date data in a binary "format" which is used for dates. If you are seeing them formatted, it's because your client (either management studio or some other tool) is converting them to strings to display.
When you use SELECT *, there is obviously no way to tell SQL Server to do any conversions on any particular columns, so the data is going to be returned in whatever the data types of the underlying query returns. So regardless of whether your data types are really date or not, no manipulation is going to happen at that point anyway.
I'm pretty sure there's no way to do what you're asking. However, there are ways to format the date string when you output it using your programming language.

Resources