tsql

Stored Procedure to Copy Views from Current Database to Another

北城以北 提交于 2020-12-12 10:51:10
问题 I've been asked whether it's possible to create a stored procedure that will copy all views in the current database to another one (named via stored procedure parameter). For context, all databases have the same schemas. This situation arises thanks to a 3rd party risk modelling tool that generates each run's output as an entirely new database (rather than additional rows in an existing database). The user wants an easy way to "apply" their 20 or so custom views (from their "Template"

How to check if VARCHAR strings are (not) hexadecimal?

耗尽温柔 提交于 2020-12-10 13:28:29
问题 TL;DR How to test if string is hex when selecting rows? If I had a table with GUIDs where some of them are Base36 instead if hexadecimal: ID | GUID ===|================================= 1 | CAFEBABECAFED00DB16B00B515BADA55 2 | 0123456789ABCDEFGHIJKLMNOPQRSTUV 3 | DEADBEAF4D15EA5EDEFEC8EDFEE1DEAD I want to get all rows with GUIDs that are not exclusively hexadecimal. For a singleton, I could try CONVERT(VARBINARY(32),[GUID],2) and see if it fails, but I can’t to that in a query. If I could

How to check if VARCHAR strings are (not) hexadecimal?

混江龙づ霸主 提交于 2020-12-10 13:22:13
问题 TL;DR How to test if string is hex when selecting rows? If I had a table with GUIDs where some of them are Base36 instead if hexadecimal: ID | GUID ===|================================= 1 | CAFEBABECAFED00DB16B00B515BADA55 2 | 0123456789ABCDEFGHIJKLMNOPQRSTUV 3 | DEADBEAF4D15EA5EDEFEC8EDFEE1DEAD I want to get all rows with GUIDs that are not exclusively hexadecimal. For a singleton, I could try CONVERT(VARBINARY(32),[GUID],2) and see if it fails, but I can’t to that in a query. If I could

Get data from every column in every table in a database | SQL Server

隐身守侯 提交于 2020-12-06 12:03:57
问题 I have seen multiple questions on how to retrieve every column from every table along with its data type, among many other pieces of information which can be summarised in the shortest way with this query: SELECT * FROM INFORMATION_SCHEMA.COLUMNS However, is it possible to get all the data from the columns and the rows they belong to get the first row in the table alongside this? I have not found a way to do so thus far. Is it possible to do such, maybe also having a WHERE condition such as

Get data from every column in every table in a database | SQL Server

一曲冷凌霜 提交于 2020-12-06 11:57:18
问题 I have seen multiple questions on how to retrieve every column from every table along with its data type, among many other pieces of information which can be summarised in the shortest way with this query: SELECT * FROM INFORMATION_SCHEMA.COLUMNS However, is it possible to get all the data from the columns and the rows they belong to get the first row in the table alongside this? I have not found a way to do so thus far. Is it possible to do such, maybe also having a WHERE condition such as

T-SQL Format seconds as HH:MM:SS time

自作多情 提交于 2020-12-03 07:28:56
问题 Is there any tricky way to format seconds like hours:minutes:seconds. For example, 3660 seconds will be displayed as 01h 01m 00s or 01:01:00 I am aware of the standard way of doing this: Divide all seconds on 3600 to get the hours Divide the rest seconds on 60 to get the minutes The rest are the seconds I met the following issues: I am not able to create separate function that do this. My code is in view using several CTEs. So, variables can be declare using the CTEs only. I am not able to

T-SQL Format seconds as HH:MM:SS time

放肆的年华 提交于 2020-12-03 07:26:31
问题 Is there any tricky way to format seconds like hours:minutes:seconds. For example, 3660 seconds will be displayed as 01h 01m 00s or 01:01:00 I am aware of the standard way of doing this: Divide all seconds on 3600 to get the hours Divide the rest seconds on 60 to get the minutes The rest are the seconds I met the following issues: I am not able to create separate function that do this. My code is in view using several CTEs. So, variables can be declare using the CTEs only. I am not able to

SELECT DISTINCT with LEFT JOIN, ORDERed BY in t-SQL

穿精又带淫゛_ 提交于 2020-12-01 09:57:00
问题 I have the following table in SQL Server 2008: CREATE TABLE tbl (ID INT, dtIn DATETIME2, dtOut DATETIME2, Type INT) INSERT tbl VALUES (1, '05:00', '6:00', 1), (2, '05:00', '7:00', 1), (3, '05:01', '8:00', 1), (4, '05:00', '8:00', 1), (5, '05:00', '6:00', 2), (6, '05:00', '7:00', 2) that selects IDs of all records of the same type, with the same dtIn date, ordered by stOut in ascending order: SELECT DISTINCT tbl.id FROM tbl LEFT JOIN tbl AS t1 ON tbl.type = t1.type AND tbl.dtIn = t1.dtIn ORDER

SELECT DISTINCT with LEFT JOIN, ORDERed BY in t-SQL

馋奶兔 提交于 2020-12-01 09:54:14
问题 I have the following table in SQL Server 2008: CREATE TABLE tbl (ID INT, dtIn DATETIME2, dtOut DATETIME2, Type INT) INSERT tbl VALUES (1, '05:00', '6:00', 1), (2, '05:00', '7:00', 1), (3, '05:01', '8:00', 1), (4, '05:00', '8:00', 1), (5, '05:00', '6:00', 2), (6, '05:00', '7:00', 2) that selects IDs of all records of the same type, with the same dtIn date, ordered by stOut in ascending order: SELECT DISTINCT tbl.id FROM tbl LEFT JOIN tbl AS t1 ON tbl.type = t1.type AND tbl.dtIn = t1.dtIn ORDER

How to create pivot using sql

懵懂的女人 提交于 2020-11-29 19:36:16
问题 I am new to SQL, I have a table like this. Below is the query : select gc.GC_Name, dt.GC_SectorType, dt.ageing, sum(cast(dt.[Brokerage Debtors] as numeric)) as Brokerage_Amt, dt.divisionalofficename from [AR].[Fact_Brokerage_Debt] dt inner join AUM.DIM_BUSINESS_TYPE BT on BT.Business_Type_WId_PK = dt.BusinessType_WID inner join aum.Dim_GroupCompany gc on dt.insurer_Wid = gc.GC_WID where bt.Business_Type_Wid in (4, 8, 10) and dt.ageing <> '<30' and cast(dt.[Brokerage Debtors] as numeric) > 0