I have the couple of columns defined as decimal(16,2).
I would like to leave the value '' (blank) in it.
When I have the select query as
CASE
WHEN FIELD1 IS NULL THEN ''
ELSE FIELD1
END AS FIELD_NAME
This will not allowed as the nature of the column.
Could you please help me how can I put a blank value in this column?
Many thanks
You will either need to leave the values as NULL, which is recommended solution, or convert everything to text. The reason for the error is that you cannot return a decimal and a text value in the same column ('' is a text value).
To convert everything to text and insert the blank value, use the following:
SELECT ISNULL(CONVERT(VARCHAR(20), Field1), '')
FROM myTable
I agree with comments, but if you must do it in SQL you could use a cast in your case statement as per below code. Better handled in front end though as the below code would mean your final select returns a varchar and not a decimal. Handled front end, you can return a decimal from SQL and display blank if NULL.
create table #temp (thing decimal(16,2))
insert into #temp (thing) values(NULL)
select
case when thing IS NULL then ''
else cast(thing as varchar(5)) end
from #temp
Related
I'm trying to add a calculated field to an existing table in SSMS, which will convert a string in the format YYYYMMDD to a date format, but I am getting errors regarding the string field not being valid.
I require the calculated field as I have '00000000' values (i.e. NULL) in the string field so can't use this in date calculations.
The code I'm using is :
ALTER TABLE [TEM].[AssignmentRates]
ADD [Date_Expired] DATE NULL
(SELECT CONVERT([date], CASE WHEN [Expiry_Date]='00000000' THEN NULL ELSE [Expiry_Date] END))
where [Expiry_Date] is the string column I'm trying to convert, and [Date_Expired] is the name of the calculated column I'm trying to add.
I get this error:
Invalid column name 'Expiry_Date'
against both instances of that field name, and can't work out why. If I run the query as a stand alone SELECT it returns the required results.
Using table aliases or the full database, table and column name for it don't appear to work either.
It's probably something incredibly obvious, but I haven't been able to work out what it is.
The error on expiry_date seems quite clear -- that is not the name of a column in the table. But you can simplify the logic:
ALTER TABLE TEM.AssignmentRates ADD Date_Expired AS
(TRY_CONVERT(date, Expiry_Date));
Actually, the nested SELECT may have caused an issue. That would not normally be used for a computed column.
Looks like a syntax issue
alter table [TEM].[AssignmentRates]
ADD [Date_Expired]
as
(
case Expiry_Date when '00000000' then null else cast(Expiry_Date as date) end
)
Your syntax was invalid for a computed column. What you had was actually adding a regular column (successfully) but then the attempting to run a select statement which was causing those column-name errors as it didn't have a from clause for context.
ALTER TABLE TEM.AssignmentRates /* don't do this */
ADD Date_Expired DATE NULL /* implied end of statement */
(SELECT CONVERT(date, CASE WHEN Expiry_Date = '00000000' THEN NULL ELSE Expiry_Date END));
Had you used the proper syntax, you'd at least get an informative error:
ALTER TABLE TEM.AssignmentRates /* error! */
ADD Date_Expired AS
(SELECT CONVERT(date, CASE WHEN Expiry_Date = '00000000' THEN NULL ELSE Expiry_Date END);
/*ERROR: Subqueries are not allowed in this context. Only scalar expressions are allowed.*/
But really you didn't need a subquery in the first place:
ALTER TABLE TEM.AssignmentRates /* success */
ADD Date_Expired AS
CONVERT(date, CASE WHEN Expiry_Date = '00000000' THEN NULL ELSE Expiry_Date END);
Gordon has a point about just using try_convert() though.
Ok so I have a table with three columns:
Id, Key, Value
I would like to delete all rows where Value is empty (''). Therefore I wrote the query to select before I delete which was:
Select * from [Imaging.ImageTag] where [Value] = ''
all pretty standard so far...
Now heres the strange part. This query returned two rows shown below with commas seperating columns:
CE7C367C-5C4A-4531-9C8C-8F2A26B1B980, ObjectType, 🎃
F5B2F8A8-C4A8-4799-8824-E5FFEEDAB887, Caption, 🍰
Why are these two rows matching on ''?
Extra Info
I am using Sql-Server, The [Value] column is of type NVARCHAR(300) and yes the table name really is [Imaging.ImageTag]
This is collation dependant.
Matches empty string
SELECT 1 where N'' = N'🍰' COLLATE latin1_general_ci_as
Doesn't match empty string
SELECT 1 WHERE N'' = N'🍰' COLLATE latin1_general_100_ci_as
The 100 collations are more up-to-date (though still not bleeding edge, they have been available since 2008) and you should use more modern collations unless you have some specific reason not to. The BOL entry for 100 collations specifically calls out
Weighting has been added to previously non-weighted characters that
would have compared equally.
It's not an answer to your "why", but in terms of your overall goal, perhaps you should alter your strategy for searching for empty values:
Select * from [Imaging.ImageTag] where LEN([Value]) = 0
As per the comments (thanks Martin Smith for providing some copy/pastable emoji):
SELECT CASE WHEN N'' = N'🍰' then 1 else 0 end --returns 1, no good for checking
SELECT LEN(N'🍰') --returns 2, can be used to check for zero length values?
Complementing this answers
When you need use 'like' at sql
WHERE
N'' + COLUMNS like N'%'+ #WordSearch +'%' COLLATE latin1_general_100_ci_as
Google send me here looking for a way filter all rows with an emoji on a varchar column.
In case that your looking for something similar:
SELECT mycolumn
FROM mytable
WHERE REGEXP_EXTRACT(mycolumn,'\x{1f600}') <> ''
--sqlserver WHERE SUBSTRING(MyCol, (PATINDEX( '\x{1f600}', MyCol ))) <> ''
the \x{1f600} is the char code for the searched emoji, you can find the emoji codes here
Ok so I have a table with three columns:
Id, Key, Value
I would like to delete all rows where Value is empty (''). Therefore I wrote the query to select before I delete which was:
Select * from [Imaging.ImageTag] where [Value] = ''
all pretty standard so far...
Now heres the strange part. This query returned two rows shown below with commas seperating columns:
CE7C367C-5C4A-4531-9C8C-8F2A26B1B980, ObjectType, 🎃
F5B2F8A8-C4A8-4799-8824-E5FFEEDAB887, Caption, 🍰
Why are these two rows matching on ''?
Extra Info
I am using Sql-Server, The [Value] column is of type NVARCHAR(300) and yes the table name really is [Imaging.ImageTag]
This is collation dependant.
Matches empty string
SELECT 1 where N'' = N'🍰' COLLATE latin1_general_ci_as
Doesn't match empty string
SELECT 1 WHERE N'' = N'🍰' COLLATE latin1_general_100_ci_as
The 100 collations are more up-to-date (though still not bleeding edge, they have been available since 2008) and you should use more modern collations unless you have some specific reason not to. The BOL entry for 100 collations specifically calls out
Weighting has been added to previously non-weighted characters that
would have compared equally.
It's not an answer to your "why", but in terms of your overall goal, perhaps you should alter your strategy for searching for empty values:
Select * from [Imaging.ImageTag] where LEN([Value]) = 0
As per the comments (thanks Martin Smith for providing some copy/pastable emoji):
SELECT CASE WHEN N'' = N'🍰' then 1 else 0 end --returns 1, no good for checking
SELECT LEN(N'🍰') --returns 2, can be used to check for zero length values?
Complementing this answers
When you need use 'like' at sql
WHERE
N'' + COLUMNS like N'%'+ #WordSearch +'%' COLLATE latin1_general_100_ci_as
Google send me here looking for a way filter all rows with an emoji on a varchar column.
In case that your looking for something similar:
SELECT mycolumn
FROM mytable
WHERE REGEXP_EXTRACT(mycolumn,'\x{1f600}') <> ''
--sqlserver WHERE SUBSTRING(MyCol, (PATINDEX( '\x{1f600}', MyCol ))) <> ''
the \x{1f600} is the char code for the searched emoji, you can find the emoji codes here
Disclaimer:
I am still learning SQL so I apologize if my question comes off as amateur-ish or is otherwise a very simple answer. I have no formal training. I am teaching myself.
The title may be a bit confusing, as I'm not entirely sure how to word this for a title.
Basically, I want to convert a column in a table thats currently VARCHAR into an INT. The column contains only numbers formatted as such:
00001
00005
02150
These are essentially ID's which will be appended to a Name column later for other purposes. If its necessary to do so, I'd also like to know how to convert the end result INT to VARCHAR for the append portion.
Here's what I have right now:
SELECT CONVERT(INT, LocNo)
It returns the results I expect but I think I need to somehow update the existing LocNo column or otherwise put it in a new column for forward use.
What should I do to achieve this?
Try this
UPDATE TableName
SET LocNo = CONVERT(INT, LocNo)
If you want new column, add new column to table and then do update
ALTER TABLE TableName
ADD NewCol Int Null
UPDATE TableName
SET NewCol = CONVERT(INT, LocNo)
When Selecting and appending to varchar you can do
SELECT CAST(LocNO As VARCHAR) + Name as NameAppended From TableName
If you want 0's back in LocNo/newCol then
SELECT right('00000' + CAST(LocNO As VARCHAR),0) + Name as NameAppended
From TableName
UPDATE YourTable
SET LocNo = CONVERT(INT, LocNo)
This might sound kind of weird, but I have a query that joins two tables. I'm using an IF statements that dictates what to return. One path runs the query/join as is, the other needs to return all of the data from the first column, but only return column names with null values. Here's the query i have now:
declare #Date DATE = '06/07/2012'
IF #DATE >= GETDATE()
BEGIN
SELECT DisplayName, '' [RegularHours], ''[OvertimeHours]
FROM Sites
ORDER BY DisplayName
END
ELSE
SELECT sites.DisplayName, hrs.SiteDate, hrs.RegularHrs, hrs.OverTimeHrs
FROM Sites sites
left join SiteHours hrs on sites.SiteID = hrs.SiteID
ORDER BY DisplayName
What's making me nervous is that the second and third columns do not have values at all, not even NULL. I'm worried that this will pose a problem later. Any ideas?
If I understand the question correctly, I think you can do:
SELECT DisplayName, NULL as 'RegularHours', NULL as 'OvertimeHours'