enter image description hereI am creating a query of two values, a period number and the corresponding date. The periods are accounting periods that are similar but not identical to our months. The dates are largely correct, but there are a few inconsistencies that I thought I would clean up with a WHERE statement.
For example, the date 4/1/2015 belongs in period 3, but it shows up in both period 3 and period 4. To fix this, I thought that I would create a WHERE statement stating:
SELECT DISTINCT table1.period,
table1.datetime
FROM table1
WHERE table1.period <> 4 AND table1.datetime <> '4/1/2015'
This took away all of period 4 away instead of just the one I needed. I have also tried the datetime syntax:
AND table1.datetime <> '20150401 00:00:00.000'
Both syntaxes had the same results. I am fairly inexperienced at SQL and have always hated dealing with datetime values, so any help would be great.
-EDIT- Forgot to add single quotations to the first datetime WHERE clause.
Try following:
SELECT DISTINCT table1.period,
table1.datetime
FROM table1
WHERE CASE WHEN table1.period = 4 AND CONVERT(VARCHAR(8),table1.datetime,112) = '20150401' THEN 1 ELSE 0 END = 0
This only get rid of rows that both has period=4 and datetime=20150401, while your query first get rid of anything that has period=4(no matter what [datetime] is), then get rid of anything has datetime=20150401.
Did you say that period is a string field ? you mean a varchar ?
Also you want to check on the date part only, not the time ?
then you can try this
SELECT DISTINCT table1.period,
table1.datetime
FROM table1
WHERE table1.period <> '4'
AND convert(date, table1.datetime) <> '20150401'
Related
How do I use GETDATE() in a WHERE clause using single quotes? I have the following script:
SELECT DISTINCT
pm_requisition.a_requisition
,pm_requisition.a_req_description
,pm_requisition.rq_position_loc
,pm_requisition.rq_status_of_emp
,pm_applicant_rec.a_applicant_no
,pm_applicant_rec.aq_status_of_appl
,pm_applicant_rec.aq_apply_date
FROM
pm_requisition
LEFT OUTER JOIN pm_applicant_rec
ON pm_requisition.a_requisition = pm_applicant_rec.a_requisition
WHERE
pm_requisition.rq_status_of_req NOT IN (N'6', N'7')
AND pm_applicant_rec.aq_status_of_appl = N'N'
AND rq_position_loc = '3300'
AND pm_applicant_rec.aq_apply_date = CAST(GETDATE()AS datetime2) --('2021-03-30 00:00:00.000')
The dataset does not yield any data using the GETDATE() clause, but if I use today's date (commented out) with single quotes I get the correct dataset. I obviously can't put single quotes around CAST(GETDATE()AS datetime2). Am I missing something simple?
The reason your query does not work is that GETDATE() returns the current date and time, not the current date. As a result, the last line will never match, as you are comparing a date only with a date and time.
Your solution CAST(GETDATE() AS DATE) works is because it removes the time, which means you are now comparing a date with a date.
If you simplify your SQL to something like this :-
SELECT TOP 1 aq_apply_date,
GETDATE() [GETDATE]
FROM pm_applicant_rec;
you will see a result like this, so two different values, and clear why they do not match :-
aq_apply_date GETDATE
----------------------- -----------------------
2021-04-04 00:00:00.000 2021-04-04 14:32:15.507
You might see something slightly different depending on the datatype used for aq_apply_date.
I've got a simple query which works as expected the first time.
However, when I re-run it, it returns the same result 504 times, instead of once.
Does anyone know why this would happen?
SELECT GETDATE(), CAST (GETDATE() AS DATE) AS Casted_Date
from Production.Product
The count of rows based on your count of rows in Production.Product table.
This query is not valid anymore to validate since you are casting Date datatype as Date again.
I have a view where I Select about 100 rows to allow users to easily query data. In this data, I have a field that is sometimes a date and sometimes text. A date or text depends on type. I cast to a date value like so.
SELECT Cast(Value as Date) as column
from Table
Where type = 1
When you then try to run a query against this column, you get a Conversion failed when converting date and/or time from character string. Here is the query.
SELECT Column
From View
WHERE Column BETWEEN '01/01/2015' AND '12/31/2015'
I have another field that is a date and if I replace it in this query, the query works. Likewise the data from the whole table will load. Any ideas are appreciated. Thanks
Basically, I need this Query to work and not give me the error described above.
select cast(value as date)
from Value
where type = 1
and cast(value as date) between '01/01/2015' and '12/31/2015'
My guess is that you have entries in that column that cannot be converted to datetime. You may be able to find them by running
Select * from table where isdate(value) = 0
I want to get all rows that have being added 'x' minutes before.
SELECT [PromoCodeID]
,[CustomerID]
,[DiscountAmount]
,[AddedBy]
,[AddedDate]
,[ModifiedBy]
,[ModifiedDate]
FROM [tbl_PromoCodesNewCustomer]
Where....
Eg: Records added 30min before from todays date time
NOTE: Record Added date is added in the field 'AddedDate' which has DATETIME datatype.
You can use this:
SELECT [PromoCodeID]
,[CustomerID]
,[DiscountAmount]
,[AddedBy]
,[AddedDate] AS added
,[ModifiedBy]
,[ModifiedDate]
FROM [tbl_PromoCodesNewCustomer]
WHERE DATEADD(minute,x,added) > GETDATE()
Where "x" in DATEADD is the number of minutes you want.
i suggest a light variation on FirstHorizon answer:
SELECT [PromoCodeID]
,[CustomerID]
,[DiscountAmount]
,[AddedBy]
,[AddedDate] AS added
,[ModifiedBy]
,[ModifiedDate]
FROM [tbl_PromoCodesNewCustomer]
WHERE AddedDate < DATEADD(minute * -1,x,getdate())
the change may look minor but depending on the number of involved rows, indexes and some other factor evaluated by the query optimizer this query may perform way better because there is no calc to make on the data.
here is an article that explain the reason (look at the second paragraph, 'Using Functions in Comparisons within the ON or WHERE Clause').
EDIT:
I'd advise you to populate the addedDate field with the UTC date and time, otherwise you could have problems with data managed among different servers \ time zones or on time change days.
So, if we consider this, the where clause will be:
WHERE DATEDIFF(mi,addedDate,GETUTCDATE)<30
This might sound kind of weird, but I have a query that joins two tables. I'm using an IF statements that dictates what to return. One path runs the query/join as is, the other needs to return all of the data from the first column, but only return column names with null values. Here's the query i have now:
declare #Date DATE = '06/07/2012'
IF #DATE >= GETDATE()
BEGIN
SELECT DisplayName, '' [RegularHours], ''[OvertimeHours]
FROM Sites
ORDER BY DisplayName
END
ELSE
SELECT sites.DisplayName, hrs.SiteDate, hrs.RegularHrs, hrs.OverTimeHrs
FROM Sites sites
left join SiteHours hrs on sites.SiteID = hrs.SiteID
ORDER BY DisplayName
What's making me nervous is that the second and third columns do not have values at all, not even NULL. I'm worried that this will pose a problem later. Any ideas?
If I understand the question correctly, I think you can do:
SELECT DisplayName, NULL as 'RegularHours', NULL as 'OvertimeHours'