Question: I have the following code that creates the `Checkin_day` variable from `CHECKIN_DATE_TIME`. I'd like to only select 'Sundays' from the newly created `Checkin_day` variable but I'm getting an invalid column name error (because it is not a variable within the dataset). What can I add to my code so that I can select only records that fall on a 'Sunday'? Is this a declare issue or do I need to sub-query? 

```
use EMTCQIData

DECLARE @StartDate Date
DECLARE @EndDate Date

Set @StartDate = '01/01/2018'
Set @EndDate = '12/31/2018'

SELECT *, 
Format([CHECKIN_DATE_TIME],'dddd') AS [Checkin_Day]
FROM ED_TAT_MASTER
WHERE (CHECKIN_DATE_TIME > @StartDate and CHECKIN_DATE_TIME < @EndDate) AND 
Checkin_Day = '*Sunday*'

```

Answer: 
Try querying the actual column, not the alias.

```
DECLARE @StartDate Date
DECLARE @EndDate Date

Set @StartDate = '01/01/2018'
Set @EndDate = '12/31/2018'

SELECT *, 
    FORMAT([CHECKIN_DATE_TIME], 'dddd') AS [Checkin_Day]
FROM 
    ED_TAT_MASTER
WHERE 
    (CHECKIN_DATE_TIME > @StartDate and CHECKIN_DATE_TIME < @EndDate) 
    AND FORMAT([CHECKIN_DATE_TIME], 'dddd') = 'Sunday' -- <- here

```

Also you can query by using DATEPART like `DATEPART(dw, [CHECKIN_DATE_TIME]) = 0`
<https://www.w3schools.com/sql/func_sqlserver_datepart.asp>
Add an AND to your WHERE clause to only get records that fall on Sunday.