time format and string format - sql

I'm using SQL server to form file names to Ms Word documents. The format is like this
clientnumber_date_time_clientname.doc.
Problem is Ms Word doesn't accept 10:19:23 time format as file name. How to change it into 10-19-23 format?
In some cases clent name is something like this: the "Coca Cola" company. Apparently, Ws Word doesn't accept "(double quotation) mark as file name. I' ve tried to change it into '(single quotation) mark via 'replace' , but it didn't work out. How to change "xxx" into 'xxx'?
Thanks in advance
here is part of that code
convert(varchar,e.ExternalID)+'_'+left(convert(varchar,#Now,120),10) +'uved'+'_'+convert(varchar,a.OwnerName)+'.doc' as NameFile

Try like following code.
DECLARE #CName varchar(100)
DECLARE #CNumber varchar(10)
SET #CNumber ='1234'
SET #CName = '"Coca Cola"'
SELECT #CNumber + '_'
+ REPLACE(REPLACE(CONVERT(VARCHAR(19), GETDATE(), 120),':','-'),' ' ,'_')
+ '_'
+ REPLACE(#CName,'"','''')
+ '.doc'
Run this query and you will get exactly the same filename what you are expecting.

You can use REPLACE as in:
SELECT REPLACE([column],'oldstring','newstring')
FROM [MyTable]

Related

Reverse substring

I am trying to extract some characters from a string.
For Example in (316409953_ND_02142022_000001.pdf) I need the characters after the last underscore _ and before the "."
Answer: 000001
#test = 316409953_ND_02142022_000001.pdf
I have tried:
REVERSE(SUBSTRING(REVERSE(test),CHARINDEX('.',REVERSE(test))+1,CHARINDEX('_',REVERSE(test)+1)))
I need help with the last part of substring. First error I am getting is "Conversion failed when converting the varchar value 'fdp.100000_22024120_DN_359904613' to data type int."
Secondly I need it to pick only after the last "_"
Please guide or let me know if there's another way to do this.
This looks like SQL Server.
On that assumption, and using the single example value, you can try the follolwing:
declare #test varchar(50) = '316409953_ND_02142022_000001.pdf'
select #test OriginalValue,
ParseName(Right(#test, CharIndex('_',Reverse(#test))-1),2) ExtractedValue;
it isn't pretty but it does the job ;P
declare #str varchar(200) = '316409953_ND_02142022_000001.pdf'
declare #tempStr varchar(200)= (select SUBSTRING(#str,LEN(#str) + 2 - CHARINDEX('_',REVERSE(#str)),LEN(#str)))
set #tempStr = SUBSTRING(#tempStr,1,CHARINDEX('.',#tempStr) - 1)
select #tempStr

Replacing a space-looking character in SQL Server

I have a database backup that I restored in SQL Server 2014 and I am using SQL Server Management Studio 18 (15.0.18206.0)
In a column of type text strings of XML files are stored. Yes, I know that the datatype text is obsolete. But I cannot change the datatype in the old program so easily.
Now I want to remove the string <SCHNITTLINIE>.
In the cell, it looks like this: ..._MIN> <SCHNITTLINIE> <S...
SELECT CAST(REPLACE(CAST(xml AS NVARCHAR(MAX)), '<SCHNITTLINIE>', '') AS TEXT)
FROM [dbo].[XMLdatei]
WHERE xml LIKE '%SCHNITTLINIE%'.
As a result, I get: ...MIN> <S...
So far so good.
I'd like to remove the "spaces" as well.
SELECT CAST(REPLACE(CAST(xml AS NVARCHAR(MAX)), ' <SCHNITTLINIE> ', '') AS TEXT)
FROM [dbo].[XMLdatei]
WHERE xml LIKE '%SCHNITTLINIE%'
Now he finds the string ' ' but not anymore.
At the beginning of the cell is a <?xml version="1.0". So I tried the following to test the spaces:
SELECT CAST(REPLACE(CAST(xml AS NVARCHAR(MAX)), 'xml ', 'xxx') AS TEXT)
FROM .[dbo].[XMLdatei].
WHERE xml LIKE '%SCHNITT%'.
As a result I got the following: <?xxxversion="1.0"
The following also works:
SELECT CAST(REPLACE(CAST(xml AS NVARCHAR(MAX)), 'xml' + char(32), 'xxx') AS TEXT)
FROM [dbo].[XMLdatei]
WHERE xml LIKE '%SCHNITT%'
Check, the test was successful. :)
Only with the string ' <SCHNITTLINIE> ' this does not work.
How can I find out what the characters before < or after > are?
How can I remove them without knowing what the characters are?
Thanks for your help.
Have a nice weekend.
Christoph

Search and Replace Byte Order Mark In Sql Server

i have 10 table and more than 10000 record that contain 
how can search  and replace this in DB?
since the  equal 0xEF,0xBB,0xBF how can search this?
i use this code
WITH foo(myvarbincolumn) AS
(
SELECT text from BPM_Letters
)
SELECT *
FROM foo
WHERE CONVERT(VARCHAR(max), myvarbincolumn) COLLATE Arabic_CI_AS
LIKE '%' + CONVERT(NVARCHAR(max), CHAR(0xEF)+CHAR(0xBB)+CHAR(0xBF)) + '%'
I found this code in stackoverflow but it s incomplete.
script of BPM_Letters
this code not find any record!
please help me
I wrote a query to find that weird character via the query below:
SELECT cast(LEFT(text,1) AS VARBINARY(MAX)) from BPM_Letters
and the result was 0xFFFE. So I wrote this query and it worked perfectly:
UPDATE BPM_Letters Set text=REPLACE(text,0xFFFE,'');
What about this CTE:
StripBOM AS
(
SELECT CASE
WHEN LEFT(text,3) = 0xEFBBBF
THEN CONVERT(varbinary(max),SUBSTRING(text, 4, LEN(text)))
ELSE text
END AS text
FROM BPM_Letters
)
It should provide you with a new table where all BOM characters have been stripped off.
P.S. This code assumes 'text' field is of type varbinary.
Here's a simpler answer that builds on the other ones:
UPDATE BPM_Letters SET text=substr(text, 4) WHERE left(text, 3) = 0xEFBBBF;
I've tested this, and it works.

How to remove text followed by a special characted in MSQL

I have a table named 'country' with a column named 'name'
and the names in this column appears with translation followed by special character '/'.
IRELAND/IRLANDE
GREECE/GRÈCE
DENMARK/DANEMARK
Now i want only the countrynames before this special character'/' so the out put should look like this..
IRELAND
GREECE
DENMARK
please help. Thanks in advance
create table #t(name nvarchar(40))
insert into #t values('IRELAND/IRLANDE')
,('GREECE/GRÈCE')
,('DENMARK/DANEMARK')
select substring(name,0,CHARINDEX('/',name)) from #t
Why LEN(names)-CHARINDEX('/',names)?
try using CHARINDEX('/',names)+1 instead.
As far as I remember, SUBSTRING's third variables should be the length of the desired output string.
A more complex query to help you out:
DECLARE #Text VARCHAR(50) = 'IRELAND/IRLANDE GREECE/GRÈCE DENMARK/DANEMARK'
SELECT
SUBSTRING(SUBSTRING(#Text,0,CHARINDEX(' ',#Text,0)),0,CHARINDEX('/',#Text,0))
+
SUBSTRING(SUBSTRING(#Text,CHARINDEX(' ',#Text,0),CHARINDEX(' ',#Text,0)),0,CHARINDEX('/',#Text,0))
+ ' '
+
REPLACE(REVERSE(SUBSTRING(REVERSE(#Text),CHARINDEX('/',#Text,0) + 1 ,CHARINDEX('/',#Text,0))),'/', ' ')

SQL UPPER only specific word

Does anyone know how I can change only a word in the varchar field in my database? They are formatted like "Cheese", "cheese", "CheesE". I want to change to them all to CHEESE, but I have other text in those fields as well. I was wondering if their was a way to single that word out and make it uppper.
Something like
update table
set field = Upper(cheese)+rest of field
Thanks
update table
set field = replace(field, 'cheese', 'CHEESE')
SQLFIddle demo
From your comments I can see the diference between substring and word.
It is possible to do this kind of 'word-centric' transformation using XML as a proxy format.
Try this...
DECLARE #pattern nvarchar(max) = 'cheese'
SELECT
converted =
cast('<a><i>' + REPLACE(field, ' ', '</i><i>') + '</i></a>' as xml)
.query(
'for $x in /a/i return
if (not( ($x) is (/a/i[last()])[1] )) then
if (data($x) = sql:variable("#pattern")) then
concat(upper-case($x), "")
else
concat($x, "")
else
if (data($x) = sql:variable("#pattern")) then
string(upper-case($x))
else
string(data($x))')
.value('.', 'nvarchar(max)')
FROM table
I'm sure that it's possible to do it by parsing the expression char-by-char but I wanted to try it with XML :)