How to create variable in SQL syntax ?

I want to declare a variable like current_dt  for CAST(FLOOR(CAST(GETDATE() AS FLOAT))AS DATETIME)) and re-use it in place of CAST(&&&&&.) instead of calling that function all the time.

INSERT INTO Table1
SELECT     *
FROM         Table2
WHERE     System_date = CAST(FLOOR(CAST(GETDATE() AS FLOAT))AS DATETIME))
GO
INSERT INTO Table3
SELECT     *
FROM         Table4
WHERE     System_date = CAST(FLOOR(CAST(GETDATE() AS FLOAT))AS DATETIME))
GO
INSERT INTO Table4
SELECT     *
FROM         Table5
WHERE     System_date = CAST(FLOOR(CAST(GETDATE() AS FLOAT))AS DATETIME))
Syd2008Asked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
here we go (note: remove the GO statements is a must if you want to use a variable)
DECLARE @dt DATETIME
SET @dt = CAST(FLOOR(CAST(GETDATE() AS FLOAT))AS DATETIME)) 
INSERT INTO Table1
SELECT     *
FROM         Table2
WHERE     System_date = @dt 
INSERT INTO Table3
SELECT     *
FROM         Table4
WHERE     System_date = @dt 
INSERT INTO Table4
SELECT     *
FROM         Table5
WHERE     System_date = @dt

Open in new window

0
 
brejkConnect With a Mentor Commented:
Delete one closing parenthesis after DATETIME keyword.
0
 
Syd2008Author Commented:
angelIII:Thanks for your tip actually 'GO' was giving me a problem
brejk: good catch thanks
0
All Courses

From novice to tech pro — start learning today.