Search code examples
sqlsql-serversql-server-cesql-server-ce-4

Use a manipulated date in an INSERT statement, in SQL Server Compact


I'm using SQL Server Compact.

To get a date without the time, I do this:

CAST(FLOOR(CAST(GETDATE() AS FLOAT)) AS DATETIME);

To insert a new record, where the date is the smallest from another table, I do this:

INSERT INTO [Product] ([OrderDate], [Name]) 
SELECT TOP(1) [OrderDate], 'Widget' FROM [OtherProduct];

How do I combine those two ideas? I want something like this, but it doesn't work:

INSERT INTO [Product] ([OrderDate], [Name]) 
SELECT CAST(FLOOR(CAST(TOP(1) [OrderDate] AS FLOAT)) AS DATETIME), 'Widget' FROM [OtherProduct];

The error:

There was an error parsing the query. [ Token in error = TOP ]

Solution

  • we have to specify TOP(1) on right place

    INSERT INTO [Product] ([OrderDate], [Name]) 
    SELECT TOP(1) CAST(FLOOR(CAST([OrderDate] AS FLOAT)) AS DATETIME), 'Widget' FROM [OtherProduct];