select replace(substring(CONVERT(varchar,SWITCHOFFSET(SYSDATETIMEOFFSET(), '+05:30'),100),13,
LEN(CONVERT(varchar,SYSDATETIMEOFFSET(),100))), ' +05:30','')
I am getting output=> 4:06PM
but I want output=> 4:06 PM
how can I get this output??
Use this SELECT replace(substring(STUFF(CONVERT(varchar,SWITCHOFFSET(SYSDATETIMEOFFSET(), '+05:30'),100), 18,0,' '),13,LEN(CONVERT(varchar,SYSDATETIMEOFFSET(),100))), ' +05:30','');
instead of
select replace(substring(CONVERT(varchar,SWITCHOFFSET(SYSDATETIMEOFFSET(), '+05:30 '),100),13,
LEN(CONVERT(varchar,SYSDATETIMEOFFSET(),100))),' +05:30',' ')
select replace(substring(CONVERT(varchar,SWITCHOFFSET(SYSDATETIMEOFFSET(), '+05:30 '),100),13,
LEN(CONVERT(varchar,SYSDATETIMEOFFSET(),100))),' +05:30',' ')
You can use stuff
select STUFF(replace(substring(CONVERT(varchar(50),SWITCHOFFSET(SYSDATETIMEOFFSET(), '+05:30'),100),13, LEN(CONVERT(varchar,SYSDATETIMEOFFSET(),100))), ' +05:30',''),6,0,' ')
It will stuff a space at the 6th position replacing 0 characters.
Below query will give you the expected output.
SELECT replace(substring(STUFF(CONVERT(varchar,SWITCHOFFSET(SYSDATETIMEOFFSET(), '+05:30'),100), 18, 0, ' '),13,LEN(CONVERT(varchar,SYSDATETIMEOFFSET(),100))), ' +05:30','');
output: 4:22 PM
Related
I'm trying to check whether or not a string ends in a year.
Input:
file_paths
wowefnowinf/wefionwe/wefowoi/2012-02-03
weofnweofn/weoew/2022-03-04
ewpfowe/ewopfew
Desired Output:
wowefnowinf/wefionwe/wefowoi/
weofnweofn/weoew/
ewpfowe/ewopfew
I'm having trouble first detecting that the strings themselves end in a date-format. This is my query:
SELECT CASE WHEN 'wowefnowinf/wefionwe/wefowoi/2012-02-03' LIKE '%/####\-##\-##'
THEN TRUE
ELSE FALSE END AS result
FROM my_table;
I should be getting true for this, but my query returns false. Any help would be appreciated.
In Snowflake you can make use of regexp_like and split_part:
with dummy as (
select 'wowefnowinf/wefionwe/wefowoi/2012-02-03' as file_path
union all
select 'weofnweofn/weoew/2022-03-04'
union all
select 'ewpfowe/ewopfew'
)
select
file_path,
split_part(file_path, '/', -1) as splitted_file_path,
regexp_like(splitted_file_path, '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]') as ends_with_date,
iff(ends_with_date, trim(file_path, splitted_file_path), file_path) as trimmed_file_path
from dummy;
Output:
We have a set of databases (80 in total). Every single one has a table called tblProfessions. The tables are not standardized. For example:
EDIT: all the databases are on the same server.
The DB1.dbo.tblProfessions is like:
intProfessionCode
strProfessionDescription
1
lawyer
2
dentist
...
...
30
doctor
And the DB72.dbo.tblProfessions is as follows:
intProfessionCode
strProfessionDescription
1
designer
2
butcher
...
...
80
chef
Suppose I ran a script from DBO1 to DBO72, and I found that the biggest table has 80 entries (in this case the DBO72 is the biggest one).
By my limited knowledge, all I know is to run the below script database by database, and write it down in a spreadsheet manually:
SELECT MAX(intProfessionCode) FROM [DB].dbo.tblProfessions;
Is there a script to run and loop through all the tblProfessions and get the one with the most entries? All I want is the biggest number found.
Thanks in advance.
You should be able to do something like this:
WITH dat
AS
(
SELECT 'db1' AS database_name, MAX(intProfessionCode) AS max_intProfessionCode
FROM DB1.dbo.tblProfessions
UNION ALL
...
UNION ALL
SELECT 'db72' AS database_name, MAX(intProfessionCode) AS max_intProfessionCode
FROM DB72.dbo.tblProfessions
)
SELECT dat.db, dat.max_intProfessionCode
FROM dat
INNER JOIN (SELECT MAX(max_intProfessionCode) AS max_intProfessionCode_overall
FROM dat) y
ON dat.max_intProfessionCode = y.max_intProfessionCode_overall
For situations like this, I usually query the catalog to write the above script rather than typing it out:
WITH
dat AS
(
SELECT STRING_AGG('SELECT ''' + QUOTENAME(s.name) + ''' AS db,
MAX(intProfessionCode) AS max_intProfessionCode
FROM ' + QUOTENAME(s.name) + '.' + QUOTENAME('dbo') + '.' + QUOTENAME('tblProfessions') + '
UNION ALL',' ') AS s
FROM sys.databases s
WHERE s.name LIKE 'DB%' --EDIT APPROPRIATELY
)
SELECT 'WITH dat
AS
(' + SUBSTRING(s,1,LEN(s) - LEN(' UNION ALL')) + ')
SELECT dat.db, dat.max_intProfessionCode
FROM dat
INNER JOIN (SELECT MAX(max_intProfessionCode) AS max_intProfessionCode_overall
FROM dat) y
ON dat.max_intProfessionCode = y.max_intProfessionCode_overall' AS scrpt
FROM dat;
Make sure the above is only returning data for the appropriate databases by editing the WHERE clause in the CTE, then copy the output, paste elsewhere and run it to get your results.
I'm using MS SQL Server and am Trying to convert the date and time data into something useful for PowerBi and
can't get the time to work, Date is fine:
USE [CDCP_AEP]
GO
select *
from
(SELECT [H1PROD]
,convert(DATE,right('0'+str([H1DTTR],len([H1DTTR])),6),102) as 'AssyDate'
,[H1TMTR]
,right('000000'+str([H1TMTR],len([H1TMTR])),6) 'TempTime'
,[H1TYPE]
,[H1LOT]
,[H1SORD]
from [AEBPCSUSRF].[JHP1]
where [H1TYPE] = 'AF' and [H1LOT] <> '')a
left join
(select [P1PROD]
,[P1LOT]
from [AEBPCSUSRF].[PLA1])b
on a.[H1LOT] = b.[P1LOT]
GO
This is the result I get with the above code:
If I change the date (H1TMTR) line to
,convert(TIME,right('000000'+str([H1TMTR],len([H1TMTR])),6)) as 'AssyTime'
I get the following error:
"Msg 241, Level 16, State 1, Line 4
Conversion failed when converting date and/or time from character string."
Any tips? Thanks in advance!
You can convert by padding, substring'ing, and putting colons in between the parts like so:
DECLARE #H1TMTR VARCHAR(6)='155509'
SELECT CONVERT(TIME,
SUBSTRING(RIGHT('000000' + #H1TMTR,6),1,2) + ':'
+ SUBSTRING(RIGHT('000000' + #H1TMTR,6),3,2) + ':'
+ SUBSTRING(RIGHT('000000' + #H1TMTR,6),5,2)
) AS [Time]
Since you are doing this for PowerBI, I presume you will be doing this quite a bit. You should make a scalar function that performs this task on a column so it only needs written once.
I have to create an inline function (stored procedure not allowed, as it cannot be accessed by end users, multistatement function not allowed because of performance issues). I am struck up in a piece of code.
The code goes like this.
ALTER FUNCTION [CDC].[FN_CBR_COPERNICUS_DBO_AGMASTER_ANY] (
#START_LSN BINARY (10)
,#END_LSN BINARY (10)
,#APPLNID SMALLINT
)
RETURNS TABLE
AS
RETURN (
SELECT (
SELECT STUFF((
SELECT ', ' + CBR.CBRCA_COLUMNNANME
FROM CBRCOLAPPLN CBR
WHERE CBR.CBRCA_APPLICATIONID = 1
AND CBR.CBRCA_TABLENAME = 'COPERNICUS_CORE_PARAMETER'
FOR XML PATH('')
,TYPE
).VALUE('.', 'NVARCHAR(MAX)'), 1, 2, ' ') CHANGED_COLUMNS
)
,__$OPERATION AS CBR_OPERATION
FROM CBR.FN_CBR_GET_NET_CHANGES_ES_CMS_DBO_COPERNICUS_CORE_PARAMETER(0x0009631C000016F90029, 0x0009631D000008720001, 'ALL WITH MASK') AS CBRUPDATEDCOLUMNS
)
The subquery select will return values , if I execute the subquery alone, it returns the below
CMS_ID
,CMS_PARA_NAME
,CMS_PARA_TYPE
,CMS_PARA_MODULE
,CMS_PARA_VALUE
,CMS_COMMENTS
,CMS_ACTIVE_YN
When I place the values of subquery inside the function and place the code as
SELECT CMS_ID
,CMS_PARA_NAME
,CMS_PARA_TYPE
,CMS_PARA_MODULE
,CMS_PARA_VALUE
,CMS_COMMENTS
,CMS_ACTIVE_YN
,__$operation AS CBR_OPERATION
FROM CBR.FN_CBR_GET_NET_CHANGES_ES_CMS_DBO_COPERNICUS_CORE_PARAMETER(0x0009631C000016F90029, 0x0009631D000008720001, 'all with mask') AS CBRUPDATEDCOLUMNS
Upon executing it , I get the desired results,from the function CBR.FN_CBR_GET_NET_CHANGES_ES_CMS_DBO_COPERNICUS_CORE_PARAMETER
Whereas if I execute the first set of code (the one written in function), placed in the begining of the question, it just results me the value of the subquery * number of rows in the function
. It just gives me the result as below, along with the some ,__$OPERATION value
CMS_ID, CMS_PARA_NAME, CMS_PARA_TYPE, CMS_PARA_MODULE, CMS_PARA_VALUE, CMS_COMMENTS, CMS_ACTIVE_YN
CMS_ID, CMS_PARA_NAME, CMS_PARA_TYPE, CMS_PARA_MODULE, CMS_PARA_VALUE, CMS_COMMENTS, CMS_ACTIVE_YN
CMS_ID, CMS_PARA_NAME, CMS_PARA_TYPE, CMS_PARA_MODULE, CMS_PARA_VALUE, CMS_COMMENTS, CMS_ACTIVE_YN
CMS_ID, CMS_PARA_NAME, CMS_PARA_TYPE, CMS_PARA_MODULE, CMS_PARA_VALUE, CMS_COMMENTS, CMS_ACTIVE_YN
Could you please help me where am I going wrong?
I want to convert the order of the values with column name PTNT_VST_CSNO from the following :
VMIP1
VMIP10
VMIP11
VMIP2
VMIP20
VMIP21
VMIP3
VMIP31
VMIP32
VMIP5
VMIP6
VMIP7
VMIP8
VMIP9
VMOP10
VMOP11
VMOP12
VMOP3
VMOP30
VMOP31
VMOP32
VMOP4
VMOP40
VMOP41
VMOP42
VMOP43
VMOP7
VMOP70
VMOP71
VMOP8
VMOP9
to:
VMIP1
VMIP2
VMIP3
VMIP5
VMIP6
VMIP7
VMIP8
VMIP9
VMIP10
VMIP11
VMIP20
VMIP21
VMIP31
VMIP32
VMOP3
VMOP4
VMOP7
VMOP8
VMOP9
VMOP10
VMOP11
VMOP12
VMOP30
VMOP31
VMOP32
VMOP40
VMOP41
VMOP42
VMOP43
VMOP70
VMOP71
I want to sort the numeric part of 'vmip' first then that of 'vmop'.. I tried a lot but failed every time. kindly help me guys to sort out the sorting problem... thank you in advance
Not the fastest thing in the world, but it should get the job done:
ORDER BY CASE WHEN PTNT_VST_CSNO LIKE 'vmi%' THEN 0 ELSE 1 END
,CAST(replace(replace(PTNT_VST_CSNO, 'vmip', ''), 'vmop', '') as int)
After great trial, I succeeded to solve it with the following way..
SELECT ptnt_vst_csno
FROM table_name
ORDER BY Substring(ptnt_vst_csno, 1, Charindex('P', ptnt_vst_csno)),
CONVERT(INT, Substring(Substring(ptnt_vst_csno,
Charindex('P', ptnt_vst_csno),
Len(
ptnt_vst_csno)), 2, Len(
ptnt_vst_csno)))
the easiest way to accomplish this would be to change your numering to 3 digits.
i.e. 1 would become 001, 2 would become 002, 10 would become 010, and so on...
this would then allow you to order the data correctly.
you might want to do something like this:
SELECT
PTNT_VST_CSNO,
'VMIP' + CASE LEN(REPLACE(PTNT_VST_CSNO, 'VMIP', ''))
WHEN 1 THEN '00' + REPLACE(PTNT_VST_CSNO, 'VMIP', '')
WHEN 2 THEN '0' + REPLACE(PTNT_VST_CSNO, 'VMIP', '')
ELSE REPLACE(PTNT_VST_CSNO, 'VMIP', '')
END
FROM
TableName
WHERE
LEFT(PTNT_VST_CSNO, 4) = 'VMIP'
ORDER BY
2