I have a calendar in an aspx page, if the user doesn't select a date it puts 0001/01/01 into the database. How can I specify another date, like put today's date instead if no date is chosen?
There are couple of ways. If it is the Calendar control, you can programatically set the selected date on Page_Load to Today's date. That way even if the user doesn't select a date, you get the default selected date.
Second option would be, before inserting to the database - you can check if the selected date is <=DateTime.MinDate, and set current date in the object.
You could replace the "0001/01/01" with convert(varchar(10),getdate(),101)
There are two ways:
1) If the date field is empty then assign the date field value as system.datetime.now
so that while you are passing it to the procedure you can insert the current date.
for example txtdate is field where date is assigned
if(txtdate.text == "")
{
txtdate.text = system.datetime.now;
}
pass this to the procedure.
2)in the database level,check whether the date is empty or not
If it is empty then assign date as getdate()
for example
create procedure test(#date varchar(50) = '')
as
begin
if(#date <> '')
begin
-- your code goes here
end
else if(#date = '')
begin
getdate() value to #date
end
Related
I am working on a website in asp.net. I am getting a date from a web page and then depending on the user input I want to get results from SQL Server database (using stored procedures).
Problem is that I am getting date only from UI in this format 2016-10-08 which is of type string. But in the database, I have a column which is of type datetime in this format 2016-10-08 17:38:00.000.
I am using this query to search but it does not work.
select *
from table
where acceptedDate like #sDate+ '%';
where sDate is input parameter for stored procedure. Please help. thanks
Don't pass dates as strings. Pass them as DateTime.
The .Net DateTime maps directly to SQL Server's DateTime. All you have to do is parse the string to a DateTime struct in your .Net code and pass it as a parameter to your stored procedure.
To search for a specific date and ignore the Time portion of the DateTime, better use >= and < in your sql:
select *
from table
where acceptedDate >= #Date
AND acceptedDate < DATEADD(DAY, 1, #Date);
If you only want compare with day level and ignoring the hours part, you can use DateDiff function.
Pass d or DAY to interval parameter of DateDiff
For example:
DECLARE #sDate VARCHAR(100)='2016-10-08'
IF ISDATE(#sDate)=1
BEGIN
select *
from table
where datediff(d,acceptedDate,#sDate)=0 --same day
END
ELSE
PRINT 'Invalid date format!'
I want a method to ask the user to change his password after having it for 90 days.
I have something like this
ALTER PROCEDURE [dbo].[spRecoverPassword]
(
#sUsername varchar(50),
#sPassword varchar(100),
#sPasswordSalt varchar (128)
)
AS
BEGIN
SET NOCOUNT ON;
if (exists (select 1
from USER
where Username = #sUsername))
begin
update USER
set Password_Salt = #sPasswordSalt,
Password = #sPassword,
where Username = #sUsername;
select 1;
end
else
begin
select -1;
end
END
Then I have a method to call this stored procedure, from ASP.NET MVC. And somehow I would like to check the date that the password was last changed and do so that if that date is 90 days higher than today it will redirect it to the recover pass again. How should I do this?
Thanks
Add a new column in users Table to save last modified date of password.
on Get method calculate the difference between Last modified date of password and today's date. and do what you want to do.
Very easy:
add a date field in USER table for example last_changed.
update last_changed with now() in the query
check days difference DATEDIFF(now(),last_changed)>90
I have a website project which selects dates from database. the database stores the date in this format "2013-01-02 00:00:00.000". but when I select the date, I want to use the format "11/06/2013" - "dd/mm/yyyy"
here is my select
-- I am trying to pass "dd/mm/yyyy"
select date from Currencies where date = '11/06/2013'
but this doesnt work. if I change my where clause as below, it works...
-- if I pass "mm/dd/yyyy" , it works
select date from Currencies where date = '06/11/2013'
but I must pass "dd/mm/yyyy", how can I do that?
Try this one
SELECT date FROM Currencies WHERE date = convert(datetime, '11/06/2013 00:00:00', 103)
You should avoid handling variable syntax inside your sql statements and use parameterized queries instead
check this out.
I am trying to check in a stored procedure a date in table if it is equal to Today's date.
Code is
DECLARE #p0 datetime
Set #p0 =GETDATE()
Select * from testtable
where dateCol=#p0
This doesn't work it just gives empty rows. How can I accomplish that? Thanks
If dateCol is just the date, not DATETIME, you can use:
SELECT *
FROM Table
WHERE dateCol = CAST(GETDATE() AS DATE)
You need:
SET #p0 = CONVERT(CHAR(10),GETDATE(),103)
GETDATE() returns the date and time. You probably have records with todays date but not at this exact time.
Also you don't really need to store it in #p0. You could use the expression directly in the WHERE clause.
I am doing a project in school, I have to create a website tool for salesmen to fill what they have done during the day, i.e. amount of quotes, quote sum, orders, order sum etc. I am using Visual Studio 2010, ASP.NET with C# with a SQL database.
I have to create a table with different columns, that I know how. But what I need is to have a column called Date and it has the datatype date. I need it to be filled automatically without having to input it manually. The same date that the new information was added. I have searched for solution in google and other places but I think I am searching with the wrong keywords, hopefully you can help me.
The format I wish for the date to be is DD-MM-YYYY
When you look for SQL default date on Google, the second result you get is this one.
In there, you have a default date example:
CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
OrderDate date DEFAULT GETDATE()
)
using the DEFAULT keyword.
Create a sql datetime column in the database, and specify a default value of GetDate() or GetUtcDate() depending on which you want. Format is irrelevant on the input side; you will have to use a formatter on the select side (or in your c# code).
You can set the default value for the column as current date time..
create table tblname (
fieldname datetime default getdate()
)
Also see this question
Add default value of datetime field in SQL Server to a timestamp
You can use one of this to insert in the table instead.
String s = System.DateTime.Now.ToString("dd.MM.yyyy");
DateTime now = System.DateTime.Now;
The second one would be your choice because the type specified in yur table is Date.
If don't want to be setting it from the app, specify which database you are using to get a specific answer.