How to fix error when updating phone number format? - sql-server

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?

Related

How to convert a timestamp string into datetime in sql server?

I was doing some data-check for 1-2 months old data in my sql server and a question rised in my mind.
I made a string 926084711140 and 20210926084711140 which I am considering to be a date-time stamp.
I thought of converting into actual datetime value. So I tried this:
'2021-' + SUBSTRING(CAST(th.edit_timestamp as varchar(50)),1,1) + '-' + SUBSTRING(CAST(th.edit_timestamp as varchar(50)),2,2)
And this returned a value 2021-09-.2 I am unable to understand this output.
I thought of doing substring but so far it failed.
Can anyone help regarding this? Any help would be appreciated.
Thanks!
As others have stated - you should fix your data model, but here is an alternative method:
Select cast(stuff(stuff(stuff(stuff(#date, 15, 0, '.'), 13, 0, ':'), 11, 0, ':'), 9, 0, ' ') as datetime)
And if you really have the first date format without the year you can do this:
Select cast(stuff(stuff(stuff(stuff(concat('2021', right(concat('0', #date), 13)), 15, 0, '.'), 13, 0, ':'), 11, 0, ':'), 9, 0, ' ') as datetime)
The first thing I'm going to tell you is: don't use this format. You have to fix the data input, don't try to fix the data output
But if there is no other way ...
I'm going to assume that your format is
aaaa-mm-ddThh:mi:ss.mmm
We can do something like this
select convert(datetime,'2021-09-26T08:47:11.140',126)
OUTPUT: 2021-09-26 08:47:11.140
declare #date varchar(50) = '20210926084711140'
select convert(datetime,substring(#date,1,4) + '-' + substring(#date,5,2) + '-'
+ substring(#date,7,2) + 'T' + substring(#date,9,2) + ':' +
substring(#date,11,2) + ':' + substring(#date,13,2) + '.' +
substring(#date,15,3),126)
one more time ... this is horrible.
try to fix your data source and not work with this.
Good luck

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.

MS Access mid equivalent in 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;

SQL Server : get part of string between 'ServicePassword=' and '&' in a url string

I'm trying to get the service password in a url stored in a table.
Example:
Parameters=http://google.com?ServiceUN=testUN&ServicePW=testPW&RequestType=testrequestType&MemberCode=TestMemberCode
This is where I'm at so far, the 9 in the substring function is only to use as a test length, that's the part I'm having issues with getting.
SELECT
SUBSTRING(Parameters,
CHARINDEX('ServicePW=', Parameters) + LEN('ServicePW='), 9)
FROM Table
WHERE TestID = 8
SELECT
substring(
Parameters,
charindex('ServicePW=', Parameters) + 10, -- len('ServicePW=')
charindex('&', substring(Parameters, charindex('ServicePW=', Parameters), len(Parameters))) - 11 -- (- len('ServicePW=') - 1)
)
FROM
Table
WHERE
TestID = 8
SELECT SUBSTRING(SUBSTRING(Parameters, PATINDEX('%ServicePW=%', Parameters), LEN(Parameters)), 11, PATINDEX('%&%', SUBSTRING(Parameters, PATINDEX('%ServicePW=%', Parameters), LEN(Parameters))) - 11)
FROM Table where TestID = 8

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.

Resources