Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a variable date in SQL

Tags:

In SQL Server Management Studio, I am trying to reference a specific date and time, using a variable for the date, as shown below.

Declare @specified_date Date set @specified_date = '07-01-2013'  Select *  from etc Where CreatedDate > CONVERT(datetime, @specified_date & '00:00:00.000' ) 

This code is not working, and I am receiving this error:

The data types date and varchar are incompatible in the '&' operator.

The data being used has both a date and time code on it, and instead of changing multiple queries, I'd like to be able to just define the date once and have the variable carry it across. If anyone knows of a solution, that would be great.

like image 735
Dakota Avatar asked Jul 30 '13 20:07

Dakota


People also ask

How do I autofill a date in SQL?

You can use now() with default auto fill and current date and time for this. Later, you can extract the date part using date() function. Let us set the default value with some date.


1 Answers

Did you try this:

Declare @specified_date Date set @specified_date = '07-01-2013' Select * from etc Where CreatedDate > @specified_date 
like image 183
BWS Avatar answered Oct 11 '22 15:10

BWS