MS Access mid equivalent in SQL Server - sql-server

I have the following SQL statement; the problem is that the MID function is not recognized. Is there an equivalent to MID in SQL Server? Thanks
SQL:
SELECT
tblHR_Employees.ADLoginID AS bar,
Replace(LCase(Mid([LikesToBeCalled], 1, 1) & [lastname]), ' ', '') AS foo
FROM
[STONE_DB].[dbo].[tblHR_Employees]
WHERE
tblHR_Employees.ADLoginID IS NULL
AND tblHR_Employees.SeparationDate IS NULL;
Returns:
Msg 195, Level 15, State 10, Line 2
'Mid' is not a recognized built-in function name.

substring() would be the equivalent -- and lower() would be the equivalent of LCase()

SELECT
tblHR_Employees.ADLoginID AS bar,
Replace(LOWER(SUBSTRING([LikesToBeCalled], 1, 1) & [lastname]), ' ', '') AS foo
FROM
[STONE_DB].[dbo].[tblHR_Employees]
WHERE
tblHR_Employees.ADLoginID IS NULL
AND tblHR_Employees.SeparationDate IS NULL;

Related

Update SQL Server table using LEFT

I'm trying to update a table in sql server using the below command but I get the following error:
Msg 156, Level 15, State 1, Line 716
Incorrect syntax near the keyword 'LEFT'
Is LEFT not allowed when updating? What can be used instead? Thanks
UPDATE DI.DBO.MHS
SET (LEFT(BATCH_DATE_2, 1) + '0' + RIGHT(BATCH_DATE_2, 6))
WHERE LEFT(BATCH_DATE_2, 1) = 2
You must specify the column that you want to update:
UPDATE DI.DBO.MHS
SET BATCH_DATE_2 = LEFT(BATCH_DATE_2,1) + '0' + RIGHT(BATCH_DATE_2,6)
WHERE LEFT(BATCH_DATE_2,1) = 2
If it is not BATCH_DATE_2 the column that you want to update then use that column after SET.

Invalid length parameter passed to the LEFT or SUBSTING function

Declare #FileNumber int
Set #FileNumber = cast (SubString(#fileName, CharIndex('Stats', #fileName) + 6, charindex('.',#fileName) - (CharIndex('Stats', #fileName) + 6)) as int)
I'm passing in '' for the #fileName and getting this error. This is in SQL Server 2016
Because ...
select SubString('', 6 , - 6)
Returnes ...
Msg 536, Level 16, State 1, Line 7
Invalid length parameter passed to the substring function.
https://learn.microsoft.com/en-us/sql/t-sql/functions/substring-transact-sql

How to fix error when updating phone number format?

Ok, I am using SQL Server 2016 and I am trying to figure out how to update the phone numbers from the Customers table. Which look like this "1234567890", and when I run this query:
USE ProductsDatabase2;
UPDATE Customers
SET PhoneNumber = '(' + SUBSTRING(PhoneNumber, 1, 3) + ') ' +
SUBSTRING(PhoneNumber, 4, 3) + '-' + SUBSTRING(PhoneNumber, 7, 4)
I get this error message:
Msg 8152, Level 16, State 13, Line 3
String or binary data would be truncated.
The statement has been terminated.
Here is the Customers table:
How do I fix this problem?

SSIS SQL statement error

I created an SSIS package with source being a SQL query, destination being a flat file.
The SQL statement executes properly in SQL Server Management Studio, however it errors out when run from SSIS.
The statement is:
use Echo_active
select
DATEFROMPARTS(year(getdate()) - 1, month(getdate()) + 2, 1) as StartDate,
eomonth(DATEFROMPARTS(year(getdate()) - 1, month(getdate()) + 4, 1) ) as EndDate,
pd.[PA-PT-NO-WOSCD],
pd.[PA-PT-NO-SCD],
REPLACE(di.[PA-DX2-CODE],'.','') DiagCode,
REPLACE(STR(di.[PA-DX2-PRIO-NO], 2, 0),' ', 0) DiagSeqNo,
di.[PA-DX2-PRESENT-ON-ADM-IND] POA,
di.[PA-DX2-CODING-SYS-IND] DiagVersion
from
patientdemographics pd
left join
DiagnosisInformation di on pd.[PA-REGION-CD] = di.[PA-REGION-CD]
and pd.[PA-HOSP-CD] = di.[PA-HOSP-CD]
and pd.[PA-PT-NO-WOSCD] = di.[PA-PT-NO-WOSCD]
where
[pa-dsch-date] > = DATEFROMPARTS(year(getdate()) -1, month(getdate()) + 2, 1)
and [pa-dsch-date] <= eomonth(DATEFROMPARTS(year(getdate()) - 1, month(getdate()) + 4, 1))
and di.[PA-DX2-PRESENT-ON-ADM-IND] <> ''
order by
di.[PA-DX2-CODE]
The error message I am receiving is :
[Execute SQL Task] Error: Executing the query "
use Echo_active
select
DATEFROMPARTS(year(getda..." failed with the following error: "Cannot construct data type date, some of the arguments have values which are not valid.".
Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
How could the same statement execute in one place but errors out in the other?
Any suggestions would be greatly appreciated.

Get a Substring in SQL between two characters and remove all white spaces

I've a strings such as:
Games/Maps/MapsLevel1/Level 1.swf
Games/AnimalWorld/Animal1.1/Level 1.1.swf
Games/patterns and spatial understanding/Level 13.5/Level 13.5.swf
I want to get only file name without its extension(String After last Slash and before Last dot), i.e Level 1 and Level 1.1 and Level 13.5, Even I want to remove all the white spaces and the final string should be in lower case i.e the final output should be
level1
level1.1
level13.5 and so on..
I tried following query but i got Level 1.swf, How do i change this Query?
SELECT SUBSTRING(vchServerPath, LEN(vchServerPath) - CHARINDEX('/', REVERSE(vchServerPath)) + 2, LEN(vchServerPath)) FROM Games
SELECT (left((Path), LEN(Path) - charindex('.', reverse(Path))))
FROM
(
SELECT SUBSTRING(vchServerPath,
LEN(vchServerPath) - CHARINDEX('/', REVERSE(vchServerPath)) + 2,
LEN(vchServerPath)) Path
FROM Games
) A
This would work, I kept your inner substring which got you part way and I added the stripping of the dot.
I have included a sql fiddle link for you to see it in action sql fiddle
Edited:
Following will remove the white space and returns lower case...
SELECT REPLACE(LOWER((left((Path), LEN(Path) - charindex('.', reverse(Path))))), ' ', '')
FROM
(
SELECT SUBSTRING(vchServerPath,
LEN(vchServerPath) - CHARINDEX('/', REVERSE(vchServerPath)) + 2,
LEN(vchServerPath)) Path
FROM Games
) A
Try this:
select
case
when vchServerPath is not null
then reverse(replace(substring(reverse(vchServerPath),charindex('.',reverse(vchServerPath))+1, charindex('/',reverse(vchServerPath))-(charindex('.',reverse(vchServerPath))+1)),' ',''))
else ''
end
This should work fine; with extension removed.
select
REVERSE(
SUBSTRING(
reverse('Games/patterns and spatial understanding/Level 13.5/Level 13.5.swf'),
5,
(charindex('/',
reverse('Games/patterns and spatial understanding/Level 13.5/Level 13.5.swf')) - 5)
))

Resources