Cast datetime as date still shows time - sql

I'm trying to cast my datetime as date to get rid of the time, but the query results still show the time. When I compare with what I see online, it looks correct.
How do I fix it?
..
, CAST("Process"."StartDate" AS date)
..
The result still shows like this:
4/14/2019 0:00
Update: I'm trying to use convert, as suggested below, but I'm getting Exception calling "fill" with "1" arguments.
There's probably something wrong with my syntax for the convert. Any ideas? All the online examples I see use datetime with convert. The complete query in powershell looks like this:
$SQLquery_Proc = #"
SELECT DISTINCT
Process.Process_ID
,"Process"."ProcessName"
,ProcessFacilities = STUFF(
(SELECT ',' + apf.FacCode FROM ProcessFacilities apf where apf.Process_ID = "Process"."Process_ID" FOR XML PATH ('')), 1, 1, ''
)
, "People"."Last_Name"
, "People"."First_Name"
, "People"."Middle_Initial"
, "People"."Degree"
, "Process"."P_ID"
, "People_Facilities_ALL"."FacCode" as "People_FacCode"
, "People_Facilities_ALL"."Current_status"
, "People_Facilities_ALL"."Status_category"
, convert("People_Facilities_ALL"."Status_from_date", 101) --issue?
, convert("People_Facilities_ALL"."Next_r_date", 101) --issue?
FROM
Process
left JOIN "DB"."dbo"."People" ON "Process"."PRACT_ID"="People"."P_ID"
left JOIN "DB"."dbo"."People_Facilities_ALL" ON "Process"."P_ID"="People_Facilities_ALL"."PRACT_ID"
ORDER BY
"People"."P_ID"
"#
Update2:
I figured out how to get convert to work. Thanks for the help!
, convert(VARCHAR(23),"People_Facilities_ALL"."Status_from_date", 101)

Related

How do I pass a parameter in Report Builder to Firebird database?

I'm looking at doing some report development for one of our Training softwares. I finally got some queries working in FB Maestro, as I'm only familiar with SQL and Oracle.
I have the following query that works and returns results, but when trying to set up a parameter for the display name, the query runs (at least it returns no errors) however the dataset does not return any data. Has anyone worked with these before?
Here's the query:
Select CertStatus, DisplayName, Count(CertStatus) From ( With cte as (Select * From (Select COURSEVERSIONSWITHAGGREGATES.CourseTitle, COURSEVERSIONSWITHAGGREGATES.CourseNumber, "MaxTrainedCompletionDate", "Course_ID", PersonnelView.DISPLAYNAME, COURSEVERSIONSWITHAGGREGATES.RecertificationValue, COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONUNIT_ID,
CASE
WHEN COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONUNIT_ID = 3 THEN DATEADD(year, 1*COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONVALUE, MaxTrainingView."MaxTrainedCompletionDate")
WHEN COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONUNIT_ID = 2 THEN DATEADD(month, 1*COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONVALUE, MaxTrainingView."MaxTrainedCompletionDate")
WHEN COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONUNIT_ID = 1 THEN DATEADD(week, 1*COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONVALUE, MaxTrainingView."MaxTrainedCompletionDate")
WHEN COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONUNIT_ID = 0 THEN DATEADD(day, 1*COURSEVERSIONSWITHAGGREGATES.RECERTIFICATIONVALUE, MaxTrainingView."MaxTrainedCompletionDate") END
AS ExpirationDate
From MAXTRAININGVIEW
INNER JOIN PERSONNELVIEW ON (MAXTRAININGVIEW."Personnel_ID" = PERSONNELVIEW.PERSONNELID) INNER JOIN COURSEVERSIONSWITHAGGREGATES ON (MAXTRAININGVIEW."Course_ID" = COURSEVERSIONSWITHAGGREGATES.COURSEID)
WHERE Personnelview.DisplayName = 'Aaron')) Select CourseTitle, CourseNumber, "MaxTrainedCompletionDate", "Course_ID", DisplayName, RecertificationValue, Recertificationunit_ID, ExpirationDate,
CASE WHEN ExpirationDate > current_date Then 'Active' WHEN ExpirationDate < current_date Then 'Expired' END As CertStatus from cte) Group By CertStatus, DisplayName
This returns values with the static value of 'Aaron' in report builder. But trying to use a parameter, it does not throw an error in report builder, however it just does not return any data.
For example this:
WHERE Personnelview.DisplayName = '#DisplayName'))
I've got the parameter based off another data set query, and that seems to work (it gives me the option to select employees)
Here is an example of it passing 'Aaron' (with personal info removed)
Example of passing #FName Parameter:
If you want to pass the parameter in report, other type database might not recognize query like "where [field] in #parameter", so I think you could try to use filter to achieve this goal(create a filter in dataset, and create a parameter, then specify it in filter properties).

