TSQL - Case Date Compare - sql-server-2005

I am trying to compare a date in SQL and create a new field to allow me to group the results by Todays date and other date(s). I am converting both dates to CHAR. However i am getting an expression error near = and ) - This is for an SSRS report.
There is already a fairly complex SQL statement and i am just trying to add this extra statement to help in grouping.
Here is an example.
Case
WHEN Convert(Char(8), FieldDate, 103) = Convert(Char(8), GetDate(), 103) Then tmpDate = '1'
ELSE tmpDate = '0'
END
This results in an incorrect sntax near '='
This results in an incorrect sntax near ')'
Thanks

Why not compare the dates directly?
CASE WHEN DATEDIFF(day, FieldDate, GETDATE()) = 0 THEN 1 ELSE 0 END AS tmpDate

Move the field to the front:
select tmpDate =
CASE
WHEN Convert(Char(8), FieldDate, 103) = Convert(Char(8), GetDate(), 103) Then '1'
ELSE '0' END

If your example is from your code (copy/paste), "Then tmpDate - '1'" won't work. You put a hyphen instead of an equal sign?

Related

How to query all rows that are due the next business day?

If it helps, I'm using BytePro which I believe is using T-SQL based on the statement they've generated.
I'm having an issue where when I try retrieving data, I need to switch based on the day of the week. (current day being today's date using GetDate()).
M-T statement:
convert(varchar(10), [Status.SchedFundingDate], 112) <= convert(varchar(10), getdate() + 1, 112)
F statement:
convert(varchar(10), [Status.SchedFundingDate], 112) <= convert(varchar(10), getdate() + 3, 112)
I'd like to combine the two to automate the switch but I get a problem with
CAST(CASE
WHEN DATENAME(DW, GETDATE()) = 'Friday'
AND [Status.SchedFundingDate] <= GETDATE() + 3
THEN 1
WHEN [Status.SchedFundingDate] <= GETDATE() + 1
THEN 1
ELSE 0
END AS BIT)
I get an error:
An expression of non-boolean type specified in a context where a condition is expected, near 'AND'
this is the right syntax :
CAST(CASE WHEN DATENAME(DW, GETDATE()) = 'Friday'
AND [Status].[SchedFundingDate] <= DATEADD(DAY, 3 , GETDATE())
THEN 1
WHEN [Status].[SchedFundingDate] <= DATEADD(DAY, 1 , GETDATE())
THEN 1
ELSE 0
END AS BIT)
db<>fiddle here

converting date format to eliminate time stamp

