How to mark a string in Microsoft SQL - sql-server

Hy all,
I have encountered a problem while cleaning some data where I can't seem to "mark" this string as a string.
The problem is as follows:
select JobPosition,
case
when (JobPosition = 'Seniority level': 'Entry level'},) then 'Entry level'
else JobPosition
end
from JobsUSA
So the 'Seniority level': 'Entry level'}, I can't put inside '' that to mark it as a string.
I've tried different versions of putting '' in all kinds of places but without luck. I'm relatively new in SQL so all help would be appreciated.
I've tried to Google the problem but I can't seem to find a solution

you can use function cast(), for example: cast(expr AS type)

When using single quotes in a quoted string you need to double them up - 'this string contains ''single'' quotes'

Related

Matching Regular Expressions In SQL Server

I am trying to extract id of Android app from its url but getting extra characters.
Using replace function in sql server, below are two sample urls:
https://play.google.com/store/apps/details?id=com.flipkart.android&hl=en com.flipkart.android
https://play.google.com/store/apps/details?hl=en_US&id=com.surveysampling.mobile.quickthoughts&referrer=mat_click_id%3Df1901cef59f79b1542d05a1fdfa67202-20150429-5128 en_US&id=com.surveysampling.mobile.quickthoughts&r
I am doing this right now:
SELECT
SUBSTRING(REPLACE(PREVIEW, '&hl=en',''), CHARINDEX('?', PREVIEW) + 4 , 50)
FROM OFFERS_TABLE;
But for 1st I am getting com.flipkart.android which is correct, but for 2nd I am getting en_US&id=com.surveysampling.mobile.quickthoughts&r.
I want to remove en_US&id from starting of it and &r from its end.
Can someone help me with any post or url from where I can refer?
What you are actually trying to do is extract the string preceded by id= until the & is found which is separator for variables in URL. Taking this condition I came up with following regex.
Regex: (?<=id=)[^&]*
Explanation: It uses the lookbehind assertion that is the string is preceded by id= until the first & is found.
Regex101 Demo
It seems like you've made some assumptions of lengths. The the &r is appearing because that is 50 characters. You are also getting the en_US because you assumed 4 characters at the beginning but your second string has more. Perhaps you can split on & and then look for the variable that begins with id=.
it seems like a function like this would help.
http://www.sqlservercentral.com/blogs/querying-microsoft-sql-server/2013/09/19/how-to-split-a-string-by-delimited-char-in-sql-server/

Query on Left function in power builder

I'm not able to figure out this issue its on power builder code.
I'm fetching a where clause and storing in a string variable and I'm trying to remove the last five characters i.e OR ". I'm using the below function but it is not working in Power Builder in ODBC connection but working in sybase connection. Any help is highly appreciated ?
Thanks
szClause= "fd_M.fd_term_date is null AND (fd_M.fd_incom_est_yn = 'Y' OR (EXISTS (SELECT * FROM fd_M f WHERE fd_M.fd_ref_fund = f.fd_id AND f.fd_incom_est_yn = 'Y'))) AND (Upper(fd_M.fd_reprt_c3)='4HSJ' OR "
szClause= Left(sClause, Len(sClause) - 5)
The obvious answer is that the last five characters of the string are ' OR, so you're trimming the closing quote at the end of '4HSJ'.
Have you tried szClause= Left(sClause, Len(sClause) - 4)?
Thanks for the response. Issue has been solved I passed wrong variable.

Get substring until a specific character starting from right to left SQL

