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.
Related
I have this sql select query:
declare #godina as integer = 2020
select
vtRokSi.KodSI,
vtRokSi.rKod,
vtRokSi.rDatum,
vtRokSi.rDatumZavr,
CONVERT(datetime, vtRokSi.rDatum) as docni
-- if CONVERT(datetime, vtRokSi.rDatum) > vtRokSi.rDatumZavr
-- select 'docni' as docni
-- else if CONVERT(datetime, vtRokSi.rDatum) <= vtRokSi.rDatumZavr
-- select 'ne docni' as docni
-- else if vtRokSi.rDatumZavr
-- select 'nema vneseno'
from GP.dbo.VT_ROK_SI as vtRokSi
where vtRokSi.GodGP = #godina
where you can see, that I am trying to compare two columns.
vtRokSi.rDatum is of type date and vtRokSi.rDatumZavr is of type datetime. I am converting vtRokSi.rDatum to datetime.
I want to print several values in the column 'docni', for example,
the string 'nema vneseno' if vtRokSi.rDatumZavr is null,
'docni' if vtRokSi.rDatum is bigger then vtRokSi.rDatumZavr,
'ne docni' if vtRokSi.rDatum is smaller or equal to vtRokSi.rDatumZavr.
How can I do that in the same select statement?
Or will I need something more powerful, i.e. separate the command into several other?
You can use a CASE expression.
...
CASE
WHEN vtroksi.rdatum > vtroksi.rdatumzavr THEN
'docni'
WHEN vtroksi.rdatum <= vtroksi.rdatumzavr THEN
'ne docni'
ELSE
'nema vneseno'
END docni
...
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.
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').
When I perform the following statement, the date formatting is lost in epc.LAST_LICPLT_REPRINT_DT:
,CASE When epc.LAST_LICPLT_REPRNT_DT is null Then ''
ELSE '' + epc.LAST_LICPLT_REPRNT_DT
END as LAST_LICPLT_REPRNT_DT
I've tried to use
cast(epc.LAST_LICPLT_REPRNT_DT as date)
and
cast(epc.LAST_LICPLT_REPRNT_DT as DATE FORMAT 'YYYYMMDD')
nether work.
SELECT COALESCE(CAST(epc.LAST_LICPLT_RPRNT_DT AS VARCHAR(10)), '') --Return Empty String If Date Null
, COALESCE(epc.LAST_LICPLT_RPRNT_DT, DATE '9999-12-31') -- Return High Date if Date is Null
FROM {MyDB}.{MyTable}
WHERE epc.LAST_LICPLT_RPRNT_DT IS NULL;
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?