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.
Related
I'm trying to compare two dates fields while i'm so formatting it but seems not to work. the select statement works but i'm having problem with the where clause
SELECT fname
,lname
,email
,rdate_up = CONVERT(VARCHAR(19), GETDATE())
,last_time = CONVERT(VARCHAR(19), GETDATE())
FROM my_account
WHERE my_account.email = MMColParam
AND rdate_up = CONVERT(VARCHAR(19), GETDATE()) < last_time_log = CONVERT(VARCHAR(19), GETDATE())
I'm guessing this isn't your real query as comparing GETDATE() to itself will always be equal.
If you want to convert dates in the SELECT that's fine, I'm guessing you meant this:
SELECT fname
,lname
,email
,CONVERT(VARCHAR(19), rdate_up)
,CONVERT(VARCHAR(19), last_time)
FROM my_account
But use the actual date values in your WHERE clause and get rid of the conversion:
WHERE my_account.email = MMColParam
AND rdate_up < last_time_log
Do not compare dates (or anything, for that matter,) after you have converted them to strings.
Compare them before converting to strings.
Convert something to a string only immediately prior to displaying it to a human being.
SELECT
date1,
date2 = GETDATE(),
date1toShowToAHumanBeing = CONVERT(VARCHAR(19), date1),
date2toShowToAHumanBeing = CONVERT(VARCHAR(19), date2)
FROM my_account
WHERE date1 < date2
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 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 have a web portal that should display only time if the date is today's date otherwise it should display date .
dossier_date_created | Expected result
----------------------------------------------------
2013-10-22 16:18:46.610 | 2013-10-22
2013-10-23 11:26:56.390 | 11:26
I tried something like this :
select
case
when CONVERT(date,dossier_date_created) = CONVERT(DATE,getdate()) then convert(char(5), dossier_date_created, 108)
else convert(date, dossier_date_created)
end as timedate
from Proj_Manage_Dossier
But the result was :
timedate
-----------
2013-10-22
1900-01-01
How can I do it only with SQL? And btw Datatype of my column "dossier_date_created" is datetime
You can just use CAST or CONVERT to get your values to DATE or TIME datatypes. But, since you can't combine two data types in same column you have to cast them both to something identical - like NVARCHAR afterwards:
SELECT CASE WHEN CAST(dossier_date_created AS DATE) = CAST(GETDATE() AS DATE)
THEN CAST(CAST (dossier_date_created AS TIME) AS NVARCHAR(10))
ELSE CAST(CAST(dossier_date_created AS DATE) AS NVARCHAR(10)) END
FROM dbo.Proj_Manage_Dossier
SQLFiddle DEMO
EDIT - For SQL Server versions older then 2008, where there are no DATE and TIME datatypes - You can also directly CONVERT to VARCHAR using appropriate style codes.
SELECT CASE WHEN CONVERT(VARCHAR(10),dossier_date_created,102) = CONVERT(VARCHAR(12),GETDATE(),102)
THEN CONVERT(VARCHAR(10),dossier_date_created,108)
ELSE CONVERT(VARCHAR(10),dossier_date_created,102) END
FROM dbo.Proj_Manage_Dossier
SQLFiddle DEMO
This works fine for me:
select
case
when (CONVERT(date,PaymentDate) = CONVERT(DATE,getdate()))
then convert(VARCHAR(15), PaymentDate, 108)
else convert(varchar, PaymentDate, 101)
end as timedate
from Payments
Hope it will help you.. :)
This will work
select case when cast(dossier_date_created as date) = cast(getdate() as date)
then cast(cast(dossier_date_created as time) as varchar)
else cast(cast(dossier_date_created as date) as varchar)
end from #Proj_Manage_Dossier
Hope it helps :)
cheers......