I know it may sound very small issue but need to resolve, following is my SQL query
SELECT
CASE
WHEN A.ReleaseDate = '1900-01-01' THEN ''
WHEN A.ReleaseDate IS NULL THEN ''
ELSE ReleaseDate
END AS ReleaseDate
FROM XYZ
I can see Releasedate is 1900-01-01 in my table but still case is using ELSE condition. I can't find out the reason.
A case expression returns a single type. I think yours will return a date. Hence you get a date and a blank string turns into some very old date.
If you want it to return a blank string, then you want a string back. So use explicit conversion:
SELECT (CASE WHEN A.ReleaseDate = '1900-01-01' THEN ''
WHEN A.ReleaseDate is null THEN ''
ELSE CONVERT(VARCHAR(10), ReleaseDate, 121)
END) as ReleaseDate
FROM XYZ;
You can cast your ReleaseDate as Varchar
SELECT (CASE WHEN ReleaseDate = '1900-01-01' THEN ''
WHEN ReleaseDate is null THEN ''
ELSE cast(ReleaseDate as varchar)
END) as ReleaseDate
FROM XYZ;
Related
I have the following query:
SELECT pics.e_firedate FROM et_pics
Result:
NULL
2014-12-01 00:00:00.000
2015-04-03 00:00:00.000
NULL
NULL
I want to replace NULL values to ''.
CASE
WHEN pics.e_firedate IS NULL THEN ''
ELSE pics.e_firedate
END
makes NULL transforming to 1900-01-01 00:00:00.000, which I've tried to cast and replace with no success also.
How can I achive my goal?
That's because when using CASE expressions, the return values must have the same data type. In case they have different data types, all values are converted to the type with a higher data type precedence.
And since DATETIME has a higher datatype than VARCHAR, '' gets converted to DATETIME:
SELECT CAST('' AS DATETIME)
The above will return 1900-01-01 00:00:00.000.
To achieve your desired result, you should CAST the result to VARCHAR
SELECT
CASE
WHEN e_firedate IS NULL THEN ''
ELSE CONVERT(VARCHAR(23), e_firedate, 121)
END
FROM et_pics
For date formats, read this.
Try:
select isNUll(CONVERT(VARCHAR, pics.e_firedate, 120), '') e_firedate
FROM et_pics
SELECT
ISNULL(CONVERT(varchar(10),pics.e_firedate,120),'') e_firedate
FROM et_pics
You have to cast/convert datetime field, not other value - for example:
case
when pics.e_firedate is null
then ''
else cast(pics.e_firedate as nvarchar(30))
end
Well, the column in your database seems to be a DATETIME column. So SQL Server converts '' to the default date value.
One solution might be to convert all the results to string, as in:
SELECT ISNULL(CONVERT(NVARCHAR, pics.e_firedate), '') AS e_firedateString
FROM et_pics
Might be more efficient? And might not.
SELECT CONVERT(VARCHAR(23), e_firedate, 121) FROM et_pics where pics.e_firedate is not null
union
SELECT '' FROM et_pics where pics.e_firedate is null
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.
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 have a feeling this is a fairly simple one. I need to edit a line in the SELECT statement that looks to see if there is a value of NULL in a field. If there is a NULL value, I need the new column (not named) to display a '0' for that row. Where the row has data I need to display '1' in that row. Is there a way to do this without greatly modifying the logic I have? (Using SQL Server Management Studio)
Here's the code:
SELECT DISTINCT t.Name,
CONVERT(VARCHAR(10), t.DischargeDateTime, 120) AS DischargeDate,
t.PatientPortalEnabled,
t.Allergy,
CONVERT(VARCHAR(10), t.AllergyUpdateTime, 120) AS AllergyUpdate,
/*This is where I would like to put the logic if possible*/ <> NULL,
t.ElapseTimeForAllergyUpdateInMinutes,
t.OldValue,
t.NewValue,
t.ElapseTimeForAllergyUpdateInHours,
t.DischargeDateTime
Try this:
CASE WHEN MyField IS NULL THEN 0 ELSE 1 END
Here it is in your code:
SELECT DISTINCT t.Name,
CONVERT(VARCHAR(10), t.DischargeDateTime, 120) AS DischargeDate,
t.PatientPortalEnabled,
t.Allergy,
CONVERT(VARCHAR(10), t.AllergyUpdateTime, 120) AS AllergyUpdate,
CASE WHEN t.MyField IS NULL THEN 0 ELSE 1 END,
t.ElapseTimeForAllergyUpdateInMinutes,
t.OldValue,
t.NewValue,
t.ElapseTimeForAllergyUpdateInHours,
t.DischargeDateTime
You can do this using a CASE statement.
SELECT DISTINCT t.Name,
CONVERT(VARCHAR(10), t.DischargeDateTime, 120) AS DischargeDate,
t.PatientPortalEnabled,
t.Allergy,
CONVERT(VARCHAR(10), t.AllergyUpdateTime, 120) AS AllergyUpdate,
/*This is where I would like to put the logic if possible*/
CASE
WHEN t.MyField IS NULL THEN 0
ELSE 1
END AS MyNewField,
t.ElapseTimeForAllergyUpdateInMinutes,
t.OldValue,
t.NewValue,
t.ElapseTimeForAllergyUpdateInHours,
t.DischargeDateTime
Using XOR bitwise operator:
declare #x int = null
select isnull((#x ^ #x),-1)+1
Pretty simple question that I can't quite find the answer for:
Is there a simple and safe† way to cast a varchar to datetime in SQL Server?
† i.e. gracefully handle non-datetime strings with a default datetime value
You can filter your rows using the isdate function. This query returns three rows without any conversion errors:
with v as (
select '20110714' value union all
select '2011-07-15' union all
select '3/22/2011' union all
select 'foo'
)
select cast(value as datetime)
from v
where isdate(value) = 1
Edit
When you want a default vaue (like the current date/time), you could do something like this:
select case when isdate(value) = 1 then cast(value as datetime) else getdate() end
From SQL Server Denali you can use TRY_CONVERT. A case expression is the only safe way in previous versions.
You could try
select cast ('28/08/2006 11:23:25' as datetime)
SELECT * FROM tab_val WHERE (CASE ISDATE(val) WHEN 1 THEN CAST(val As DateTime) ELSE NULL END)
More details in the manual
we can place a check that column have the valid datetime and if not then return nulls not genrating any error.
SELECT CASE ISDATE([YourDate]) WHEN 0
THEN CAST([YourDate] AS DATETIME)
ELSE CAST(NULL AS DATETIME) END AS [Your Date]
FROM [dbo].[yourtable]