view in snowflake as follows;
CREATE OR REPLACE VIEW A AS(
SELECT tab1.colA,
tab1.colB,
CASE WHEN COALESCE(DATE_ACT_CREATED,'missing')='missing' THEN DATE ('1970-01- 01') ELSE TO_DATE(DATE_ACT_CREATED) END AS ACT_CREATED_DATE
FROM tab1);
Please note that because of some reasons, column DATE_ACT_CREATED was defined as varchar earlier. This view is created fine. But when I try to retrieve records from it;
SELECT * FROM viewA;
I get following error,
Date '' is not recognized
But when I take out the entry; CASE WHEN COALESCE(DATE_ACT_CREATED,'missing')='missing' THEN DATE ('1970-01- 01') ELSE TO_DATE(DATE_ACT_CREATED) END AS ACT_CREATED_DATE the error is gone.
May I know how can I handle this date error issue? Found this link enter link description here, but it couldn't help much.
Help is appreciated.
You get the error because the case when DATE_ACT_CREATED is an empty string is not handled in your CASE condition. So the TO_DATE function couldn't work.
You should try TRY_TO_DATE. If your entry couldn't be casted as a date then it returns null that will be handled with your COALESCE.
CREATE OR REPLACE viewA AS(
SELECT
tab1.colA,
tab1.colB,
COALESCE(TRY_TO_DATE(DATE_ACT_CREATED), DATE('1970-01- 01')) AS ACT_CREATED_DATE
FROM tab1);
Using the following query you will not get the error if the input is ''(Empty string), actual date, NULL keyword, or missing keyword.
CREATE OR REPLACE VIEW A
AS
SELECT COL1, COL2,
CASE WHEN COALESCE(DATE_AT_CREATED, '') = '' THEN DATE('1970-01-02')
ELSE CASE WHEN DATE_AT_CREATED = 'missing' THEN DATE('1970-01-02') ELSE TO_DATE(DATE_AT_CREATED) END
END AS ACT_CREATED_DATE
FROM TAB1;
SELECT * FROM A;
Related
I wrote the following query in T-SQL for SQL Server
SELECT
CASE
WHEN ADDR_LINE_1 REGEXP '^[0-9]'
THEN SUBSTRING(ADDR_LINE_1,1,CHARINDEX(' ',ADDR_LINE_1))
ELSE NULL
END AS HOUSE_NUMBER
FROM CUSTOMER
What I want is that if the column ADDR_LINE_1 starts with a number, I want to extract the HOUSE_NUMBER from it. But right now my query gives a parse error. If I replace the word REGEXP with LIKE, the parse error goes away, but I always get NULL for HOUSE_NUMBER. What is the correct syntax for my query?
You could use ISNUMERIC and LEFT like this.
SELECT
CASE WHEN ISNUMERIC(LEFT(ADDR_LINE_1, 1)) = 1
THEN SUBSTRING(ADDR_LINE_1, 1, CHARINDEX(' ', ADDR_LINE_1))
ELSE NULL
END AS HOUSE_NUMBER
FROM CUSTOMER
How about using the LIKE
SELECT
CASE
WHEN ADDR_LINE_1 Like '%[0-9]%'
THEN SUBSTRING(ADDR_LINE_1,1,CHARINDEX(' ',ADDR_LINE_1))
ELSE NULL
END AS HOUSE_NUMBER
FROM CUSTOMER
How can I create a select statement for a view and inside of it, convert the datatype of the OverallSat Column from nvarchar(10) to a decimal(10,2) only where the values are not null. Crystal reports is having a difficult time with this column.
Current Statement:
SELECT [BPF_FR_ID]
,[ResolveDate]
,[Organization]
,[AssigneeGroup]
,[Survey_Category]
,[HDATechComp]
,[TechComp]
,[ProfCourt]
,[Timeliness]
,[OverAllSat]
,[KeySent]
,[CheckSave]
,[TicketNumber]
,[Form]
FROM [NotesData].[dbo].[BPF_FinanceReport_tbl]
I am looking to replace the value of [OverAllSat] with something like this...
Select OverAllSat,
Case When IsNumeric(OverAllSat)<>0 THEN
CONVERT(nvarchar(10),CONVERT(decimal(10,2),OverAllSat))
ELSE
OverAllSat
End as OverAllSat
From [NotesData].[dbo].[BPF_FinanceReport_tbl]
Is this possible through a view selection criteria, or does it have to be somewhere else(trigger on insert/update). any Examples are greatly appreciated.
Thank you!
You can use ISNULL()
Select OverAllSat,
Case When IsNumeric(ISNULL(OverAllSat,0)) = 1 THEN
CONVERT(decimal(10,2),OverAllSat)
ELSE
CONVERT(decimal(10,2),ISNULL(OverAllSat,0))
End as OverAllSat1
From [NotesData].[dbo].[BPF_FinanceReport_tbl]
Or if you are really strict about how it has not to be null then you have to add it to your case condition.
Select OverAllSat,
Case
When OverAllSat IS NOT NULL AND IsNumeric(OverAllSat) = 1 THEN
CONVERT(decimal(10,2),OverAllSat)
When OverAllSat IS NOT NULL THEN
CONVERT(decimal(10,2),OverAllSat)
ELSE
0
End as OverAllSat1
From [NotesData].[dbo].[BPF_FinanceReport_tbl]
You'll have to separate that out to two columns. One that is decimal(10,2) and the other that is varchar(10). This sort of formatting task should be left to your presentation layer if possible.
I have a column that holds a rejected reason for a person.
The value is either something like "Person refused" or "10/18/2012"
I'm trying to setup my query to check if the value is a date, then it can put a blank space in its place. Otherwise, keep the text that's in there.
The query that I setup is:
SELECT DISTINCT person_id,
CASE description
WHEN ISDATE(description) THEN ''
END AS refuseReason
FROM person
But this returns an error. If the dates themselves are saved as VARCHAR, then would I have to convert them first to a date, then check to see if ISDATE? If I did that, would it convert the text when it comes accross that?
EDIT: I tried it by casting it to datetime, and i keep getting error as well:
SELECT DISTINCT person_id,
CASE description
WHEN ISDATE(CAST(OBSVALUE AS DATETIME)) THEN ''
END AS refuseReason
FROM person
(Assuming TSQL) IsDate() returns 1 or 0. (Also your CASE syntax was incorrect)
Try:
SELECT DISTINCT person_id,
CASE
WHEN ISDATE(description) = 1 THEN ''
ELSE description
END AS refuseReason
FROM person
The ISDATE() functions returns a boolean 1 or 0
you have to make the clause by using
WHEN ISDATE(#variable) = 1 THEN ''
ELSE #variable
I use this command to select all the specific dates if the given variable is date, if it is not it should return all of the fields.
The commands works when #query is in the form of a date, but it returns an error:
"Conversion failed when converting date and/or time from character string."
when it is any other arbitrary string.
Code:
select * from table where
format(dateofbirth,'dd/MMM/yyyy') = Case
when ISDATE(#query)=1 then
format(CONVERT(datetime,#query),'dd/MMM/yyyy')
else
format(dateofbirth,'dd/MMM/yyyy')
Edit:
#query can be any string for eg. "1/1/2013" , "random" , "3".
The command should return all fields if #query is not in form of a date.
You can work around this problem by re-formulating your query condition like this:
declare #query as varchar(20)='blah'
SELECT *
FROM testtable
WHERE ISDATE(#query) = 0
OR CONVERT(date, dateofbirth) = CASE ISDATE(#query)
WHEN 1 THEN CONVERT(date, #query) ELSE NULL
END
Demo on sqlfiddle.
The problem is that logic operators are not short-circuited in SQL, so the optimizer treats CONVERT(date, #query) as something that it can pre-compute to speed up the query. By expanding the condition to a CASE that depends entirely on #query you can eliminate the execution of the CONVERT branch when ISDATE(#query) returns "false".
I have a query which selects a table based on the provided uniqueidentifier Id so when i provides Id to a value it returns the result but if i am passing it like below i am getting an error message. Please suggest how to use Convert or Cast in this case to resolve this issue.
Thanks
SELECT * FROM TBL WHERE ID=''
Assuming #id is a string parameter, and that the ID column is not nullable, you could do something silly like this:
SELECT * FROM TBL WHERE ID = COALESCE(NULLIF(#id, ''), ID);
But really you should be passing NULL if there is no value, not an empty string. This shouldn't be a string parameter in the first place.
According to the comments on this answer, the solution should be something like this (in pseudo-code):
if textbox1.Text.ToString().Equals('') {
SELECT * FROM TBL
}
else {
SELECT * FROM TBL WHERE ID='"+textbox1.text+"'
}
You may do the IF in the C# application.