I have a string column where I need to find the substring until the first '-' is found.
Example column ELOT-IGS-2. I need to get "2" as output.
These columns come from a table so I cannot declare the variable as a fixed string.
I tried LOCATE, SUBSTRING_INDEX but none are build in functions.
I also tried RIGHT(ID_BETSLIP,CHARINDEX('-',ID_BETSLIP)-1) but this does not work when I have 2 times "-"
Does anyone have an idea?
select RIGHT(<your Field>, CHARINDEX('-',REVERSE(<your Field>))-1)
DECLARE #t varchar(20) = 'ELOT-IGS-2'
select REVERSE(SUBSTRING(reverse(#t),0,CHARINDEX ('-',REVERSE(#t))))
Simplest would be
select SUBSTRING(reverse(ELOT-IGS-2),0,charindex('-',reverse(ELOT-IGS-2)))
Based on #mohan111's solution you can add REVERSE in order to get what you need
DECLARE #t varchar(20) = 'ELOT-IGS-2'
select REVERSE(SUBSTRING(reverse(#t),0,CHARINDEX ('-',REVERSE(#t))))
You should have done this yourself, once you got #mohan111's solution! For your own improvement you can not ask for everything :-( Once you got a solution that is almost what you need, it is time to try to improve it yourself. Please check this MSDN page, and every time that you need to parse a string, START HERE :-) Try to go over the functions and maybe you will find the solution.
The REVERSE query and most string parsing function are not good solution in SQL Server, and in most cases you are better to do this using SQLCLR function.

How do I do quotes within quotes in TSQL

I have a case statement that will set a varchar variable #stored_proc_name to the name of a procedure that I want to call later on. One of the procedures I want to call takes a varchar as an argument (and also an int). How do I do this? Currently what I have is this
SELECT #stored_proc_name = CASE #p_process_name
WHEN 'misdate' THEN 'findb..sp_cycle_date_daily'
WHEN 'balpremmis' THEN 'findb..pc_bal_writ_prem'
WHEN 'ursctl' THEN 'MIS_feeds..pc_mis_update_feed_control "URSPO", 1'
ELSE NULL
END
EXECUTE #stored_proc_name
The last one is my procedure that takes arguments, where "URSPO" should be the first, and 1 is the int. It is running on SQL server 2k8 (I think I remember some difference with single vs. double quotes between this and older versions). Is what I have correct? I admit that I haven't tested it yet, but it is a part of a huge process that I really don't want to kick off right now.
Thank for any help!
To escape a single quote inside a string literal, use a double quote like below. See the Constants section in the BOL for more information:
select ' '' ' -- creates a string containing a space, a single quote, and another space
In your example, this would be the string:
'MIS_feeds..pc_mis_update_feed_control ''URSPO'', 1'
If you were going to use this string in a LIKE expression, you might want to refer to this question.
And if you were going to build a string to be a SQL identifier, you might want to escape it using the QUOTENAME function.
You can use the code below:
set quoted_identifier off;
Select "'Hi'"
quoted_identifiere will enable you to use ", instead of ';

Is it possible to concat strings in SOQL?

I've read thread from 2005 and people said SOQL does not support string concatenation.
Though wondering if it is supported and someone has done this.
I'm trying to concat but no luck :(
Below is APEX code trying to find record with specified email.
String myEmail = 'my#email.com';
String foo = 'SELECT emailTo__c, source__c FROM EmailLog__c
WHERE source__c = \'' +
myEmail + '\';
Database.query(foo)
Even though the record is indeed in the database, it does not query anything. Debug shows
"row(0)" which means empty is returned.
Am I doing concat wrong way?
UPDATE
I just found a way not have to add single quote. Just needed to apply same colon variable even for String that has query.
String foo = DateTime.newInstance(......);
String bar = 'SELECT id FROM SomeObject__c WHERE createdOn__c = :foo';
List<SomeObject__c> result = Database.query(bar);
System.debug(result);
This works too and is necessary if WHERE clause contains DateTime since DateTime cannot be surrounded with single quotes.
Why do you use Database.query()? Stuff will be much simpler and faster if you'll use normal queries in brackets
[SELECT emailTo__c, source__c FROM EmailLog__c WHERE source__c = :myEmail]
Not to mention that parameter binding instead of string concatenation means no need to worry about SQL injections etc.. Please consider getting used to these queries in brackets, they look weird in beginnign but will save your butt many times (mistyped field names etc).
As for actual concatenation - it works like you described it, I'm just unsure about the need to escape apostrophes. Binding the variables is safest way to go.
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic_soql.htm
http://www.salesforce.com/us/developer/docs/api/index_Left.htm#CSHID=sforce_api_calls_soql.htm|StartTopic=Content%2Fsforce_api_calls_soql.htm|SkinName=webhelp

Resources