I have a table where the date and time stamp are logged together and I want to only show the date in a 10 character output. Also I want to return as blank some dates in the field that are 01/01/1800.
The current table format is 2013-06-28 00:00:00:000
I just want the date. I was using RTRIM function but it keeps erroring out.
Thanking you in advance
Try this:
SELECT CASE CONVERT(DATE, #MyValue) WHEN '01/01/1800' THEN NULL ELSE CONVERT(DATE, #MyValue) END
Or if you prefer:
DECLARE #DateOnlyValue DATE
SET #DateOnlyValue = CONVERT(DATE, #MyValue)
SELECT CASE #DateOnlyValue WHEN '01/01/1800' THEN NULL ELSE #DateOnlyValue END
The latter is just DRYer.
You can just do:
select (case when datecol > '1800-01-01' then convert(varchar(10), datecol, 121) end)
This will convert the value to a string of the form YYYY-MM-DD.
This uses NULL for "blank". If you want an empty string:
select (case when datecol > '1800-01-01'
then convert(varchar(10), datecol, 121)
else ''
end)
This also assumes that the values with '1800-01-01' don't have a time component. That seems reasonable for a default extreme value.

SQL replace using SELECT and invalid column

I have a simple table where the date column answer_6 (formatted as varchar(max) either has a valid date or string Now. I wanted to substitute the Now with the current date/time and then calculate the difference. Here is what I did:
select
CASE [answer_6]
WHEN 'Now' THEN CONVERT(varchar, GETDATE())
ELSE answer_6
END as x,
answer_6 from [tDataMult] where DATEDIFF(yy, GETDATE(), [x]) > 5
The system give me invalid column name 'x'. If I remove the DateDiff SELECT statement works fine.
You can't use aliases in the WHERE clause of the same level as it was defined as WHERE clause is evaluated before the SELECT clause. Try the following :
SELECT t.* FROM (<...>) t WHERE DATEDIFF(yy, GETDATE(), [t.x]) > 5
Instead <...> put your query without WHERE clause.
You can't refer to an alias you've just created in a WHERE clause. The easiest way to fix it would just be to turn your original query into a subquery:
SELECT *
FROM
(
select
CASE [answer_6]
WHEN 'Now' THEN CONVERT(varchar, GETDATE())
ELSE answer_6
END as x,
answer_6 from [tDataMult]
) AS qry
WHERE DATEDIFF(yy, GETDATE(), [x]) > 5
Or you could replicate the expression in your WHERE clause:
select
CASE [answer_6]
WHEN 'Now' THEN CONVERT(varchar, GETDATE())
ELSE answer_6
END as x,
answer_6 from [tDataMult]
WHERE DATEDIFF(yy, GETDATE(),
(CASE [answer_6]
WHEN 'Now' THEN CONVERT(varchar, GETDATE())
ELSE answer_6
END)
) > 5
Column X does not exist in tDataMult table because it is an alias.
Replace it with answer_6 in DateDiff function.
However, one of the possible value of answer_6 column is varchar format ('Now').
You need to have valid data format to use DATEDIFF function properly. (for example: 'yyyy-mm-dd').

SQL Numeric to Date (with an exception)

I have a SQL Query in which I'm querying specific data from a table with the dates stored in a Numeric Column. I'm using the below query to convert the Numeric date to an actual date, but I have instances where the Numeric Value is 0, meaning that i.e. the Start Date is October 31, 2013, and there is no End Date, meaning that 0 is an open record. Is there a way I can add an error-catch where if the value is 0, just return either a 0 or 'Open' and continue?
SELECT WMACCT# as 'Account #', CONVERT(Date, CONVERT(Varchar(8),OPENDT),104)
as 'Open Date',
CONVERT(Date, CONVERT(Varchar(8),CLDT),104)as 'Close Date'From
server.server
You can use a CASE WHEN to set the values you want when the date is 0 and do what you were doing for the remaining values:
SELECT WMACCT# AS 'Account #',
CASE
WHEN OPENDT = 0 THEN '2013-10-31'
ELSE CONVERT(DATE, CONVERT(VARCHAR(8), OPENDT), 104)
END AS 'Open Date',
CASE
WHEN DATE = 0 THEN NULL
ELSE CONVERT(DATE, CONVERT(VARCHAR(8), CLDT), 104)
END AS 'Close Date'
FROM SERVER.SERVER
No, there's no "error-catch".
You could use an expression to test for the zero value, and bypass the datatype conversion:
For example:
CASE WHEN OPENDT = 0 THEN NULL ELSE CONVERT(Date, CONVERT(Varchar(8),OPENDT),104) END
You could return NULL. To have the expression return a '0' or 'Open', that wouldn't match the datatype of DATE returned by the ELSE. A column in a resultset has to have a datatype, it can't return a DATE on some rows, and a VARCHAR on other rows.

CASE expressions on datetime columns

I'm trying to access a datetime column to find out whether the date is within a week from today, or overdue. Then write a new column's value to say Incoming, Overdue or Fine.
SELECT
CASE next_action_date
WHEN (BETWEEN GETDATE()+7 AND GETDATE()) THEN 'Incoming'
WHEN (< GETDATE()) THEN 'Overdue'
ELSE 'Fine'
END AS condition
FROM
tableName
This is what I've got so far, but as you can probably see by looking, it doesn't work at all:
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'BETWEEN'.
There are two syntaxes of the CASE expression - the so-called simple one that compares a single value against a list of other values, and a searched one with generic boolean conditions. You picked the simple case, but it does not have enough flexibility for what you need; you should switch to the searched syntax, like this:
SELECT
CASE
WHEN next_action_date BETWEEN GETDATE() AND GETDATE()+7 THEN 'Incoming'
WHEN next_action_date < GETDATE() THEN 'Overdue'
ELSE 'Fine'
END AS condition
FROM
tableName
Please try
select CASE
when next_action_date between GETDATE() and GETDATE()+7 then 'Incoming'
when next_action_date < GETDATE() THEN 'Overdue'
else 'fine' end as Condition
from(
select GETDATE()+6 next_action_date
)x
Try this one -
DECLARE #Date DATETIME
SELECT #Date = GETDATE()
SELECT
condition = CASE
WHEN t.next_action_date BETWEEN #Date AND DATEADD(DAY, 7, #Date) THEN 'Incoming'
WHEN t.next_action_date < #Date THEN 'Overdue'
ELSE 'Fine'
END
FROM dbo.tableName t
use DATEADD(Day, 7, GETDATE())
You should uses the other form of the case statement
SELECT
CASE
WHEN (next_action_date BETWEEN GETDATE()+7 AND GETDATE()) THEN 'Incoming'
WHEN (next_action_date < GETDATE()) THEN 'Overdue'
ELSE 'Fine'
END AS condition
FROM
tableName
http://www.devx.com/tips/Tip/15633