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;
Related
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;
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 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.
I am running this query
select * from dbo.CHARGES m
LEFT JOIN Docs z ON z.DocId=m.DocId
AND CHARGE_DATE=CAST(z.DocDate + z.DocTime AS DATETIME)
And getting Conversion failed when converting date and/or time from character string, since some rows of DocDate, DocTime have null value
Here DocTime is Varchar(5)
How can i run this query by ignoring NULL or wrong values?
If your strings are in the format yyyy-MM-dd hh:mm then your convert expression will be something like:
SELECT CONVERT(DATETIME, ISNULL(DocDate, '1900-01-01') + ' ' + ISNULL(DocTime, '00:00'), 121)
However, it is probably adivisable to check that it actually is a date before you try to convert it:
SET DATEFORMAT YMD;
SELECT DocDate,
DocTime,
Formatted = CASE WHEN ISDATE(ISNULL(DocDate, '1900-01-01')
+ ' ' + ISNULL(DocTime, '00:00')) = 1
THEN CONVERT(DATETIME, ISNULL(DocDate, '1900-01-01')
+ ' ' + ISNULL(DocTime, '00:00'), 121)
ELSE NULL
END
FROM (VALUES
('2013-10-01', '17:30'),-- CORRECT FORMAT
('2013-10-01', NULL), -- NULL TIME
('2013-13-10', '17:30'), -- INVALID DATE
('2013-01-05', 'XX:30'), -- INVALID TIME
(NULL, '17:00') -- NULL DATE
) t (DocDate, DocTime);
Note, I have set the dateformat even though it is set within the convert, this is for the benefit of ISDATE(), if the date format is not set this way, it may think that 2013-13-10 is a valid date (13th October 2013), but will through an error when it comes to the convert.
If/When you upgrade to SQL-Server 2012 you can simply use TRY_CONVERT:
SET DATEFORMAT YMD;
SELECT DocDate,
DocTime,
Formatted = TRY_CONVERT(DATETIME, ISNULL(DocDate, '1900-01-01')
+ ' ' + ISNULL(DocTime, '00:00'), 121)
FROM (VALUES
('2013-10-01', '17:30'),-- CORRECT FORMAT
('2013-10-01', NULL), -- NULL TIME
('2013-13-10', '17:30'), -- INVALID DATE
('2013-01-05', 'XX:30'), -- INVALID TIME
(NULL, '17:00') -- NULL DATE
) t (DocDate, DocTime);
Examples on SQL Fiddle
I don't condone this approach, and (as I have in a comment) would strongly advise correcting the problem (which is storing data as the wrong type) rather than jumping through hoops to work around data errors.
Try this code, the query depend of your necessity.
With Null Values
select * from dbo.CHARGES m
LEFT JOIN Docs z
ON z.DocId=m.DocId
AND (
z.DocDate IS NULL
OR z.DocTime IS NULL
OR CHARGE_DATE=CAST(z.DocDate + z.DocTime AS DATETIME)
)
Without Null Values
select * from dbo.CHARGES m
LEFT JOIN Docs z
ON z.DocId=m.DocId
AND (
NOT z.DocDate IS NULL
AND NOT z.DocTime IS NULL
AND CHARGE_DATE=CAST(z.DocDate + z.DocTime AS DATETIME)
)
I am trying to change the date format of a column, the column is set as varchar column name date time. The problem is that i cannot actually change the data type because the data is automatically inputted by a PLC on the automation side. I need the date in a date or numeric value because when i run my queries i need to give the system a date range. I am trying to use substrings to work around this issue but am getting an error saying that the data type is out of range. here is the syntax of my query.
select cast(
(substring(datetime, 1, 4) + '-' +
SUBSTRING(DateTime, 5, 2) + '-' +
SUBSTRING(DateTime, 7, 2) + ' ' + '00:00:00.000') as dateTime) as "Date"
, ID1
, ID2
, diameter
, WeightTheoretical
, WeightActual
, StockType
from table1
where datetime is not null
and datetime <> ''
and datetime <> '0'
order by "Date", ID1;
Edit- the date format is as such 20120622:00:00:00:000
Assuming your date is with the format yyyymmdd, you can convert the varchar to datetime like this:
select convert(datetime, columname, 112)
It looks from your SQL that your date string is of the format YYYYMMDD
This should convert fine using either the CAST or CONVERT functions:
eg
SELECT CONVERT(datetime,'20120601')
SELECT CAST('20120601' as datetime)
both return the expected value as a datetime.
EDIT: Based on the supplied format you specified, I'd use the SubString to chop the supplied data down a bit:
eg
SELECT CONVERT(datetime,SUBSTRING('20120601',1,8))
Based on the format of your data in the table (20120622:00:00:00:000) you can do the following:
declare #date varchar(50)
set #date = '20120622:00:00:00:000'
select cast(left(#date, 8) as datetime)
or
select convert(datetime, left(#date, 8))
results:
2012-06-22 00:00:00.000