Saving sql query that contains arabic into a cell - sql-server

declare #val nvarchar(max),#H_ARABIC nvarchar(max)
select #val = 'select [settings_key] as N''اسم'' from [settings]'
set #H_ARABIC= #val;
print #H_ARABIC
It is showing results as
select [settings_key] as N'???' from [application_setting] but how can I get this result
select [settings_key] as N'اسم' from [application_setting] I have tried many ways by changing the quotation mark but no use. Pls help

Prefix your string literal with N.
declare #val nvarchar(max),#H_ARABIC nvarchar(max)
select #val = N'select [settings_key] as N''اسم'' from [settings]'
set #H_ARABIC= #val;
print #H_ARABIC

Related

SQL Server Stored Procedure insert query with given parameters

first of all, sorry for my English,
I am a newbie in stored procedure, so i'm seek for a help on it.
I have a project that need me to create a SP for a configurable table name and column name. I've manage to pass the table name and column name value from vb/vb.net and now i'm stuck on SP, below are sample of my code.
example :
frmTblname = table_a
frmClmnName = clm_A1, clm_A2, clm_A3, clm_A4, clm_A5,
toTblName = table_b,
toClmnName = clmn_b1,clmn_b2, clmn_b3, clmn_b4, clmn_b5
from vb/vb.net Rslt = ConnectionExec.RunSP(con, "sp_configurable_insert", frmTblname, frmClmnName, toTblName, toClmnName)
how to add that into SQL insert query?
Here are my SP
CREATE procedure [dbo].[sp_configurable_insert] #fromTable nvarchar(50),#fromColumn nvarchar(4000),#toTable nvarchar(50),#toColumn nvarchar(4000)
I've tried this but it seems not giving any result.
set #Query1 = 'insert into '+#toTable+'('+quotename(#toColumn)+')
select top 20 '+#fromColumn+'
from '+#fromTable+'
can anyone help me, please?
Thanks :)
Things to change:
quotename(#toColumn) will return '[clmn_b1,clmn_b2, clmn_b3, clmn_b4, clmn_b5]' which is not correct. Remove quotename() or set #toColumn = '[clmn_b1], [clmn_b2], [clmn_b3], [clmn_b4], [clmn_b5]'.
your query statement is unfinished.
Stored procedure (with correct syntax):
CREATE procedure [dbo].[sp_configurable_insert]
#fromTable nvarchar(50),
#fromColumn nvarchar(4000),
#toTable nvarchar(50),
#toColumn nvarchar(4000)
AS
BEGIN
DECLARE
#stm nvarchar(max),
#err int
SET #stm =
N'insert into '+#toTable+' ('+#toColumn+') select top 20 '+#fromColumn+' from '+#fromTable
EXEC #err = sp_executesql #stm
IF #err <> 0 BEGIN
RETURN #err
END
RETURN 0
END
you need to add EXECUTE(#Query1) to your SP and it will work
Add at the end of your query:
exec sp_executesql #Query1
So it should look like:
set #Query1 = 'insert into '+#toTable+'('+quotename(#toColumn)+')
select top 20 '+#fromColumn+'
from '+#fromTable+'
exec sp_executesql #Query1
So pay attention that #Query1 is incomplete or contains error coz quotes is unbalanced.

Substring select string between two characters

The following code
declare #text VARCHAR(MAX)
set #text='[Dim Company].[Company].[23]'
SELECT SUBSTRING(#Text, CHARINDEX('[Dim Company].[Company].[', #Text)
, CHARINDEX(']',#text) - CHARINDEX('[Dim Company].[Company].[', #Text) + Len(']'))
Returns [Dim Company]. I was expecting it to return the integer between the last [] -- in this case 23. How can I get the desired field?
If you know it's always last, why not REVERSE the string, find the value between the first pair of brackets, then REVERSE that result?
Much easier than nesting CHARINDEX calls.
This will give you 23]
SELECT RIGHT(#text, CHARINDEX('[',REVERSE(#text))-1)
This will give you 23
SET #tmp = RIGHT(#text, CHARINDEX('[',REVERSE(#text))-1)
SET #tmp = LEFT(#tmp, LEN(tmp)-1)
fiddle : http://sqlfiddle.com/#!3/d41d8/36013
I agree with mwigdahl, this is bad way of doing things, but this is the answer you seek:
declare #text VARCHAR(MAX)
set #text='[Dim Company].[Company].[23]'
SELECT SUBSTRING(#Text,
LEN('[Dim Company].[Company].[')+1,
LEN(#text)-LEN('[Dim Company].[Company].[')-1)
A better approach is to use the PARSENAME() function. The function's purpose is actually to parse an object name, such as server.schema.table, but since your string looks just like that, we can hijack the function. It's far more efficient, and cleaner than charindex/substring.
declare #text varchar(max), #p1 varchar(max), #p2 varchar(max), #p3 varchar(max)
set #text='[Dim Company].[Company].[23]'
set #p1 = PARSENAME(#text, 3)
set #p2 = PARSENAME(#text, 2)
set #p3 = PARSENAME(#text, 1)
select #p1, #p2, #p3

Special character not showing up in select query in SQL Server

I have an question about special characters not showing up in the query
DECLARE #varName1 NVARCHAR(500);
set #varName1 = 'ÜCŞKUçÖ'
select '''' + #varName1 + ''' As Name1'
These are the Turkish characters. When I run the above query I get this character as S but it should come as Ş
In the database I have this column value as ÜCŞKUçÖ
How can I solve the issue?
You're not actually storing it as a Unicode value...
SET #varname1 = N'ÜCŞKUçÖ';
SELECT #varname1;
DECLARE #varName1 NVARCHAR(500);
set #varName1 = N'ÜCŞKUçÖ'
select #varName1 As Name
For more in details http://technet.microsoft.com/en-us/library/ms180059.aspx

Get column value from dynamically achieved column name

Below is the code where I am getting a columns name dynamically(example F8 is the column name) and storing it in #whcode. What i need is the value that is stored in this column for the where condition specified below. I can see the value stored in the column is 2 but I cannot get it. What it is returning me is the column name itself. How can I get the value in the column. Please help.
declare #count1 int
set #count1 = (select min(srno) from TEMP_STOCK_uPDATE)
declare #whcode varchar(20)
select * from TEMP_STOCK_uPDATE where srno='16091'
set #whcode=(SELECT COLUMN_NAME
FROM Ecata_New.INFORMATION_SCHEMA.COLUMNS
where Table_Name = 'TEMP_STOCK_uPDATE'
and COLUMN_NAME =(select whcode from dbo.temp_stock_map where func=(select func from dbo.temp_stock_map where sr_no=6)))
--select #whcode as 'abcd'
select #whcode as 'abc'
from TEMP_STOCK_uPDATE
where
F1=(select F1 from dbo.TEMP_STOCK_uPDATE where srno=#count1)
You can build your SQL statement dynamically to do this. Build your statement in a varchar and then EXEC it.
DECLARE #SQL VARCHAR(4000)
SET #SQL = 'select ' + #whcode + ' from TEMP_STOCK_uPDATE where F1=(select F1 from dbo.TEMP_STOCK_uPDATE where srno=#count1)'
EXEC (#SQL)

Mistake in Sql query while splitting string

What mistake I am making in this sql query
Declare #str Varchar(100) = 'asasa,bsasas,csasa,dsczxvvc'
declare #d varchar(2)=','
SELECT
RIGHT(LEFT(#str,Number-1),
CHARINDEX(#d,REVERSE(LEFT(#d+#str,Number-1))))
FROM
master..spt_values
WHERE
Type = 'P' AND Number BETWEEN 1 AND LEN(#str)
AND SUBSTRING(#str,Number,1) = #d
Expected Result
(No column name)
asasa
bsasas
csasa
dsczxvvc
Actual Result
(No column name)
asasa
bsasas
csasa
Your AND SUBSTRING(#str, Number, 1) = #d is forcing a comma to be at the end of dsczxvvc...there isn't one. asasa,bsasas,csasa,dsczxvvc, works.
add one more comma at the end of string
you can directly add comma in the string like = "asasa,bsasas,csasa,dsczxvvc,"
or
handle this thing in sql side.
Declare #str Varchar(100)
set #str = 'asasa,bsasas,csasa,dsczxvvc'
declare #d varchar(2)
set #d =','
set #str = #str + ','
SELECT
RIGHT(LEFT(#str,Number-1),
CHARINDEX(#d,REVERSE(LEFT(#d+#str,Number-1))))
FROM
master..spt_values
WHERE
Type = 'P' AND Number BETWEEN 1 AND LEN(#str)
AND SUBSTRING(#str,Number,1) = #d
This is because since the last string portion does not have the seperator at the end of the string
If you add the following code just before the SELECT statement, it will work
set #str = #str + #d
There are many split functions on the web, you can use one of them actually.
Split string using CLR function
Split using XML
SQL Server split string function

Resources