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).
Related
I have imported csv file to sql server manager. one of column is date but his data type is vachar(50).
I used the basic (cast, convert, try_parse) but still get message:
(Conversion failed when converting date and/or time from character
string.)
could you please help me :)
select cast(
replace(
case
when [factuur_datum] in ('null','')
then null
else [factuur_datum]
end, '-','') as date) as [factuur_datum]
from [dbo].[verkoop]
Please use below query to convert it into date
convert(date,'20-12-2018',105)
This should do what you intend:
select try_cast(replace(factuur_datum, '-', '') as date) as factuur_datum
from [dbo].[verkoop]
Non-dates will result in NULL.
I take it factuur_datum is the date column:
select
cast(factuur_datum as date) as [Alias]
from [dbo].[verkoop]
Not sure what the data looks like so can you include an example?
EDIT
convert(datetime,'factuur_datum',1)
possibly will error out if there are null values so you may need to case out the nulls then convert.
I’ve got a date column where some rows have got NULL values. I would like to use ISNULL or something like that to substitute those values with something like ‘N/A’, however, when I try to use ISNULL, I get an error due to two different data types. If I try to convert my Date column to VARCHAR in order to be able to use ISNULL, then the way my column displays dates gets distorted. Is there any way to solve this problem?
ISNULL(DateSent, 'N/A')
I recommend COALESCE(), because it is standard. However, your problem is that the first column is a date/time and that is not compatible with a string.
So, you need to convert the value. You can use the default format:
COALESCE(CONVERT(VARCHAR(255), DateSent), 'N/A')
Or you can add a conversion argument:
COALESCE(CONVERT(VARCHAR(255), DateSent, 120), 'N/A')
Or you can use FORMAT() for more flexibility.
You can try this
Select ISNULL(Cast(DateSent as Varchar(20)), 'N/A')
Below is one example:
declare #DateSent date = getdate()
Select ISNULL(Cast(#DateSent as Varchar(20)), 'N/A')
For null values as below.
declare #DateSent1 date = NULL
Select ISNULL(Cast(#DateSent1 as Varchar(20)), 'N/A')
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>>]
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.
I have alphanumeric values like. XYZ1,XYZ2......XYZ11, XYZ12 and so on, now I want to select only the Max numeric value, i.e. 12 here.
I tried-
select max(REPLACE(ID,'XYZ','')) from myTable;
but this is returning 9. why?
Try converting to INT before max
select max(cast(REPLACE(ID,'XYZ','') as int)) from myTable;
It's still treating your value as a string instead of a number. Try:
select max(CAST(REPLACE(ID,'XYZ','') AS INT) from myTable;
Because you're still comparing strings. The fact that they contain only numeric digits doesn't mean that they're not strings. You need to convert them:
SELECT MAX(CAST(REPLACE(id, 'XYZ', '') AS INT)) FROM My_Table
Another method is
select max(REPLACE(ID,'XYZ','')*1) from myTable