Copying a query from SQL server to MS Acess

I've written a query in SQL server that runs okay.
I need to run this query in Access as there is more data I need to pull into the query.
However when I copy the SQL into Access it doesn't work, I get a syntax error, Missing operator in query expression.
SELECT main.Tbl_ServiceOrder.ServiceOrder
, main.Tbl_Serviceorder.BusinessPartnerNumber
, main.Tbl_ServiceOrder.ExternalPointofDeliverynumber
, main.Tbl_ServiceOrder.ServiceProduct
, main.Tbl_ServiceOrder.SOCreatedOn
, main.Tbl_ServiceOrder.MainUserStatus
, main.Tbl_ServiceOrder.MainUserStatusDesc
, main.Tbl_ServiceOrder.ReasonCode
, main.Tbl_ServiceOrder.ReasonCodeDesc
, main.Tbl_ServiceOrder.SOActualFinishDate
, main.Tbl_ServiceOrder.BasicStartDate
,CAST(CASE WHEN reasoncode <> 'CMPL' THEN 'Pre Install'
WHEN reasoncode = 'CMPL' and SOActualFinishDate < (getdate()-182) THEN 'SMART BAU'
ELSE 'Post Install' END as Varchar) as SMRTPot
FROM main.Tbl_ServiceOrder
WHERE main.Tbl_ServiceOrder.ServiceProduct ='SMINSTALL'
OR main.Tbl_ServiceOrder.ServiceProduct ='SMEXCHANGE'
The WHEN on the first line of the CAST is highlighted by access as the error point.
What am i doing wrong?
I've tried changing the cast to the IIF below
, IIF (reasoncode <> 'CMPL','Pre Install',(IIF SOActualFinishDate < (getdate()-182),'SMART BAU','Post Install')) as SMRTPot
Above gives syntax error (comma) in query expression
IIF (reasoncode <> 'CMPL','Pre Install',(IIF SOActualFinishDate < (getdate()-182),'SMART BAU','Post Install')) as SMRTPot
above gives syntax error in query expression.
Okay so this is what I'm using now:
SELECT main.Tbl_ServiceOrder.ServiceOrder
, main.Tbl_Serviceorder.BusinessPartnerNumber
, main.Tbl_ServiceOrder.ExternalPointofDeliverynumber
, main.Tbl_ServiceOrder.ServiceProduct
, main.Tbl_ServiceOrder.SOCreatedOn
, main.Tbl_ServiceOrder.MainUserStatus
, main.Tbl_ServiceOrder.MainUserStatusDesc
, main.Tbl_ServiceOrder.ReasonCode
, main.Tbl_ServiceOrder.ReasonCodeDesc
, main.Tbl_ServiceOrder.SOActualFinishDate
, main.Tbl_ServiceOrder.BasicStartDate
,Switch(reasoncode <> 'CMPL', 'Pre Install',
reasoncode = 'CMPL' AND SOActualFinishDate < ( Getdate() - 182 ), 'SMART BAU',
True, 'Post Install') as SMRTPot
FROM main.Tbl_ServiceOrder
WHERE main.Tbl_ServiceOrder.ServiceProduct ='SMINSTALL' OR main.Tbl_ServiceOrder.ServiceProduct ='SMEXCHANGE'
Error message is now could not find file 'file path\main.mdb
Too many issues and too long for a comment.
You are using SQL Server syntax and not MS Access SQL syntax.
WHEN...CASE is not valid in MS ACcess SQL, you should use IIF or SWITCH instead
CAST is also not valid. You can convert to string using CStr
GetDate is also not valid, you should use DATE().
If you want to substract 182 days then you should do DATEADD('d', DATE() , -182)
But the most handy way to handle this is probably to make passthrough query that will be executed on SQL Server and not MS Access (thus not on linked tables), so you can keep your SQL Server syntax. Search that path.

Append Query Trouble

