Searching and Replacing Null Value - case

I'm looking to check my SourceCode variable for Null values and then convert all Nulls to the text value BaseSource. Before I run this on live data I want to make sure that is what the following code will do:
,case ISNULL(SourceCode,' ')
when ' ' then 'BaseSource'
else SourceCode end
Can I expect that with this code Null values will first be converted to a blank spaces, and then to BaseSource within the current SourceCode column?
Thank you.

In SQL SERVER you can use a syntax like this:
UPDATE YourTable SET SourceCode = ISNULL(SourceCode, 'BaseSource')
Before UPDATE your live data you can try this SELECT:
SELECT ISNULL(SourceCode, 'BaseSource') FROM YourTable

Related

what to do NOT to round my keyid while importing table from oracle to R [duplicate]

I'm using the RODBC package inside an ORACLE DATA BASE (DB). Everything is doing very well, but I need to obtain a table from this DB and some variables as character type, not numbers.
So, I made my query like this:
e ManzResul_VIII<-sqlQuery(con,"select distinct t.fono_id_dis,
t.id_predio,
t.co_calle,
t.nu_casa,
t.x,
t.y,
t.plancheta from c_araya.proy_dist08_todo t where nvl(t.fono_tipo_dis, '-') not in ('CLIENTE', 'POTENCIAL', 'CARTERA') and nvl(t.x, 0) <> 0 ")
Is impossible to get the ID number as Character, this query change the type of my iDs variables from Character to Numeric type (The ID has a zero at the beginning, but has been changed it into a number). I have read the function description, but I can see how to manage it.
Any idea would be really appreciated, thanks in advance!
Change the query to cast the id to the data type you want:
select distinct cast(t.fono_id_dis as varchar(255)) as id
. . .
This should work with most databases.
This works for me:
library(RODBC)
con <- odbcDriverConnect('driver={SQL Server};server=[your server name];database=[your database name];trusted_connection=true')
ManzResul_VIII<-sqlQuery(con,"select distinct ('m' + id) as id from [your table]")

Issue with Casting string to date in Teradata

I have a string column - COL1 in TABLE1 which is if string data type. This table is loaded by Informatica session ( data coming from mainframe) and the format of the COL1 is YYYY-MM-DD. Now I have to use TABLE1 as the source in my next mapping . In the SQL override query of second mapping i will be casting COL1 to date using the below query .
SELECT
CAST(COL1 AS DATE FORMAT 'YYYY-MM-DD') AS CHK_DT FROM TABLE1
But when i try to execute this query in Teradata SQLA, just to check if it runs fine it gives me below error.
SELECT Failed. 2666: Invalid date supplied for COL1.
Can you please help me resolve this issue ? This is not the only date column which has issue, there are two more date columns . I guess the resolution is same for all three columns .
P.S - Just to verify, I updated all rows of COL1 of TABLE1 as 2016-12-12 and ran the select statement, select worked fine . I then updated COL1 of all rows as 2016-13-12, it gave same error . If either of DD or MM is more than 12, it is giving me error
Thanks
If DATE is represented/stored in ANSI standard literal YYYY-MM-DD, the CAST will work.
SELECT CAST('2016-12-13' AS DATE FORMAT 'YYYY-MM-DD') AS Date1
However i doubt that in your case.
The date is most probably in YYYY-DD-MM format. In that case the ANSI standard format will throw the error. You need YYYY-DD-MM
select CAST('2016-13-12' AS DATE FORMAT 'YYYY-DD-MM') AS Date2
P.S. You can confirm the conversion to date using TYPE() function. It should return DATE in your case
Hi Please try this piece of code
CAST(CAST(date_col AS FORMAT 'YYYY-MM-DD') AS VARCHAR(15))
instead of the transformation you are using.
Thanks for your response. However the issue was something else. Some of the incoming records had space in this column . So I had to tweak my informatica mapping to put a trim on date column . Now the select is running fine . Thanks for your time .

How to use MariaDB's REGEXP_REPLACE?

I have read the docs for MariaDB's REGEX_REPLACE but cannot get my query to work. I am storing links in a column, link and want to change the end of the link:
From www.example.com/<code> to www.example.com/#/results/<code> where <code> is some hexidecimal hash, e.g. 55770abb384c06ee00e0c579. What I am trying is:
SELECT REGEX_REPLACE("link", "www\\.example\\.com\\/(.*)", "www\\.example\\.com\\/#\\/results\\/\\1");
The result is:
Showing rows 0 - 0.
I wasn't able to figure out what the first argument was--the documentation says "subject". Turns out it's just the column name. So this works:
UPDATE my_table
SET my_link = REGEXP_REPLACE(
my_link,
"http:\\/\\/www\\.example\\.com\\/(.*)",
"http:\\/\\/www\\.example\\.com\\/#\\/results\\/\\1")
WHERE my_link IS NOT NULL

Prompt comes up when running report

I have created a report from the following query
SELECT TasksEntries.Project, TasksEntries.Task, Sum(TimeTracker.WorkHours) AS
TotalHours
FROM TasksEntries INNER JOIN TimeTracker
ON (TasksEntries.EmployeeId = TimeTracker.EmployeeId) AND (TasksEntries.TaskID
= TimeTracker.TaskId)
GROUP BY TasksEntries.Project, TasksEntries.Task;
The start and end dates are captured from a form and passed like shown below. WorkDate is a column in the TimeTracker table and is not present in any other table.
strWhere = "WorkDate BETWEEN #" & txtMgrRptStartDate & "# AND #" &
txtMgrRptEndDate & "#"
DoCmd.OpenReport "rptTotalProjectHours", acViewPreview,
"qryTotalProjectHours", strWhere, acWindowNormal
When the report is invoked, I get a box where it says
"Enter parameter value" for Workdate.. I am unsure why I get that.
The query runs fine when I run it separately with the start and end dates hard coded in there. The problem seems to occur only when it is passed through the docmd.openreport. Can someone help me get to the bottom of this? When passing the WorkDate thru the OpenReport, is it mandatory that the column name be present in the SELECT statement?
Appreciate your help in advance.
I solved it by including the date criteria in the WHERE clause of the SELECT statement and specifying the form text box names of start and end dates.
The other thing that I did was to eliminate the query name from the Docmd.openreport since the report was created from the named query.

How to do a column name inside of a dynamic where clause? TO_NUMBER(column name)

I am currently trying to create a dynamic Select statement when the user has to input a various amount of criteria to search by.
Currently, I have every part of the statement working except for the most important part.
I am attempting to do something like this:
selStmt := 'SELECT column_one, column_2, column_3
FROM nerf
whereClause := ' WHERE TO_NUMBER('''|| column_one ||''') <= '''|| userInput ||'''';
However, in doing this the WHERE cluse of my SELECT statement is not accurate as shown by my output line:
WHERE TO_NUMBER('') <= '5';
I have tried various solutions with quote marks and I end up with either a ORA-00905 missing identifier error, or I get a ORA-00911: invalid character error.
At this point I'm not quite sure how to approach this issue.
Any useful help gets thanks in advance.
For some reason, Oracle uses the single quote to delimit strings and to escape characters, so using '' is an instruction to Oracle to add a quote inside your string. Example:
'This is a string with a quote here: '' and then it ends normally'
will be represented as
This is a string with a quote here: ' and then it ends normally
In your example, you are ending the WHERE clause you're building up and then concatenating a PL/SQL variable identifier called column_one:
' WHERE TO_NUMBER('''|| column_one ||''')
...and with a NULL value for the identifier column_one this is represented as
WHERE TO_NUMBER('')
Presumably you want to reference column_one from inside the query, and not from a PL/SQL variable of the same name, so you should remove the quotes around it like so:
whereClause := ' WHERE TO_NUMBER(column_one) <= TO_NUMBER('''|| userInput ||''')';
Escaping strings in Oracle is often infuriating - it helps a lot if you have a good IDE with decent syntax highlighting like TOAD or SQL*Developer.
This should work:
selStmt := 'SELECT column_one, column_2, column_3 FROM nerf';
whereClause := ' WHERE TO_NUMBER(column_one) <= TO_NUMBER('''|| userInput ||''')';

Resources