Table Name is tabelea columns are name,expdate. Both Column have not null contraints. Both column had character varying data type.
Values
name expdate
A '10-05-2015'
B ' '
Now i want to fetch the value which expdate is not empty then convert to date format otherwise so empty. So i tried like this
select name,case when replace(expdate,' ','') <> '' then
to_char(cast(expdate as date),'dd-MM-yyyy') else null end from tablea
But is not work its show error invalid input syntax for type date: "' '".
How to solve this?
i tried trim also.
Postgresql 9.3
You can combine that in a single call if you convert empty strings to a null value:
select name,
to_char(cast(nullif(trim(expdate), '') as date),'dd-MM-yyyy')
from tablea;
The cast relies on some implicit data type conversion. It would be better to use to_date() with an explicit format instead:
to_char(to_date(nullif(trim(expdate), ''), 'dd-mm-yyyy'),'dd-MM-yyyy')
SQLFiddle example: http://sqlfiddle.com/#!15/a9831/1
You can use the tilde to compare your string with the desired date pattern, before making it a date:
select
name,
case when expdate ~ '^[[:digit:]]{2}-[[:digit:]]{2}-[[:digit:]]{4}$' then
to_date(expdate, 'dd-MM-yyyy')
else
null
end as the_date
from tablea;
(Of course it is a bad idea to use a string data type to store dates in the first place.)
SQL fiddle: http://sqlfiddle.com/#!15/a66cf/2.
Related
Something like the example below:
SELECT SSN, Name, CONVERT(VARCHAR(50), Hours) ISNULL(Hours, '')
where I want to convert an int to varchar and at the same time set the null values as an empty string. How can I do to make this possible?
ISNULL(CONVERT(VARCHAR(50), Hours), '')
An alternative option if you are on 2012+ would be to use
SELECT CONCAT(Hours,'')
which has the same end result of converting to string and returning empty string instead of NULL.
Wrap your convert in the isnull;
SELECT
SSN
,Name
,ISNULL(CONVERT(VARCHAR(50), Hours), '')
This way it will allow for the nulls to be pulled on the conversion, you won't be able to do the isnull inside the convert as a zero length character won't be compatible with an int field (or other number only field).
Here is part of my query:
IsNull(CONVERT(date, V_CONSTAT_ACTUAL_DATES.ID50), '') AS 'actualFinish'
V_CONSTAT_ACTUAL_DATES.ID50 is a NULL and is a datetime column. So the results I get is 1900-01-01, what I am trying to do is return nothing just ' ' How would I accomplish this?
The mixing of types you have now is definitely a problem.
If you want to display an empty string for a null date you need convert the date into a short date time string not a date. You can use the convert function to do that. The last parameter accepts a style code so you can have it display just the date part.
COALESCE(CONVERT(varchar(8), V_CONSTAT_ACTUAL_DATES.ID50, 101), '') AS 'actualFinish'
This will display the date in mm/dd/yy format if the column is not null, or an empty string if it is.
To see more about date to string conversions go to https://msdn.microsoft.com/en-us/library/ms187928.aspx.
Rather than ISNULL or COALESCE try using a CASE statement -
SELECT CASE WHEN date IS NULL THEN '' ELSE CONVERT(date, V_CONSTAT_ACTUAL_DATES.ID50) END
Simple ISNULL Nested CAST function can remove 1900-1-1 value if data is NULL
ISNULL(CAST(CAST(<<DateColumn>> AS DATE) AS Varchar),' ') [<<Date Column Name>>]
I would like to check a date value in my SQL query. If a date is equal to a predefined date then do not print anything, ELSE print the existing date value.
How can I write it correctly in order to take the desired date value ?
I have the following query:
(SELECT (CASE
WHEN (PaymentsMade.PaymentDate = '09/09/1987') THEN ' '
ELSE PaymentsMade.PaymentDate
END)
) as dateOfPayment
When I run this query it works correctly when the date is equal to '09/09/1987' , whereas when the date is not equal to '09/09/1987' it prints '01/01/1900'.
How can I retrieve the dates values that are not equal to the predefined date '09/09/1987'?
Any advice would be appreciated.
Thanks
The CASE clause needs to return a consistently-typed value, so it is implicitly converting a space to a date (which is evaluated as 1 Jan 1900).
You have two choices:
select a null instead of a blank space.
explicitly cast the date in the else condition to a string.
Here's an (implicit) example of the former:
SELECT (CASE WHEN PaymentsMade.PaymentDate <> '09/09/1987'
THEN PaymentsMade.PaymentDate
END)
as dateOfPayment
Use NULL, not empty string
An empty string is cast to zero implicitly, which is '01/01/1900'
SELECT CAST('' AS datetime)
Using a CASE statement changes the value in that field, but doesn't change which rows are returned.
You appear to want to filter out rows, and if that is the case, use a WHERE clause...
SELECT
*
FROM
PaymentsMade
WHERE
PaymentDate <> '09/09/1987'
You could use NULLIF to replace a specific date with a NULL:
SELECT NULLIF(PaymentsMade.PaymentDate, '09/09/1987')
FROM ...
Don't just use an empty string, because it would be converted to the type of PaymentDate, which is probably a datetime, and an equivalent datetime for '' would be 1900-01-01 00:00:00.000.
I have a nvarchar(5) column of data that is formatted MMMDD (for example, OCT26). With my select statement, I'd like to convert it to a datetime data type with the current year, and then save that datetime value as an alias, say, UsefulDate. So something like 10-26-2012.
Something like: SELECT (whatever SQL gets the job done) AS UsefulDate
The exact formatting doesn't matter; I just need to be able to compare two dates together with greater than and less than operators. Also, sometimes the column will be blank. In that case, I'd like to set the alias to blank as well. Is this possible?
Thanks for your help!
You can convert varchar fields in format MMMDD to date with current year with :
select convert(datetime,'OCT26'+','+cast(year(getdate()) as varchar),107)
So your query would be something like :
select convert(datetime,case varcharDate when '' then null else varcharDate end +
','+cast(year(getdate()) as varchar),107) as UsefulDate
from table
select CASE WHEN ISDATE(mmmdd+' '+right(year(getdate()),4)) = 1
THEN CAST(mmmdd+' '+right(year(getdate()),4) as datetime)
END UsefulDate, *
from tbl
I have a column in my select statement that looks like this:
SELECT CASE WHEN fu.SentOutDate IS NULL THEN '' ELSE fu.SentOutDate END
This returns 1900-01-01 00:00:00.000 for the ones that would otherwise be NULL
I know this because when I put in just fu.SentOutDate it comes up as NULL
Why does this happen and how can I just get it to return a blank value?
Try converting the date to a string so it doesn't try to convert '' to a date:
(CASE WHEN fu.SentOutDate IS NULL THEN '' ELSE CONVERT(varchar,fu.SentOutDate) END)
It's casting your '' to a DATETIME, since your other column you'd return is a datetime column.
SELECT CASE WHEN 1=1 THEN '' ELSE GETDATE() END
will give you the same value...
You can convert this to a varchar(32), but I'm not certain of the ramifications
SELECT CASE WHEN 1=1 THEN '' ELSE CAST(GETDATE() AS varchar(32)) END
A column can only return one data type - DATETIME != string/VARCHAR.
If you want a zero length string in the event of the value being NULL, you have to explicitly change the data type, using CAST/CONVERT to change the non-NULL value to a VARCHAR/etc data type.
If you're just checking for NULL values, you might try ISNULL() and cast the date as a varchar.
SELECT ISNULL(CAST(fu.SentOutDate AS VARCHAR(50)), '') AS SendOutDate
FROM tablename
It sounds like you're displaying this value in a GUI or client somewhere. In my opinion, the best practice is to convert it from the NULL value there, not in the query.
If you ever create a database that scales to millions of users, you want a little processing as possible in the database and as much as possible in the client. Doing conversion of a date to character is an unneeded load on the system (character calculation is always much slower than math).
This may work:
case when ISNULL(convert(varchar, a.rec_dt, 108), '00:00:00')='00:00:00' then ''
else CAST(rec_dt as varchar)