Tag: tsql

How would I pivot this type of data in T-SQL?

I have a query which generates results such as this ID | Column2 | Sentence —-|———|——— 1 | SameVal |This is a sentence 1 | SameVal |This is another unique sentence 1 | SameVal |A third unique sentence 2 | SameVal |This is a sentence 2 | SameVal |This is another unique sentence 2 | […]

Executing formula from table

I have a process that builds reports based upon dynamic SQL queries stored in tables. When I originally wrote it as a proof-of-concept it was able to successfully work when using a cursor style process…was originally actually done as a script in the proof, using Do/While – the “proof” was moved to tSQL initially in […]

conversion varchar data type to a datetime data type resulted in an out-of-range

need help trying to get from the database some info and i want to call only the persons of today and i have the error the conversion of a varchar data type to a datetime data type resulted in an out-of-range value. TRY_CONVERT isn’t a recognized function select DTC.PrimerNombre+ ‘ ‘ + DTC.SegundoNombre+’ ‘+ DTC.PrimerApellido […]

SQL Server time sheet calculation

I have a time punch program the outputs the data set below. RECTYP_43 are the (1) in and (2) out punches. I need a query to look at the look at the LOGINDATE_43 and LOGINTIME_43 and the RECTYPE_43 and get the difference between 1 and 2. I thought this would be easier than it has […]

Table name variable in MS SQL query

I have dynamically created tables, like XXX_JOURNAL. Where XXX – is table prefix (variable), and _JOURNAL – is constant in table name. I need create UPDATE trigger on database, not on particular table, and use table name (prefix) as variable: CREATE TRIGGER triggerName ON %_JOURNAL FOR UPDATE AS UPDATE XXX_JOURNAL SET COMPANY_ID = LEFT(tableName,3) //tableName […]

Combine 2 tables in SQL with some different column names (Transact SQL)

I’m trying to combine multiple tables in SQL Server 2012, where some columns are the same, but others are different. I want to have them in the same table, with NULL values where data is missing. What I want is something like union but allowing different column names. For example, Table 1: ID | First […]

How to create txt or csv file by tsql

I have a temp table in sql server 2008 and I need to create a csv of txt file by tsql from that temp table. What is the correct way for it? Thanks.

SQL Transaction connected with WAITFOR DELAY

I was looking through some posts but I couldn’t find similar case that could explain me if this solution is safe. I want to execute many UPDATE queries at once but due to server performance issues, I can run only limited number of queries at once. The solution seems to be easy by using WAITFOR […]

T-SQL : If login failed, then sql

Goal: Write a T-SQL statement that checks for login permissions on a linked server db, and then updates a column value based on login result. Sudo-ish code: @Source = linked server connection string while true if exists(select [Something] from [@Source]) update [Table] set [Column] = 1 else update [Table] set [Column] = 0 @Source = […]

How to write the query result from a variable to a table

What I want to achieve here is to get the result from the CheckSum_Agg(Binary_CheckSum(*)) and write this to a table with the matching TABLE_NAME. The query below is something I came up with, but it’s missing the next step to write the query result to a table. DROP TABLE HashTable CREATE TABLE HashTable ( HashTable […]

MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.