SQL Server : LENGTH() inside of REPLICATE() - sql-server

I have a SQL Server table with the columns Lvl and Title. I need to insert a "-" in front of the title for every character in the Lvl field.
As an example: If Lvl = 111 the title should become --- My Title.
I can only edit the following SQL string. There is no possibility to create other functions or likewise.
SELECT REPLICATE('_', { fn LENGTH(Lvl) }) + ' ' + Title AS Title
FROM Documents
My problem is, that the LENGTH() function doesn't work inside the REPLICATE() function. Does anybody know why or how to solve this problem?
Thank you.

Try this:
SELECT REPLACE(Lvl, '1', '-') + ' ' + Title as Title
FROM Documents
Simply take the Lvl column, and replace all instances of 1 with whatever character you want, then concatenate Title to the end of the result.

Try this. It works fine for me -
select REPLICATE('-',LEN(Lvl)) + ' ' + Title as title from documents

Related

Get the specified input parameter from stored procedure

I want to get all the related information from database. For example I have a summary column which contains data like "A01.1 | Foodland Inbound QIP Details and Pre Unloading" and I want to pass only "A01" in input parameter and get all the data related to "A01"
select * from TableName where summary_column like 'A01%';
Based on the code in your comment, the where clause will only find a summary that is the equal to A01.2 e.g:
TaskMaster.Summary =#summary
You want to use LIKE and a wild card:
WHERE TaskMaster.Summary LIKE '''' + #summary + '%' + ''''
The above will find everything that starts with A01.2, so if you want to find all with A01, pass #summary as A01

How to combine feet and inches into one combined column

