I have a table named @table1 which have two field one LoginDate and another Name field.
| Date | Name |
| 01/10/2024 10:00:00 AM | Mr. Deb Kumar |
| 1/10/2024 12:30:00 PM | Mr. Srinibash |
I want to create a view @table2 like below;
| Greetings | Name |
| Good Morning | Mr. Deb Kumar |
| Good Afternoon | Mr. Srinibash |
I want if Date field's Time value is between 6:00:00 to 12:00:00 then Greetings will be Good Morning and if Time value is between 4:00:00 to 5:00:00 then Greetings will be Good Afternoon and so on. How can i do this?

Jignesh KumarRank #91 · 22.9K reputationPosted Oct 1, 2024, 10:09 AM EDT
Hello,
SELECT CAST(TourDate AS DATE) as date , EmployeeName AS Name, STRING_AGG(CONCAT( CASE WHEN CAST(DATEPART(HOUR, [TourDate]) AS INT) < 12 THEN 'Good Morning ' WHEN CAST(DATEPART(HOUR, [TourDate]) AS INT) BETWEEN 12 AND 17 THEN 'Good Afternoon ' ELSE 'Good Evening ' End + 'From ', DeparturePlace, ' To ', ArrivalPlace), ', ') WITHIN GROUP (ORDER BY TourDate) AS VisitedPlace FROM Tour GROUP BY CAST(TourDate AS DATE), EmployeeName;Amit MohantyRank #112 · 20K reputationPosted Oct 1, 2024, 8:33 AM EDT
Check this
SELECT CASE WHEN CAST(LoginDate AS TIME) BETWEEN '06:00:00' AND '12:00:00' THEN 'Good Morning' WHEN CAST(LoginDate AS TIME) BETWEEN '12:00:01' AND '16:00:00' THEN 'Good Afternoon' WHEN CAST(LoginDate AS TIME) BETWEEN '16:00:01' AND '20:00:00' THEN 'Good Evening' ELSE 'Good Night' END AS Greetings, Name FROM @table1;Jignesh KumarRank #91 · 22.9K reputationPosted Oct 1, 2024, 9:01 AM EDT
Hello,
You can use DatePart function to achieve this,
CREATE VIEW GreetingView AS SELECT CASE WHEN CAST(DATEPART(HOUR, [LoginDate]) AS INT) < 12 THEN 'Good Morning, ' + [Name] WHEN CAST(DATEPART(HOUR, [LoginDate]) AS INT) BETWEEN 12 AND 17 THEN 'Good Afternoon, ' + [Name] ELSE 'Good Evening, ' + [Name] END AS GreetingMessage FROM Table1;You can select from GreetingView,
SELECT * FROM GreetingView;Debashish Mahonta146 reputationPosted Oct 1, 2024, 10:42 PM EDT
@Jignesh Kumar sorry for late reply. Your above code is working fine. But I got one problem. I want to add 'And' before the last concate word and full stop symbol at the end.
Example: concate word1, word2, word3 and last word.
Jignesh KumarRank #91 · 22.9K reputationPosted Oct 1, 2024, 11:15 PM EDT
Yo need to do change in string aggrigate function.
Debashish Mahonta146 reputationPosted Oct 1, 2024, 9:39 AM EDT
@Amit Mohanty Thanks for quick reply.
I concated two column into one column which TourDate is same and GroupBy Date from below tableTour:
Code Used:
SELECT CAST(TourDate AS DATE) as date , EmployeeName AS Name, STRING_AGG(CONCAT('From ', DeparturePlace, ' To ', ArrivalPlace), ', ') WITHIN GROUP (ORDER BY TourDate) AS VisitedPlace FROM Tour GROUP BY CAST(TourDate AS DATE), EmployeeName;Result
I want to add 'At Morning', 'At Evening' etc text to start of VisitedPlace text like Below;
How can i do this?
Debashish Mahonta146 reputationPosted Oct 1, 2024, 11:59 PM EDT
@Jignesh Kumar How can i do that?