Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am on SQL Server 2012, I am trying to write a SELECT statement that uses LEFT() and RIGHT() functions to display a ProductName if the 3rd letter from the end of the product name is a particular letter.
Example the 3rd letter is an 'a'
The simplest optio here is to use like:
select ProductName from Products where ProductName like '%a__'
% is a wildcard matching any number of characters, and _ matches a single characters. like '%a__' means the third-to-last letter is an a.
Working example: http://www.sqlfiddle.com/#!6/6b479/8
If you want to use left and right, you should use:
select ProductName from Products
where len(ProductName)>=3 AND left(right(ProductName,3),1)='a'
Working example: http://www.sqlfiddle.com/#!6/6b479/2
left(right(ProductName,3),1) means: take the last three letters, and take take the first letter out of them. We also check for len(ProductName)>=3 to make sure there are at least three letters, otherwise we may return short product names that begin with an a.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
It's a SQL instance and the examples are as follows:
Table values to transform:
String is Donald.Duck and the desired result is DDuck
String is Mickey.Mouse and the desired result is MMouse
you can use a regular expression in sql. it runs slower but it will use the dot.net framework
The regular expression is simple. Look forward and find a period then capture all non space characters proceeding the period.
how to call a regular expression in tsql
https://www.sqlshack.com/t-sql-regex-commands-in-sql-server/
#extract the data to a csv then process it and update back into the database
txt="Donald.Duck and the desired result is DDuck String other strings are Mickey.Mouse and Daffy.Duck"
pattern = re.compile(r'([a-zA-Z]+\.[a-zA-Z]+)')
result=re.findall(pattern,txt)
for item in result:
front,back=item.split('.')
txt=re.sub(item, front[0]+'.'+back,txt)
print(txt)
output
Donald.Duck and the desired result is DDuck String other strings are Mickey.Mouse and Daffy.Duck
Sql steps:
split the words into a table of words by space
fetch each word and do an instr to check for a period
split the front and back word by period
keep the first character of the front word and reassemble and replace the phrase
join the words into a sentence and update the database with the new sentence.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I Have below query:
where POSTDATE between to_date('&START_DATE','DD-MM-YYYY') and to_date('&END_DATE','DD-MM-YYYY')
and username='&username'
But my requirements are:
I need all usernames between Start date & End date(3rd Input is
blank) AND / OR
specified usernames(in 3rd Input) between provided
start date and End date in query
You want the date range check anyway, then you either want all users or a specified user; so you can check if you were given a username:
where POSTDATE between to_date('&START_DATE','DD-MM-YYYY') and to_date('&END_DATE','DD-MM-YYYY')
and ('&&username' is null or username='&&username')
If no user name is given then the second line evaluates to:
and ('' is null or username='')
In Oracle '' is equivalent to null (though they have always warned that could change one day...) so the first clause is true.
You haven't said if you're explicitly prompting for the inputs (with accept); if you are then you could use single & or double &&, but if not using double && will prevent you being asked twice.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to search only in 2016 year.
select from products
WHERE year="2016"
and name like '%"& name &"%'
or size like '%"& Size &"%'
Put parens around your or-ed conditions, and change the double quotes to single quotes for the year (as suggested by jarlh):
select from products
WHERE year='2016'
and (
name like '%"& name &"%'
or size like '%"& Size &"%'
)
Oh and in case this is VB.NET and you got name and Size from a parameter or something, make sure you are escaping them to prevent SQL injection (if this not already done somewhere else).
No need for double quotes, or any quotes in the LIKE.
SELECT *
FROM products
WHERE year = '2016'
AND (name LIKE '%name%' OR size LIKE '%size%'
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
You can I display special Unicode characters in my result set on my SQL Server?
e.g. How can i display those characters ☺♦♦?
Well due to the fact that your question is very unspecific here are some solutions.
You need to specify a string as unicode string. This can be achieved by adding an N in front of the string. See example:
SELECT N'☺♦♦' as a, '☺♦♦' as b
Result:
a b
---- ----
☺♦♦ ???
If you want to store those symbols, you need a type as nvarchar or nchar.
Hopefully this helps you.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to complete cards numbers in sql. I have the prefix =11111 and the number of the card which is variable, therefore it could be '25' or '2130' but at the end I must have 14 numbers. So I need to fill spaces with zeros.
I've read about 'LPAD' but I don't understand very well this method.
You could use lpad, but if you're starting with a number you could use a 9-digit format model instead, and concatenate that onto your prefix:
select '11111' || to_char(25, 'FM000000000') from dual;
11111000000025
The FM format modifier stops Oracle adding a space for a potential +/- sign indicator.
SQL Fiddle demo
Use the ZEROFILL attribute.
But your database should only be responsible for saving data and not changing it before saving.
The best way would be to send the zerofilled data to the database server.