Combining last name and first name isn't an issue for me - as shown above. The query spits that out fine. However, in this database schema, inches and feet are separated. When I try to combine them as height_feet + ' ' ' + height_inches, I get an error for obvious reasons. What's the best way to combine feet and inches, so that it reads as 6'7 for instance.
I've tried
,height_feet + ''' + height_inches as 'Height'
I've also tried
,concat(cast(height_feet as char), ''' ', cast(height_inches as char), '"')
Neither have helped me get the desired result. All I want is it to read 5'10 if feet is 5 and inches is 10.
SELECT TOP 10
last_name + ', ' + first_name as 'Employee'
,days as 'Days Employed'
,height_feet
,height_inches
,ROUND(sales, 3) as 'Sales'
The right function to do concatenation in MySQL is CONCAT, therefore use it for all your concatenations.
Your problem is just because you are trying to concatenate a reserved char '. In order to concatenate a single quote you need to escape it so you double it.
SELECT
concat(last_name, ', ', first_name) as 'Employee'
,days as 'Days Employed'
,concat(height_feet, '''',height_inches)
,ROUND(sales, 3) as 'Sales'
from ...
where ...
LIMIT 10 -- "TOP 10" is MSSQL syntax
See an example here: http://sqlfiddle.com/#!9/9eecb/102692
EDIT
I just noticed that you may have tagged your question with MySQL when it should be SQL SERVER. Nevertheless since SQL Server has the same function CONCAT the same query will work, see the same example running here: http://sqlfiddle.com/#!18/9eecb/54309

How to avoid data repetition insertion?

Recently I have posted a question, it contains some syntax error, now the code is running without error, thanks to #Arulkumar.
But now I am facing one more problem, data from excel sheet is storing properly on to SQL Server database, but when I press refresh button or if I go to that link again in my application, data is repeating in the database. Means again it is retrieving values from excel and storing same data again in the database.
How can I avoid data repetition. Can any one please help me to solve this issue? Code and excel sheet sample is there in the above mentioned link.
You need MERGE statement
request.query('MERGE [mytable] as target USING (SELECT SalesPersonID, TerritoryID FROM OPENROWSET(' +
'\'Microsoft.ACE.OLEDB.12.0\', \'Excel 12.0;Database=D:\\sample\\test\\data\\1540_OPENROWSET_Examples.xls;HDR=YES\', ' +
'\'SELECT SalesPersonID, TerritoryID FROM [SELECT_Example$]\')' +
' ) as source' +
' ON target.SalesPersonID = source.SalesPersonID' +
' WHEN MATCHED THEN UPDATE SET TerritoryID = source.TerritoryID' +
' WHEN NOT MATCHED THEN INSERT (SalesPersonID, TerritoryID) VALUES (source.SalesPersonID, source.TerritoryID);'
,function(err,recordset){
if(err) console.log(err)
It will update TerritoryID if there is already row with same SalesPersonID and insert row if there is no matches in mytable.
If you need join on both fields change this:
ON target.SalesPersonID = source.SalesPersonID
On this:
ON target.SalesPersonID = source.SalesPersonID AND target.TerritoryID = source.TerritoryID
And after that - remove this string because it doesn't need anymore:
'WHEN MATCHED THEN UPDATE SET TerritoryID = source.TerritoryID' +

Conditionally count based on other field data in SSRS Report

I am working SSRS report, I have a field named as Details and I would like to get the count of other field SerialNumber.
So in short I want to get the total count of SerialNumber which has no Details.
I tried below but not working. It always give the total count count without considering blank Details
=CStr(COUNT(IIF(Not(IsNothing(Fields!Details.Value)),Fields!SerialNumber.Value,0)))
How can I achieve this by expression? Please help.
Issue was with my data. In dataset I was fetching rows based on Isnull() like below
SELECT CASE
WHEN 'Y'='Y' THEN ISNULL(Code ,'') + ' ' + ISNULL(Description ,'')
ELSE CASE
WHEN ISNULL(Code ,'')='' THEN ISNULL(RefCharge ,'')
+' '+ISNULL(RefDescription ,'')
ELSE ISNULL(Code ,'')+' '+ISNULL(Description ,'')
END
END AS Details
So it was showing (3 blank spaces) and my expression was.
=COUNT(IIF(Fields!Details.Value<>"",Fields!Number.Value,Nothing))
Finally I tried below, now it's working fine now.
=COUNT(IIF(Trim(Fields!Details.Value)<>"",Fields!Number.Value,Nothing))
Note: So I have noted that whenever we need to check such kind of conditions we must have to use Trim()

SQL Server - remove HTML Tag from a varchar

I know there are a few about similar topics on here but I can't find one related to my issue, this is it:
I have a table with an ID Column and a QRCode column. each time an item is added the primary key auto increments. The QRCode will scan in to be like the following:
"http://somewebsite.com/12345/987654321"
i want to be able to remove the "http://somewebsite.com/" from the string, I know how to do this in C# however I am unsure of how to do this in Sql Server. any guidance would be great, thanks
Regular formats are like the following, and used in the example below.
"http://somewebsite.com/12345/456564654"
"http://somewebsite.com/12345/989886765"
"http://somewebsite.com/12346/987654321"
the query returns the following results:
SELECT
REPLACE
(
REPLACE(QRCode, 'http://somewebsite.com/', '')
,'/', ' '
) AS QRCode
FROM
QRTable
WHERE
QRCode LIKE '%http://somewebsite.com/%'
"12345 456564654"
"12345 989886765"
"12346 987654321"
Now i need to update the table with those new results however as there's 3 results, i get the error message "Subquery returned more than 1 value". is there a way to replace the selected values in the table with the ones that exist based on the primary key field?
**Removed previous example
A more complete answer based on your updated question. This removes the first portion of the URL as well as the trailing / so that you get your desired output.
DECLARE #Variable VARCHAR(50)
SET #Variable = 'http://somewebsite.com/12345/456564654'
SET #Variable =
REPLACE
(
REPLACE(#Variable, 'http://somewebsite.com/', '')
,'/', ' '
)
PRINT #Variable
Output = 12345 456564654
Looking at your SQL statement you want this:
SELECT
REPLACE
(
REPLACE(QRCode, 'http://somewebsite.com/', '')
,'/', ' '
) AS QRCode
FROM
QRTable
WHERE
QRCode LIKE '%http://somewebsite.com/%'

Resources