I am having trouble with the final piece of my append query. I have the records generating just like I want with the exception of not triggering until the Expression Event Date is <=Date(). It is giving me a unmatched error when I place the <=Date() in the criteria field of the query builder. I tried it with DateSerial and a few other variations. I'm sure it has to do with the expression being that and not a hard date. Any assistance would be appreciated.
INSERT INTO SchedulingLog (
UserID
, LogDate
, EventDate
, Category
, CatDetail
, [Value]
)
SELECT Roster.UserID
, Date() AS LogDate
, DateSerial(Year(Date()),Month([WM DOH]),Day([WM DOH])) AS EventDate
, SchedulingLog.Category
, SchedulingLog.CatDetail
, Max(tblAccrual!WeeksAccrual*Roster!Schedule) AS [Value]
FROM tblAccrual
, [Schedule Type]
, Category
INNER JOIN CatDetail
ON Category.CategoryID = CatDetail.CategoryID
, SchedulingLog
INNER JOIN Roster
ON SchedulingLog.UserID = Roster.UserID
WHERE (((tblAccrual.Years)<=Round((Date()-[wm doh])/365,2)))
GROUP BY Roster.UserID
, Date()
, DateSerial(Year(Date()),Month([WM DOH]),Day([WM DOH]))
, SchedulingLog.Category
, SchedulingLog.CatDetail
HAVING (((SchedulingLog.Category) Like "Vac*")
AND ((SchedulingLog.CatDetail) Like "Ann*"));
I believe the issue is not explicitly converting the user input date with CDate. I suspect it's fine in most of the query because the [MW DOH] parameter is provided directly to functions which will convert it to date. However the WHERE clause would need an explicit conversion.
The following generates the error "This expression is typed incorrectly, or it is too complex to be evaluated. For example, a numeric expression may contain too many complicated elements. Try simplifying the expression by assigning parts of the expression to the variables."
SELECT Date()-[userinput] AS something;
Whereas the following code does not
SELECT Date()-CDate([userinput]) AS something;

Comparing Date Values in Access - Data Type Mismatch in Criteria Expression

