Wasi
Wasi

Reputation: 751

Need help to get records of current date?

I am trying to write Stored Procedure for getting records for current date, but I am not able to do so. I have created one for record display with latest first and trying to modify the same.

Here is the code:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[SP_SELECTTODAYRECORDS_Test]
AS
BEGIN 
    SELECT col_Source as 'country', 
    col_FirstName +' '+col_LastName as 'name',
    description as 'state' ,
    col_county
    from tbl_Info_Test, tbl_CountySite 
    where col_Pic is not null
    and col_Source = sourcecountry
    and ISDATE(col_BookDate) = 1
    order by CONVERT(datetime, col_BookDate) DESC
END

Upvotes: 0

Views: 2788

Answers (2)

Milind Thakkar
Milind Thakkar

Reputation: 980

If you are trying to get report for today's date, then you can use convert function with GetDate().

If you add following where condition in your SP, it will give records with col_BookDate of today's date.

CONVERT(VARCHAR(10),col_BookDate,101)=CONVERT(VARCHAR(10),GETDATE(),101)

Upvotes: 1

Yván Ecarri
Yván Ecarri

Reputation: 1739

Date fields combine Date and Time information. To select records for current date you have to ommit the time part. One approach is to remove the time part from the field. See the following link:

Most efficient way in SQL Server to get date from date+time?

Other approach is to select all records between current date start (at 00:00) and next day's start:

... WHERE DateField >= @Date AND DateField < DATEADD(d, 1, @Date)

Upvotes: 1

Related Questions