From: Swati Shrivastava on
I am using this query to retrieve data. I want to select a record from
this mentioned table Tb_Giveaway with respect to current date. I am
not getting data while executing this qyery in sql server 2008.
Can anybody help?
select * from [Tb_Giveaway] where Dateforgiveaway=GETDATE()
From: Erland Sommarskog on
Swati Shrivastava (swatikhushbu24(a)gmail.com) writes:
> I am using this query to retrieve data. I want to select a record from
> this mentioned table Tb_Giveaway with respect to current date. I am
> not getting data while executing this qyery in sql server 2008.
> Can anybody help?
> select * from [Tb_Giveaway] where Dateforgiveaway=GETDATE()

If you do

SELECT getdate()

what you see? Do you expect that value to match any rows in your table?

To strip the time portion, use this:

convert(char(8), getdate(), 112)


--
Erland Sommarskog, SQL Server MVP, esquel(a)sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

From: Swati Shrivastava on
On Jul 31, 2:00 pm, Erland Sommarskog <esq...(a)sommarskog.se> wrote:
> Swati Shrivastava (swatikhushb...(a)gmail.com) writes:
> > I am using this query to retrieve data. I want to select a record from
> > this mentioned table Tb_Giveaway with respect to current date. I am
> > not getting data while executing this qyery in sql server 2008.
> > Can anybody help?
> > select * from [Tb_Giveaway] where Dateforgiveaway=GETDATE()
>
> If you do
>
>    SELECT getdate()
>
> what you see? Do you expect that value to match any rows in your table?
>
> To strip the time portion, use this:
>
>    convert(char(8), getdate(), 112)
>
> --
> Erland Sommarskog, SQL Server MVP, esq...(a)sommarskog.se
>
> Links for SQL Server Books Online:
> SQL 2008:http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
> SQL 2005:http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
> SQL 2000:http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx



I just need to select the row that matches with the current date only
not time.

From: Bob Barrows on
Swati Shrivastava wrote:
> On Jul 31, 2:00 pm, Erland Sommarskog <esq...(a)sommarskog.se> wrote:
>> Swati Shrivastava (swatikhushb...(a)gmail.com) writes:
>>> I am using this query to retrieve data. I want to select a record
>>> from this mentioned table Tb_Giveaway with respect to current date.
>>> I am
>>> not getting data while executing this qyery in sql server 2008.
>>> Can anybody help?
>>> select * from [Tb_Giveaway] where Dateforgiveaway=GETDATE()
>>
>> If you do
>>
>> SELECT getdate()
>>
>> what you see? Do you expect that value to match any rows in your
>> table?
>>
>> To strip the time portion, use this:
>>
>> convert(char(8), getdate(), 112)
>>
>
>
>
> I just need to select the row that matches with the current date only
> not time.
Next time you have a question, please provide us with sufficient information
to provide an answer. At the minimum, provide the datatypes of the columns
involved, and show us some sample data and intended results. You would have
received an immediate and appropriate answer if you had provided this
information.


If Dateforgiveaway is a datetime field, and if dates and times were entered,
so that the data looks like this:

2010-07-31 3:30:05
2010-07-31 7:54:36

Then you need to query for a range:
WHERE Dateforgiveaway >= convert(char(8), getdate(), 112) and
Dateforgiveaway < convert(char(8), dateadd(d,1,getdate()), 112)

If the data was entered without times, then Erland's answer should have
worked for you.


From: picayunish on
On 31-7-2010 14:22, Swati Shrivastava wrote:
> See i want to retrieve the data everytime by matching current date(not
> time) with one of the date column only . i used the Getdata() function
> in sql query server 2008 but still i am not getting any result.
> Could you tell me what query i should used to do that?

Instead of using GetData() use GetDate() in the where or in the having.

e.g.
select *
from tblOrders
where datepart(yyyy, tblOrders.DateRec) = datepart(yyyy, getdate())
and datepart(mm, tblOrders.DateRec) = datepart(mm, getdate())
and datepart(dd, tblOrders.DateRec) = datepart(dd, getdate())
--
Edwin van der Vaart
 |  Next  |  Last
Pages: 1 2
Prev: For XML Path / used with Pivot
Next: query