I have a question which I'm not sure if possible or not but let's say I have a table with Employeeid, Date and AttendanceStatus i.e "Present"/"Absent".
I can get the status of all employees for all dates or for dates I specify in the query.
My question is that is it possible to get today's date by default so whenever I run the query it give me the data rows for today's instead of all the records from the database. I want to use default so that I don't have to change the date everyday.
Thanks in advance
Try this:
select * from TABLE_NAME where
where cast([Date] as date) = cast(getdate() as date)
Also, as already mentioned, you could create a view:
create view V_TABLE_NAME as
select * from TABLE_NAME where
where cast([Date] as date) = cast(getdate() as date)
You can apply where clause for current day;
select * from table where
Date > cast(getdate() as date) and
Date < DATEADD(day,1,cast(getdate() as date))
Or you can create a view;
create view v_table
as
select * from table where
Date > cast(getdate() as date) and
Date < dateadd(day,1,cast(getdate() as date))
Then query;
select * from v_table
Try This Logic
DECLARE #MyDate DATE
SELECT
*
FROM YourTable
WHERE DateField = ISNULL(#MyDate,GETDATE())
Here, If You are not passing any values in the Parameter #MyDate, it will take Current Date as Default. The following can also be used
DECLARE #MyDate DATE
SELECT
*
FROM YourTable
WHERE
DateField = CASE WHEN ISDATE(#MyDate) = 1 THEN #MyDate ELSE GETDATE() END
Related
I've got a database that needs to display records between 2 dates.
At the moment I've got a form with 2 text boxes on - one for datefrom and one for dateto.
My plan was to format and store the values from these text boxes as global variables
DateFrom = Format(Me.DateFrom, "yyyy/mm/dd")
DateTo = Format(Me.DateTo, "yyyy/mm/dd")
that I cam then pass into a passthrough query with this criteria for field called dateCollected
HAVING (((Collections.DateCollected) str_to_date(datefrom, ā%y/%m/%dā) and str_to_date(dateto, ā%y/%m/%dā)) AND ((dbo.udfRawHazPropsList([ConsignmentNoteNumber],[ewc_code])) Is Not Null))
However, no matter what I try I can't get this to work. Any ideas as to a solution would be much appreciated!
You can get it by two ways:
1. If your sp variable of date type-
DECLARE #StartDate DATE = '2020-07-23',
#EndDate DATE = '2020-07-27'
SELECT * from TableName
WHERE CreatedON between #StartDate and #EndDate --First Way
SELECT * from TableName
WHERE CreatedON >= #StartDate and CreatedON <#EndDate --Second Way
SELECT * from TableName
WHERE CreatedON between CONVERT(varchar,#StartDate,6)
and CONVERT(varchar,#EndDate,6) --Third Way
2. If your sp have Varchar Variable type-
DECLARE #StartDate1 VARCHAR(50) = '2020-07-23',
#EndDate1 VARCHAR(50) = '2020-07-27'
SELECT * FROM TableName
WHERE CreatedON between CAST(#StartDate1 AS DATE)
and CAST(#EndDate1 AS DATE) --First Way
SELECT * from TableName
WHERE CreatedON >= CAST(#StartDate1 AS DATE)
and CreatedON < CAST(#EndDate1 AS DATE) --Second Way
SELECT * from TableName
WHERE CreatedON between CONVERT(varchar,Cast(#StartDate1 AS date),6)
and CONVERT(varchar,Cast(#EndDate1 AS date),6) --Third Way
In Third way in both, you can change the value to get the different format.
CONVERT(varchar,Cast(#EndDate1 AS date),6)
Change Third Value to get different format if you need.
How can I query a DateTime field only based on the date ignoring the time in the field?
select * from TABLE_NAME
where DATE = '2020-01-08'
You can achieve by this way, demo in db<>fiddle
select * from TABLE_NAME
where CAST([DATE] AS DATE) = CAST('2020-01-08' AS DATE)
You can do date comparison:
mydatetime >= cast('2020-01-08' as date) and mydatetime < cast('2020-01-09' as date)
Or you can cast() your datetime to a date (which is shorter to write, but far less efficient, since it cannot take advantage of an index on the datetime column):
cast(mydatetime as date) = cast('2020-01-08' as date)
Well this is my case: I have an input date X (dd-mm-yyyy), and I want to count the number of days between it with the year part is changed into current year and today's date in SQL. I t comes with the following condition, after the year is changed temporarily: (Here's my current idea of the logic)
- If date X is earlier than today, then difference = datediff(X,now), with the X year is current year
- If date X is later than today, then difference = datediff(X,now), with the X year is one year before
Sample case:
1st case: The input date is 6-6-1990. Today (automatically generated) is 22-8-2011. Then the difference will be = datediff(6-6-2011,22-08-2011)
2nd case: The input date is 10-10-1990. Today (automatically generated) is 22-8-2011. Then the difference will be = datediff(10-10-2010,22-08-2011)
Any idea how to do this in SQL (in SQL Server)? Or is there any other more simple alternatives for this problem? I'd also like this to be done in the query and not using a stored procedure or function
Sorry if there's already a similar question, I just don't know the exact keyword for this problem :( if there's a question like this previously, feel free to direct me there.
Thanks in advance
Here is the implementation (if I understood the logic you need correctly):
USE YourDbName
GO
CREATE FUNCTION YearPartDiff (#date datetime)
RETURNS int
AS
BEGIN
DECLARE #dateCurrentYear datetime
SET #dateCurrentYear = DATEADD(year, YEAR(GETDATE()) - YEAR(#date), #date)
DECLARE #result int
IF #dateCurrentYear < GETDATE()
SET #result = ABS(DATEDIFF(day, #dateCurrentYear, GETDATE()))
ELSE
SET #result = ABS(DATEDIFF(day, DATEADD(year, -1, #dateCurrentYear), GETDATE()))
RETURN(#result)
END
GO
And the example of usage:
USE YourDbName
GO
DECLARE #someDate datetime
SET #someDate = '2011-06-06'
SELECT dbo.YearPartDiff(#someDate) /*returns 77*/
SET #someDate = '2010-10-10'
SELECT dbo.YearPartDiff(#someDate) /*returns 316*/
Basically, #Andrei's solution, but in a single statement:
SELECT
DayDiff = DATEDIFF(
DAY,
DATEADD(YEAR, CASE WHEN LastOcc > GETDATE() THEN -1 ELSE 0 END, LastOcc),
GETDATE()
)
FROM (
SELECT LastOcc = DATEADD(YEAR, YEAR(GETDATE()) - YEAR(#InputDate), #InputDate)
) s
This seems to do the job
SELECT DATEDIFF(DAY, CONVERT(DATETIME, N'2011-06-06'), CONVERT(DATETIME, N'2011-08-22'))
So the basic syntax is
SELECT DATEDIFF(DAY, CONVERT(DATETIME, N'yyyy-mm-dd'), CONVERT(DATETIME, N'yyyy-mm-dd '))
Alternatively, you can use GETDATE() instead of the string for today's date
I have used "SELECT DATEDIFF( D, "+myDate+", GETDATE())" in my code, on SQL Server 2005. It works for me. The value myDate of course would be the DateTime input value.
you should try this query:
create table #T (inp_date datetime)
insert #T values ('06-06-1990')
insert #T values ('08-22-1990')
insert #T values ('10-10-1990')
--select * from #T
select inp_date, GETDATE(),
CASE
WHEN DATEADD(yy,DATEDIFF(yy,inp_date,GETDATE()),inp_date) <= GETDATE()
THEN DATEDIFF(dd,DATEADD(yy,DATEDIFF(yy,inp_date,GETDATE()),inp_date),GETDATE())
ELSE DATEDIFF(dd,DATEADD(yy,DATEDIFF(yy,inp_date,GETDATE())-1,inp_date),GETDATE())
END
from #T
Some of the dates in a column of dates were recorded wrong. I'd like to make a query which subtracts one day from each date IF the days are in a certain date range.
I know I'll have to use DATEADD and UPDATE, but I can't seem to figure it out. Thanks in advance.
This should do it:
UPDATE [SomeTable]
SET [DateColumn] = DATEADD(d, -1, [DateColumn])
WHERE [DateColumn] BETWEEN [Date1] AND [Date2]
Here's the MSDN doc's on the DATEADD function: http://msdn.microsoft.com/en-us/library/ms186819.aspx
When performing updates on data like this, it's always best to run a select statement first with the same criteria to ensure that you're updating the correct records. It also helps reduce the stress level of updating (especially if you're unfamiliar with SQL).
SELECT *, --Depending on what columns you would like to see, the wildcard could be replaced
DATEADD(d, -1, [DateColumn]) AS ProposedDate
FROM [SomeTable]
WHERE [DateColumn] BETWEEN [Date1] AND [Date2]
DECLARE #min_date datetime, #max_date datetime;
UPDATE yourtable
SET date_column = DATEADD(day, -1, date_column)
WHERE id IN (
SELECT id
FROM yourtable
WHERE date_column BETWEEN #min_date AND #max_date
);
You'll have to set appropriate values for #min_date and #max_date.
UPDATE MyTable
SET DateField = DATEADD(Day, -1, DateField)
WHERE Datefield BETWEEN '1/1/2011' AND '2/1/2011'
use UPDATE
set the data = ( do the date math )
WHERE the date is in the range you want
and the key is the row you want.
How would I show something in SQL where the date is greater than the current date?
I want to pull out data that shows everything greater from today (now) for the next coming 90 days.
I was thinking =< {fn NOW()} but that doesnt seem to work in my sql view here.
How can this be done?
SELECT *
FROM MyTable
WHERE CreatedDate >= getdate()
AND CreatedDate <= dateadd(day, 90, getdate())
http://msdn.microsoft.com/en-us/library/ms186819.aspx
Assuming you have a field for DateTime, you could have your query look like this:
SELECT *
FROM TABLE
WHERE DateTime > (GetDate() + 90)
In sql server, you can do
SELECT *
FROM table t
WHERE t.date > DATEADD(dd,90,now())
For those that want a nice conditional:
DECLARE #MyDate DATETIME = 'some date in future' --example DateAdd(day,5,GetDate())
IF #MyDate < DATEADD(DAY,1,GETDATE())
BEGIN
PRINT 'Date NOT greater than today...'
END
ELSE
BEGIN
PRINT 'Date greater than today...'
END
For SQL Server
select *
from YourTable
where DateCol between getdate() and dateadd(d, 90, getdate())
Select * from table where date > 'Today's date(mm/dd/yyyy)'
You can also add time in the single quotes(00:00:00AM)
For example:
Select * from Receipts where Sales_date > '08/28/2014 11:59:59PM'
If you're using SQL Server it would be something like this:
DATEDIFF(d,GETDATE(),FUTUREDATE) BETWEEN 0 AND 90