Getting all rows created today - sql-server

I am unable to get all the rows created today. I have used multiple functions like getdate(), Cast, Convert, etc. but all in vain.
This is my basic query:
SELECT timeId
FROM table_roaster_time_table
WHERE (user_id = #user_id) AND (DATEDIFF(d, date, GETDATE()) = 0)
I want to get the timeId from the table table_roaster_time_table where userid will be provided and the date is today.
How do I do this?

In order to keep any chance of using an index on the [date] column (even if one doesn't exist today, it may in the future), try:
AND [date] >= DATEADD(DAY, 0, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP))
AND [date] < DATEADD(DAY, 1, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP));
If you're using SQL Server 2008 or better, you can do something like this to shorten the code but still make use of an index on [date] if one exists:
AND CONVERT(DATE, [date]) = CONVERT(DATE, CURRENT_TIMESTAMP);
EDIT
Since you seem to be confused why 3/6/2012 is March 6th and not June 3rd, I might also suggest that instead of manually inserting ambiguous date literals like '3/6/2012' into the database, you make the column a default such as:
ALTER TABLE dbo.table_roaster_time_table
ALTER COLUMN [date] DATETIME NOT NULL;
ALTER TABLE dbo.table_roaster_time_table
ADD CONSTRAINT df_date DEFAULT (CURRENT_TIMESTAMP)
FOR [date];
If you're going to insert date literals then at least use a safe and unambiguous format, such as YYYYMMDD:
INSERT dbo.table_roaster_time_table([date]) VALUES('20120603');
Now there is no confusion.

Related

can we use if condition for compare 2 dates and display the row in sql server