i'm having an issue comparing a date in an access database. basically i'm parsing out a date from a text field, then trying to compare that date to another to only pull newer/older records.
so far i have everything working, but when i try to add the expression to the where clause, it's acting like it's not a date value.
here's the full SQL:
SELECT
Switch(Isdate(TRIM(LEFT(bc_testingtickets.notes, Instr(bc_testingtickets.notes, ' ')))) = false, 'NOT ASSIGNED!!!') AS [Assigned Status],
TRIM(LEFT(bc_testingtickets.notes, Instr(bc_testingtickets.notes, ' '))) AS [Last Updated Date],
bc_testingtickets.notes AS [Work Diary],
bc_testingtickets.ticket_id,
clients.client_code,
bc_profilemain.SYSTEM,
list_picklists.TEXT,
list_picklists_1.TEXT,
list_picklists_2.TEXT,
list_picklists_3.TEXT,
bc_testingtickets.createdate,
bc_testingtickets.completedate,
Datevalue(TRIM(LEFT([bc_TestingTickets].[notes], Instr([bc_TestingTickets].[notes], ' ')))) AS datetest
FROM list_picklists AS list_picklists_3
RIGHT JOIN (list_picklists AS list_picklists_2
RIGHT JOIN (list_picklists AS list_picklists_1
RIGHT JOIN (bc_profilemain
RIGHT JOIN (((bc_testingtickets
LEFT JOIN clients
ON
bc_testingtickets.broker = clients.client_id)
LEFT JOIN list_picklists
ON
bc_testingtickets.status = list_picklists.id)
LEFT JOIN bc_profile2ticketmapping
ON bc_testingtickets.ticket_id =
bc_profile2ticketmapping.ticket_id)
ON bc_profilemain.id =
bc_profile2ticketmapping.profile_id)
ON list_picklists_1.id = bc_testingtickets.purpose)
ON list_picklists_2.id = bc_profilemain.destination)
ON list_picklists_3.id = bc_profilemain.security_type
WHERE ( ( ( list_picklists.TEXT ) <> 'Passed'
AND ( list_picklists.TEXT ) <> 'Failed'
AND ( list_picklists.TEXT ) <> 'Rejected' )
AND ( ( bc_testingtickets.ticket_id ) <> 4386 ) )
GROUP BY bc_testingtickets.notes,
bc_testingtickets.ticket_id,
clients.client_code,
bc_profilemain.SYSTEM,
list_picklists.TEXT,
list_picklists_1.TEXT,
list_picklists_2.TEXT,
list_picklists_3.TEXT,
bc_testingtickets.createdate,
bc_testingtickets.completedate,
DateValue(TRIM(LEFT([bc_TestingTickets].[notes], Instr([bc_TestingTickets].[notes], ' '))))
ORDER BY Datevalue(TRIM(LEFT([bc_TestingTickets].[notes], Instr([bc_TestingTickets].[notes], ' '))));
the value i'm trying to compare against a various date is this:
DateValue(Trim(Left([bc_TestingTickets].[notes],InStr([bc_TestingTickets].[notes],' '))))
if i add a section to the where clause like below, i get the Data Type Mismatch error:
WHERE DateValue(Trim(Left([bc_TestingTickets].[notes],InStr([bc_TestingTickets].[notes],' ')))) > #4/1/2012#
i've even tried using the DateValue function around the manual date i'm testing with but i still get the mismatch error:
WHERE DateValue(Trim(Left([bc_TestingTickets].[notes],InStr([bc_TestingTickets].[notes],' ')))) > DateValue("4/1/2012")
any tips on how i can compare a date in this method? i can't change any fields in the database, ect, that's why i'm parsing the date in SQL and trying to manipulate it so i can run reports against it.
i've tried googling but nothing specifically talks about parsing a date from text and converting it to a date object. i think it may be a bug or the way the date is being returned from the left/trim functions. you can see i've added a column to the end of the SELECT statement called DateTest and it's obvious access is treating it like a date (when the query is run, it asks to sort by oldest to newest/newest to oldest instead of A-Z or Z-A), unlike the second column in the select.
thanks in advance for any tips/clues on how i can query based on the date.
edit:
i just tried the following statements in my where clause and still getting a mismatch:
CDate(Trim(Left([bc_TestingTickets].[notes],InStr([bc_TestingTickets].[notes],' ')))) > #4/1/2012#
CDate(Trim(Left([bc_TestingTickets].[notes],InStr([bc_TestingTickets].[notes],' ')))) >
CDate("4/1/2012") CDate(DateValue(Trim(Left([bc_TestingTickets].[notes],InStr([bc_TestingTickets].[‌​notes],' '))))) > #4/1/2012#
i tried with all the various combinations i could think of regarding putting CDate inside of DateValue, outside, ect. the CDate function does look like what i should be using though. not sure why it's still throwing the error.
here's a link to a screenshot showing the results of the query http://ramonecung.com/access.jpg. there's two screenshots in one image.
You reported you get Data Type Mismatch error with this WHERE clause.
WHERE DateValue(Trim(Left([bc_TestingTickets].[notes],
InStr([bc_TestingTickets].[notes],' ')))) > #4/1/2012#
That makes me wonder whether [bc_TestingTickets].[notes] can ever be Null, either because the table design allows Null for that field, or Nulls are prohibited by the design but are present in the query's set of candidate rows as the result of a LEFT or RIGHT JOIN.
If Nulls are present, your situation may be similar to this simple query which also triggers the data type mismatch error:
SELECT DateValue(Trim(Left(Null,InStr(Null,' '))));
If that proves to be the cause of your problem, you will have to design around it somehow. I can't offer a suggestion about how you should do that. Trying to analyze your query scared me away. :-(
It seems like you are having a problem with the type conversion. In this case, I believe that you are looking for the CDate function.
A problem might be the order of the date parts. A test in the Immediate window shows this
?cdate(#4/1/2012#)
01.04.2012
?cdate(#2012/1/4#)
04.01.2012
Write the dates backwards in the format yyyy/MM/dd and thus avoiding inadverted swapping of days and months!
DateValue("2012/1/4")
and
CDate(#2012/1/4#)

Using MySQL Cast to find Wordpress posts with a custom field content equivalent to Price

I am using custom fields in Wordpress to contain an 'RRP' currency value. This field is text based, but I want to be able to run queries where I can bring out posts which have an RRP in a specific range.
I've been looking into the MySQL CAST function and this seems to be the right thing, but I can't seem to get it working. Everything I use seems to just become 0.00. Can anyone point out where I am going wrong?
SELECT wpostmeta.meta_value, CAST( 'wpostmeta.meta_value' AS DECIMAL( 10, 2 ) ) , wposts . *
FROM tc_posts wposts, tc_postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'rrp'
AND CAST( 'wpostmeta.meta_value' AS DECIMAL( 10, 2 ) ) < 9.99
ORDER BY wposts.post_date DESC
I think I see your problem here. 'wpostmeta.meta_value' will give you 0 when casted, you should get rid of those quotes, since it's the value you want to cast from, not the name of the field:
SELECT wpostmeta.meta_value, CAST(wpostmeta.meta_value AS DECIMAL(10, 2)), wposts.*
FROM tc_posts wposts, tc_postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'rrp'
AND CAST(wpostmeta.meta_value AS DECIMAL(10, 2)) < 9.99
ORDER BY wposts.post_date DESC
Cheers.