0
SELECT 
    Lname + ' ' + Ename  as Name, 
    DateOfBirth,  
    DATEDIFF(hour,dateOfBirth,GETDATE())/8766 AS Age 
FROM EmpTBL 
WHERE 
    DATEADD( Year, DATEPART( Year, GETDATE()) - DATEPART( Year, DateOfBirth), DateOfBirth) 
    BETWEEN CONVERT( DATE, GETDATE()) AND CONVERT( DATE, GETDATE() + 30);

The above query is what i use when i was testing inside the SQL it seems to work but when i add it to my ASP.net project the result is different, i already add "Convert(date, getdate()) " How to select date without time in SQL

Output from my project

Output from my project

Output from my SQL

Output from my SQL

Community
  • 1
  • 1

5 Answers5

2

When we fetch date from database to asp.net it's in Date-Time format not in Date so you can convert it to custom date as you want:

<%# Convert.ToDateTime(Eval("DateOfBirth")).ToString("yyyy/MM/dd")%> 

You can use any Format specifier/Custom Date and Time Format String according to your requirement.

Rojalin Sahoo
  • 1,025
  • 1
  • 7
  • 18
1

change your query to

select CONVERT(VARCHAR(10),dateOfBirth,101) FROM EmpTBL  WHERE <>
Laxmikant
  • 588
  • 4
  • 11
0

You can use the CONVERT function of ASP.Net, you can convert into string then format it.

Some thing like adding .ToString("MM/dd/yyyy").

I Think this post can help you: String Format Date - C# or VB.NET

Community
  • 1
  • 1
japzdivino
  • 1,736
  • 3
  • 17
  • 25
0

.net has no such datatype for date only. Your simply getting the default time in your datetime object.

Sql has date, time and datetime concepts so Sql can just show the date.

You can ignore the time in code or choose not to show the time on the UI by formatting it.

Paul Spain
  • 517
  • 3
  • 15
0

Use the DataFormatString property in the GridView. If you set it to {0:d}, you should just get a short date format.

Nerdwood
  • 3,947
  • 1
  • 21
  • 20