0

I am trying to create a view on a table called petients in my database. The table has five columns. One of them is the column which I want to keep patient admitted date. It data type is datetime so I want to create a query that filters the data in this table based on current date. For example I want create a view that shows only details of petients who have been recorded on the current day.

Here is my code:

CREATE VIEW [dbo].[recent petients]
AS 
    SELECT petient_id, name, age, contact 
    FROM [petients] 
    WHERE [date] = 'date.Today'

I am getting an error saying that failed to convert date to string. Can you help me to solve it, or where is my code wrong?

3
  • Please, take care of your punctation for next time. I corrected it for today. Thanks. Commented Feb 22, 2021 at 10:17
  • Tag your question with the database you are using. Commented Feb 22, 2021 at 12:08
  • You say you want to create a view. Why? This is a trivial query to implement in your application and your application will likely need to query this table in many different ways. So do you intend to create views for every "search" situation? The more objects you add to the database the more work you create maintaining and testing them. I also seems schema issues waiting to be found - but that's a very different question. Commented Feb 22, 2021 at 14:09

2 Answers 2

1

Your code looks like SQL Server code. If so, I would recommend:

SELECT petient_id, name, age, contact
FROM [patients] 
WHERE [date] = CONVERT(date, GETDATE());

As a note: This version is much better than DATEDIFF() because it allows the use of an index on patient([date]).

If the "date" column has a time component, you can use:

WHERE CONVERT(date, [date]) = CONVERT(date, GETDATE())

Note that this is also index-safe in SQL Server.

Sign up to request clarification or add additional context in comments.

1 Comment

It is sargable but it may not always be a good idea. Certainly easier to understand than that datediff nonsense.
0

I'm assuming you are using Transact-SQL from Microsoft SQL Server, but you should specify the sql dialect you are using.

Since the datetime field type generally includes also a time, it is better to use the DATEDIFF function: https://learn.microsoft.com/it-it/sql/t-sql/functions/datediff-transact-sql?view=sql-server-ver15

In your case, to consider only the record where date=today, the difference in days must be zero:

--SQL QUERY
WHERE DATEDIFF(day, GETDATE(), [date]) = 0
  1. day identifies the element you want to consider the difference. A list of names or abbreviations can be found in the link
  2. GETDATE() returns now datetime
  3. 2nd and 3rd arguments are the dates you want to make the difference between

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.