I have 2 dates column, one is current date and the one is user define date, I have to display the specific row when the user define date is 2days greater than current date.
In the below code I try to display the row, when both dates are equal. But I don't know how display the row in sql.
I have following Columns,
Rid
DateTime (User define date and time)
Reminder
Description
CRDateTime (current date and time)
I have to set 3 condition.
if both dates are equal means that should be display,
if DateTime is 2 days before CRDate means I have to display that row,
if DateTime is 1 day before the CRdate means I have to display that row.
declare DateTime as datetime1;
select RId if CAST (DateTime as date) =CAST (#CRDateTime as date)
The IF...ELSE statement is a control-flow statement that allows you to execute or skip a statement block based on a specified condition.
For example:
IF Boolean_expression
BEGIN
-- Statement block executes when the Boolean expression is TRUE
END
ELSE
BEGIN
-- Statement block executes when the Boolean expression is FALSE
END
If you want to check something in your SQL Statements you should use the where clause.
It could be looking like:
SELECT * FROM `[YOUR_TABLE_NAME]` WHERE `DateTime` = `CRDateTime`
If you want to check the date in a 2 Day decade you can use the SQL DATEADD Function.
It lookse like:
SELECT DATEADD(day, +1, '2017/08/25') AS DateAdd;
Resault of this code is 2017/08/26
In your case it looks like this:
SELECT RId where DATEADD(day, +2, DateTime) = CRDateTime
--- EDIT SECTION: ---
EDIT 1:
All in one you can use this code here:
SELECT RId WHERE
`DateTime` = `CRDateTime` OR
DATEADD(day, +1, DateTime) = CRDateTime OR
DATEADD(day, +2, DateTime) = CRDateTime
As I understand you basically want to show all the records with in range of 2 days.
SELECT RId, DateTime, Reminder, Description, CRDateTime
WHERE CRDateTime BETWEEN DATETIME AND DATEADD(day, +2, DateTime)
Or you may try this
SELECT RId, DateTime, Reminder, Description, CRDateTime
WHERE DateTime BETWEEN DATEADD(day, -2, CRDateTime) AND CRDateTime
The question isn't clear. I assume the actual question is how to filter a table's rows between two dates.
Filtering in SQL (in any product) is the job of the WHERE clause, not IF. You can use the BETWEEN clause to select values in a range.
If both the table field and the query parameter are date variables, the query is easy :
CREATE TABLE table1
(
RId int PRIMARY KEY,
DateField date,
INDEX IX_Table1_Date (DateField)
)
declare #dateParam date='20190801';
SELECT RId
FROM table1
WHERE DateField BETWEEN dateadd(day,-2,#dateParam) AND #DateParam
This query will take advantage of the IX_Table1_Date index to speed up the search. Typically, applying any kind of function on a table field prevents the query engine from using any index that includes that field simply because the values stored in the index have no relation to the function's result.
If you use the date parameter to be the current date, just assign GETDATE() to it.
declare #dateParam date=GETDATE();
If the field isn't a date, you can cast it to date and still get a fast range query, because the query engine is fast enough to convert the cast to a range query.
SELECT RId
FROM table1
WHERE cast(DateField as date) BETWEEN dateadd(day,-2,#dateParam) AND #DateParam
If DateField is not a date-related type, eg it's a varchar, a) that's a serious bug and b) the server won't be able to use any indexes.
Casting the parameter values won't affect performance as they actual values are calculated before the query starts executing. The query becomes quite noisy though :
declare #dateParam datetime='20190801'
SELECT RId
FROM table1
WHERE cast(DateField as date)
BETWEEN dateadd(day,-2,cast(#dateParam as date)) AND cast(#DateParam as date)
That's why it's better to use the correct type for parameters

Change SQL Server date string ot other format

Please forgive me for my dunglish, I am dutch.
I have a table that looks like this:
CREATE TABLE [dbo].[WSLogons]
(
[Date] [nchar](15) NULL,
[Time] [nchar](15) NULL,
[Username] [nchar](15) NULL,
[Domain] [nchar](15) NULL,
[Computer] [nchar](15) NULL,
[HostComputerName] [nchar](15) NULL
) ON [PRIMARY]
There is no primary key.
I fill this table with some user data and I made a big mistake well multiple but I am facing the fact that my date and time columns are filled wrong.
This is have solved but in my database I have different date and time formats.
and I am querying with a convert date and time function.
This is not working because in some rows, I have date and time values like this:
5/18/2018 9:00 AM
5/18/2018 8:28 AM
and in some in the right way like this
18-05-2018 14:52
as I was saying, I query for information with a order by and a convert
order by CONVERT(date, Date, 105) desc
If in the results are date and time with the US notation, I get a format error.
Now I am as you can see not a SQL guru. I have corrected the script that is filling the table but now I want to convert the wrong dates into the right format so the data is saved.
I'd like to get a nice result in a query with this statement.
SELECT
CONVERT(date, [Date] ,101) as datum
,[Time]
,[Username]
,[Domain]
,[Computer]
,[HostComputerName]
FROM
[AuditLogons].[dbo].[WSLogons]
WHERE
date LIKE '%/%/%'
AND Time LIKE '%:% AM%' OR Time LIKE '%:% PM%'
Now the datum column is presented as
2018-05-18 11:59 AM
2018-05-18 1:25 PM
How do I get from here to update those rows in formatting the date like 18-5-2018?
If I try to convert to do
CONVERT(date, [Date], 105)
I get an error, too.
Hope you can understand my problem and I hope for some help
Many thanks.
Roger
This seems to work, but the underlying issue is that rule number one of database design is to use the appropriate data types. If you can change those columns, do it; if you can't, make sure you are validating your input.
DECLARE #WSLogons TABLE(
[Date] [nchar](15) NULL,
[Time] [nchar](15) NULL
)
INSERT INTO #WSLogons ([Date],[Time]) SELECT '5/18/2018','9:00 AM'
INSERT INTO #WSLogons ([Date],[Time]) SELECT '5/18/2018','09:00'
SELECT *
,CONVERT(datetime2, [Date] + ' ' + [Time]) as datum
FROM #WSLogons
first add real date column to your table:
alter table dbo.WSLogons add realDate datetime;
then update it with proper data ie:
update dbo.WSLogons
set realDate = CONVERT(datetime, Date, 101)
where Date like '[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]'
for date format in dd/mm/yyyy style
for other formats see here

How can I compare a DATE value with the result of GETDATE()

I am trying to grant and revoke server roles for user id picked from a table.
I am using the following query to insert a row from master table to another table whenever the expiry date approaches, however the command is not inserting any rows into the slave table.
Insert into tbl2(userid, role, startdate, expirydate)
Select userid, role, startdate, expirydate
from tbl1
where expirydate = Dateadd(day,0, getdate())
If I use <= or >= the above query is working but that is not helpful when we have multiple rows in tbl1.
It's because GETDATE() returns a DATETIME value, and you're likely comparing it to a DATE, so you're effectively comparing values like this:
SELECT GETDATE() AS DateTimeValue,
CAST(GETDATE() AS DATE) DateValue;
Output:
DateTimeValue DateValue
2017-10-04 10:34:35.023 2017-10-04
By default, a DATE will have a time set to midnight if comparing to a DATETIME, like: 2017-10-04 00:00:00.000.
These values aren't going to be equal with a time included, so use CAST or CONVERT to get a DATE without the time:
where expirydate = Dateadd(day,0, CAST(GETDATE() AS DATE))
Although, it looks like you don't need that Dateadd on the WHERE clause, so remove it unless this is edited / sample code. So maybe edit it to this:
where expirydate = CAST(GETDATE() AS DATE)
Reference
GETDATE (Transact-SQL)
Returns the current database system timestamp as a datetime value without the database time zone offset. This value is derived from the operating system of the computer on which the instance of SQL Server is running.

SQL Server Converting int to Date

I have a scenario where I have an int column with the following dates for example:
20131210
20131209
What I want is, I want to convert the above to a date datatype so that I can use it with GETDATE() function.
This is my try but I am getting an error:
SELECT CONVERT(DATETIME, CONVERT(varchar(8), MyColumnName))
FROM MyTable
WHERE DATEADD(day, -2, CONVERT(DATETIME, CONVERT(CHAR(8), MyColumnName))) < GetDate()
This is the error I am getting:
Conversion failed when converting date and/or time from character string.
You have at least one bad date in your column (it could be 99999999 or 20130231 or who knows). This is what happens when you choose the wrong data type. You can identify the bad row(s) using:
SELECT MyColumnName FROM dbo.MyTable
WHERE ISDATE(CONVERT(CHAR(8), MyColumnMame)) = 0;
Then you can fix them or delete them.
Once you do that, you should fix the table. There is absolutely no upside to storing dates as integers, and a whole lot of downsides.
Once you have the date being stored in the correct format, your query is much simpler. And I highly recommend applying functions etc. to the right-hand side of the predicate as opposed to applying it to the column (this kills SQL Server's ability to make efficient use of any index on that column, which you should have if this is a common query pattern).
SELECT MyColumnName
FROM dbo.MyTable
WHERE MyColumnName < DATEADD(DAY, 2, GETDATE());
Try:
CREATE TABLE IntsToDates(
Ints INT
)
INSERT INTO IntsToDates
VALUES (20131210)
, (20131209)
SELECT CAST(CAST(Ints AS VARCHAR(12)) AS DATE)
FROM IntsToDates
I've had a similar problem where I need to convert INT to DATE and needed to cater for values of 0. This case statement did the trick for me
CASE MySourceColumn
WHEN ISDATE(CONVERT(CHAR(8),MySourceColumn)) THEN CAST('19000101' AS DATE)
ELSE CAST(CAST(MySourceColumn AS CHAR) AS DATE)
END
AS MyTargetColumn

Ignoring the time element of a datetime field

I have a datetime field in my table. I want to delete records based on a date but I am not interested in the time element. How would I write the SQL for this ? I am using MS SQL 2008.
For best use of indexes, I'd go for this kind of approach:
To delete all records for 1st December:
DECLARE #DateToDelete DATETIME
SET #DateToDelete = '20091201'
DELETE FROM MyTable
WHERE MyDateField >= #DateToDelete AND MyDateField < DATEADD(dd, 1, #DateToDelete)
The alternatives include:
DELETE FROM MyTable
WHERE CAST(CONVERT(VARCHAR(10), MyDateField, 120) AS DATETIME) = #DateToDelete
which converts each datetime value to just it's date part.
But I'd still go with my original way as it allows for more efficient execution.
If you use MS SQL 2008 then you could convert to a new DATE type
DELETE FROM table WHERE date_filed >= CONVERT(DATE,GETDATE())
Is the time relevant in any other place? If not, then you should use a DATE column instead. If you cannot, then the best way to seek a date part of a datetime in a WHERE clause is to use a range:
... WHERE dateColumn BETWEEN '20091221' and '20091222';
Note that given the datetime accuracy of 3ms a datetime like 20091221 23:59:59.999 may be aproximated to 20091222 00:00:00.000 and this can sometime create problems.
There is a great collection of blog posts on the topic of datetime at T-SQL Tuesday #001 (Date/Time Tricks): The Roundup
Try this:-
declare #date datetime
set #date = '2006-11-09'
select #date, dateadd(ms, -1, DATEADD(dd,1,#date))
delete from login
where datecreated between #date AND dateadd(ms, -1, DATEADD(dd,1,#date))
This is what datediff is for:
delete from Table where datediff(day,'2009-12-09',date_filled) = 0

Resources