Hi! I used bellow sql query it’s not worked. Can anybody help me?
DECLARE @ProductTable AS TABLE(Id INT IDENTITY(1,1),DateSaller DATE,Name VARCHAR(50))
INSERT INTO @ProductTable VALUES('11.09.2016','Apple')
INSERT INTO @ProductTable VALUES('12.08.2017','Orange')
INSERT INTO @ProductTable VALUES('02.07.2017','Cherry')
INSERT INTO @ProductTable VALUES('09.09.2015','Melon')
INSERT INTO @ProductTable VALUES('10.10.2017','Tomato')
INSERT INTO @ProductTable VALUES('10.09.2016','Potato')
INSERT INTO @ProductTable VALUES('09.09.2017','Cucumber')
INSERT INTO @ProductTable VALUES('2016-11-12','Onion')
DECLARE @DT DATE
SET @DT = (SELECT DateSaller FROM @ProductTable)
IF (DATEPART(YEAR,@DT)) < (DATEPART(YEAR,GETDATE()))
BEGIN
SELECT CONVERT(VARCHAR(25),DateSaller,104) AS PREDATE, Name AS PREPRODUCT, DateSaller, Name FROM @ProductTable
END
ELSE
BEGIN
SELECT '' AS PREDATE, '' AS PREPRODUCT, CONVERT(VARCHAR(25),DateSaller,104)DateSaller, Name FROM @ProductTable
END
Output result must be like bellow example:
Id
|
PreDate
|
PreProduct
|
DateSaller
|
Name
|
1
|
09.11.2016
|
Apple
|
|
|
2
|
|
|
08.12.2017
|
Orange
|
3
|
|
|
07.02.2017
|
Cherry
|
4
|
09.09.2015
|
Melon
|
|
|
5
|
|
|
10.10.2017
|
Tomato
|
6
|
09.10.2016
|
Potato
|
|
|
7
|
|
|
09.09.2017
|
Cucumber
|
8
|
12.11.2016
|
Onion
|
|
|