sql server like query for Unicode - sql-server

i am trying to retrieve data from database using Unicode character ₹ with following query
select AnnualSalary,* from UserDetails where AnnualSalary like N'₹%'
I don't understand why this query is not working as normal like query, is there any other way to do this ? please suggest if there is another way

Maybe something like the following will help:
DECLARE #Tab TABLE (ID INT, Salary NVARCHAR(MAX))
INSERT #Tab VALUES (1, N'₹10,000'),(2,N'20,000'),(3,N'30,000'),(4,N'₹50,000')
SELECT ID, CAST(Salary AS nvarchar) Salary
FROM #Tab
WHERE UNICODE(Salary) = UNICODE(N'₹')

hey guys BJones ans is working fine for me and i also found another way and posting here more information
select AnnualSalary,* from UserDetails where CHARINDEX(N'₹', AnnualSalary) > 0
this also gives me the same result as BJones ans give

When you hard-code literals in unicode, you should add the N letter just before the start of the string.
This is non-unicode
'Your string'
This is unicode
N'Your string'
So your like should be
LIKE N'₹%'

select AnnualSalary,* from UserDetails where AnnualSalary like LIKE UNICODE(N'₹%')
Please try this.Its working in SQL Server 2017

you can try below one it will give you correct answer.
select AnnualSalary,* from UserDetails where AnnualSalary like '\U+20B9%';

Related

Convert number to varchar using to_varchar with leading zeros in Snowflake

In Snowflake there is a number column storing values like: 8,123,456. I am struggling determining how to structure select statement to return a value like: 00008123456.
In SQL SERVER you can do something like this:
right(replicate('0', 11) + convert(varchar(11), job_no), 11) AS job_no
I understand Snowflake to_varchar function can be used, but not sure how to structure like in SQL Server.
Just add a format string on to_varchar():
select to_varchar(8123456, '00000000000');
Give it try with: lpad(to_char(nos),11,0)
More examples & details: https://docs.snowflake.com/en/sql-reference/functions/lpad.html#examples
a solution provided by Greg Pavlik saved me.
I just switched from DB2 to Snowflake.
So, TO_VARCHAR( <numeric_expr> [, '<format>' ] ) worked.

Build a SQL query with a DateTime parameter

I can successfully use a Query with a Date parameter in string format as
SELECT * FROM ORDERS WHERE [DATE]='20160209'
but I haven't seen any sample of a Query specifying a DateTime parameter in string format.
Next samples are rejected by Microsoft SQL Server Management Studio:
SELECT * FROM ORDERS WHERE [DATE]='20130523T153500000Z'
SELECT * FROM ORDERS WHERE [DATE]='2013-05-23T15:35:00:000Z'
I know this is not a good practice and I should pass DateTime values rather than strings, but sometimes it is useful for debugging.
What is the right format to include a string formatted datetime on a SQL query?
No so sure where you've got those date formats...
This one '2013-05-23T15:35:00:000Z' just doesn't seem to be right. I haven't seen that nanoseconds were delimited by a ':' character. It is usually a decimal of a second, so '2013-05-23T15:35:00.000Z' is a better format and it works:
select convert(DateTime,'2013-05-23T15:35:00.000Z')
As for the other, you might need to do the parsing yourself:
select CONVERT(DATETIME,LEFT('20130523T153500000Z',4)+SUBSTRING('20130523T153500000Z',5,2)+SUBSTRING('20130523T153500000Z',7,2))
hope this helps.
Can you just do something like this?
SELECT *
FROM ORDERS
WHERE [DATE] = CONVERT(DATETIME,'20130523T153500000Z')
As long as the string is in a workable format.
If it's just for debugging, you might do something like:
DECLARE #val VARCHAR(25)
-- Easily swapped out with different testing values
SET #val = '20130523T153500000Z'
SELECT *
FROM Orders
WHERE [DATE] = CAST(#val AS DATETIME)
-- You could also use CONVERT

Pro*C returning Not a number in the IN clause

I am trying to run an SQL from my C code using Pro*c. This is my SQL
EXEC SQL select count(1) from MY_TABLE where id IN ( :format );
id is a NUMBER(10) and format is a char array containing value 1,2,3,4,5
This is returning error with "Not a Number"
However if the format array is just a single number, its running fine.
Please let me know if someone find the error.
Thx!
IN clause accept bind variables only as (:1,:2,:3) , so you have yo know the number of bind variables before hand. Which is not likely.
Simplest way is to form a dynamic query string with hard coded values in Pro*C.
There are alternative solutions from AsKTom and My SO answer
for(i=0;i<5;i++)
{
EXEC SQL select count(1) from MY_TABLE where id IN ( :format[i] );
}
I am telling to use above code as it is too lousy, just explaining how arrays works in Pro*C. You have to give the index of the array.
Edit: I learned a new thing for this problem:- We can also use
EXEC SQL FOR 5
SELECT COUNT(1) FROM MY_TABLE WHERE id IN (:format);
EXEC SQL SELECT COUNT(1) FROM MY_TABLE WHERE id IN
(:format[0],:format[1],:format[2],:format[3],:format[4])

Getting at string data in SQL

I have a row entry with the following format:
Site=[number];this=that;foo=bar;
[number] above can be from 1...infinity. So I need to split out the [number] to use in another select statements where clause. Site=[number] is always at the beginning in the string and the data is always separated by a semi-colon.
declare #t nvarchar(100) = 'Site=230;this=that;foo=bar;';
select convert(int, substring(#t,6, charindex(';',#t,0)-6))
SELECT SUBSTRING(col, 1, CHARINDEX(col,';'))
Why are you storing data in the database in this format? Split it up into columns so that you can do meaningful queries.
You can play with the string this way:
declare #tx as nvarchar(100)
set #tx = 'Site=[number];this=that;foo=bar;'
print substring(
#tx,
CHARINDEX('[', #tx)+1,
CHARINDEX(']',#tx)-CHARINDEX('[',#tx)-1)
Hope this helps.
I don't have MS Sql Server available to try this out, but have you tried something like
Select
field
convert(bigint, substring(field, 6)) as thenum
from
thetable
where
condition=something
where field is the name of the field containing site=[number];...
The theory goes that substring will strip off site= off the beginning, and convert
will (hopefully) convert the number portion and ignore the rest of the text from the semicolon onwards.
It may or may not work. If not you may need to write an elaborate function instead.

How to find if a word has all vowels (SQL Server 2005)

DECLARE #t TABLE(Words VARCHAR(100))
INSERT INTO #t
SELECT 'Stack Overflow' UNION ALL
SELECT 'EQUATORIAL'
SELECT * FROM #t
WHERE Words LIKE '%[AEIOU]%'
I am getting both as the output
Words
Stack Overflow
EQUATORIAL
The desired output being EQUATORIAL
Thanks
I suppose the simplest version would be:
SELECT *
FROM #t
WHERE Words LIKE '%A%'
AND Words LIKE '%E%'
AND Words LIKE '%I%'
AND Words LIKE '%O%'
AND Words LIKE '%U%'
... like %a% and like %e% .... is the only SQL way I know of. Is this homework?
Have you considered a CLR SQL Server function that uses a regular expression?
It is because like %[AEIOU]% is true if the word contains any one of them not all of them see Aaronaught or No Refunds for solution.

Resources