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
Related
I have converted all of my SQL Server commands to ACCESS except one. Here is the code in SQL Server Syntax that I cannot figure out which commands to use. The Code takes an hour value stored as a string of three or four characters, e.g., 25:00, 2:30, 09:20, etc. and converts them to a number equivalent, e.g., 25, 2.5, 9.33333, etc. Can someone provide me with the correct commands to use in place of convert, float, and substring? Here is my Code:
(
(
iif(LEN(dbo_t_MakerProcesses.ProcessTarget) = 5
AND
SUBSTRING(dbo_t_MakerProcesses.ProcessTarget,3,1) = ':',
convert(float, substring(dbo_t_MakerProcesses.ProcessTarget, 1, 2)
+ '.'
+substring(dbo_t_MakerProcesses.ProcessTarget, 4, 2)),0
)
-Round(iif(LEN(dbo_t_MakerProcesses.ProcessTarget) = 5
AND SUBSTRING(dbo_t_MakerProcesses.ProcessTarget,3,1) = ':',
convert(float,substring(dbo_t_MakerProcesses.ProcessTarget, 1, 2)
+ '.'+substring(dbo_t_MakerProcesses.ProcessTarget,4,2)),
0),0,1
)
)/.6
)+
Round(
iif(
LEN(dbo_t_MakerProcesses.ProcessTarget) = 5
AND
SUBSTRING(dbo_t_MakerProcesses.ProcessTarget,3,1) = ':',
convert(float,substring(dbo_t_MakerProcesses.ProcessTarget, 1, 2)
+ '.'
+substring(dbo_t_MakerProcesses.ProcessTarget, 4, 2)
),0
),0,1
) AS Target
Convert are as follows (you may need to look up more):
Cdbl()
CInt()
CStr()
CLng()
Float :
Double
And SubString:
MID( text, start_position, [number_of_characters] )
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?
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;
I'm trying use SSIS/SSDT to sync a PostgreSQL source view into a SQL Server target table. I have the initial sync working after a lot of trail n error.
The issue I'm having is I want to use an out loop to limit the scope of the Data Flow to sync on day at the a time.
I have the loop increasing the date by 1 fine.
The expression I'm using for the ODBC Source (PostgreSQL) is as follows
"SELECT * from usage r
where
start_time >= '" + ((DT_STR, 4, 1252) DATEPART("yyyy", #[User::PeriodStart]) + "-" + (DT_STR, 2, 1252) DATEPART("mm", #[User::PeriodStart]) + "-" + (DT_STR, 2, 1252) DATEPART("dd", #[User::PeriodStart]))
+ "'
and start_time < ('" + ((DT_STR, 4, 1252) DATEPART("yyyy", #[User::PeriodStart]) + "-" + (DT_STR, 2, 1252) DATEPART("mm", #[User::PeriodStart]) + "-" + (DT_STR, 2, 1252) DATEPART("dd", #[User::PeriodStart]))
+ "'::date + INTERVAL '1 day')
and r.chargeable=true
AND r.start_time > '2000-01-01T12:00:00'
order by r.start_time asc"
The #[User::PeriodStart] is only evaluated once and isn't updated each time its loops.
I'm sure this is simple but my google skills with SSIS is failing
Any help would be much appreciated
It fixed it self. not sure what I did.
I am not a SQL expert so please forgive me if this is SQL 101 :).
In a select statement there are 2 replace functions. They look for a Servername and it's admin share d$ by it's UNC path. Example '\SERVERNAME\d$'
It then replaces '\SERVERNAME\d$' with 'D:'.
Here is the query currently:
select Replace(p.Path,'\\SERVERNAME\d$','D:') as searchpath
,p.path as fullpath
,s.ShareName
,s.SharePath
,p.Member
,p.Access
From Paths As p
Left Outer Join Shares as s on
Replace(p.Path,'\\SERVERNAME\d$','D:') Like s.SharePath + '\%'
Up until now it has always been d$.
Today my needs have changed and I need the query to find ANY servername UNC path admin share regardless of share letter (c$, d$, e$, f$...etc) and replace it with it's respective drive letter (D:, E:, F:... etc).
My thought is replace function could find the $ and look one character to the left of it to get the proper share letter, then use that for the replace. The issue I have, not being a SQL professional, is that I know SQL can likley do what I need it to do...I just don't know how to get there. I've googled and found some examples, but haven't had any luck in getting them to work.
Any help would be greatly appreciated.
You can use a combination of STUFF, PATINDEX, LEN to get what you want.
Sample Query
DECLARE #ReplaceChar VARCHAR(100) = '[prefixcharacters]\\SERVERNAME\d$[postcharacter]'
DECLARE #SearchString VARCHAR(100) = '\\SERVERNAME\_$'
SELECT
STUFF(#ReplaceChar,PATINDEX('%' + #SearchString + '%',#ReplaceChar),LEN(#SearchString),
UPPER(SUBSTRING(#ReplaceChar,PATINDEX('%' + #SearchString + '%',#ReplaceChar) + LEN(#SearchString) - 2,1)) + ':') as searchpath
WHERE PATINDEX('%' + #SearchString + '%',#ReplaceChar) > 0
Output
[prefixcharacters]D:[postcharacter]
Alternate Query
You can shorten the query if you want to get the previous character before $ as per your title. Something like this
DECLARE #ReplaceChar VARCHAR(100) = '[prefixcharacters]\\SERVERNAME\d$[postcharacter]'
DECLARE #SearchString VARCHAR(100) = '\\SERVERNAME\_$'
SELECT
STUFF(#ReplaceChar,
PATINDEX('%'+#SearchString+'%',#ReplaceChar),
LEN(#SearchString),
UPPER(SUBSTRING(#ReplaceChar,CHARINDEX('$',#ReplaceChar) -1,1)) + ':')
WHERE PATINDEX('%'+#SearchString+'%',#ReplaceChar) > 0
In this query
STUFF replaces your pattern with with the character before $ + ':'
Start of pattern is identified by PATINDEX('%'+#SearchString+'%',#ReplaceChar)
D is identified by getting the charindex of '$' and then getting the previous character using SUBSTRING
What about ΒΈ
select Replace(SUBSTRING(p.path, 14, Len(#spath)-14),'$',':') as searchpath
,p.path as fullpath
,s.ShareName
,s.SharePath
,p.Member
,p.Access
From Paths As p
Left Outer Join Shares as s on
Replace(SUBSTRING(p.path, 14, Len(#spath)-14),'$',':') Like s.SharePath + '\%
select as searchpath
DECLARE #str nvarchar (100)
SET #str = '\\SERVERNAME\d$'
IF #str LIKE '\\SERVERNAME\_$'
SET #str = UPPER(SUBSTRING(#str, 14, 1)) + ':'
SELECT #str
Starting from previous, something like
select UPPER(SUBSTRING(p.path, 14, 1)) + ':' as searchpath
,p.path as fullpath
,s.ShareName
,s.SharePath
,p.Member
,p.Access
From Paths As p
Left Outer Join Shares as s on
SUBSTRING(p.path, 14, 1) + ':' Like s.SharePath + '\%'
I am no mysql expert either :)
Based on the logic you mentioned in the last part of the question, I have used concat and substring to get to the drive letter in the column.
Hope this helps
select replace(path, concat(substring(path, 1, locate('$', path) - 2), substring(path, locate('$', path) - 1, 1) , '$'), concat(substring(path, locate('$', path) - 1, 1) , ':')) as searchpath ...
The remaining part of the query would be the same.