Copy and Paste T-SQL query result into SSMS Text Editor with Column Align - tsql

When I copy and paste the query results into the text editor the columns are miss-aligned and hard to read. Is there a way to copy and paste into the text editor with column align like when you copy and paste the query result into Excel or Sheets it keeps the columns aligned? Is there a setting, extension or more advanced text query editor that will keep the columns aligned? Is there a way to add an extension using the Excel or Sheets paste function that will keep the columns aligned?
Example
INSERT INTO ##TestTable(Name, UpdateByApp1Date, UpdateByApp2Date, UpdateByApp3Date )
VALUES('ABC', '2015-08-05','2015-08-04', '2015-08-06'),
('NewCopmany', '2014-07-05','2012-12-09', '2015-08-14'),
('MyCompany', '2015-03-05','2015-01-14', '2015-07-26')
SELECT * FROM ##TestTable
--ID Name UpdateByApp1Date UpdateByApp2Date UpdateByApp3Date
--1 ABC 2015-08-05 00:00:00.000 2015-08-04 00:00:00.000 2015-08-06 00:00:00.000
--2 NewCopmany 2014-07-05 00:00:00.000 2012-12-09 00:00:00.000 2015-08-14 00:00:00.000
--3 MyCompany 2015-03-05 00:00:00.000 2015-01-14 00:00:00.000 2015-07-26 00:00:00.000
I want to see the results copy paste into the text editor with columns aligned
--ID Name UpdateByApp1Date UpdateByApp2Date UpdateByApp3Date
--1 ABC 2015-08-05 00:00:00.000 2015-08-04 00:00:00.000 2015-08-06 00:00:00.000
--2 NewCopmany 2014-07-05 00:00:00.000 2012-12-09 00:00:00.000 2015-08-14 00:00:00.000
--3 MyCompany 2015-03-05 00:00:00.000 2015-01-14 00:00:00.000 2015-07-26 00:00:00.000

When you copy and paste the standard format, it puts tabs between each column - a relatively standard process for pasting into tables (e.g., Excel). SQL Server Management Studio, by default, outputs its results that copy to this tab-delimited format.
However, you can change the type of output which changes the way it copies. If you change it to output to 'Results to text', it formats it as a text table.
To change the output/results mode, you can
Change it in preferences (Tools -> Options -> Query results -> SQL Server)
In the main working area, before you run the query, you can right-click on/around the SQL statement area and select 'Results To -> Text'
Before running the query, hit CTRL-T (hit CTRL-D to revert to the normal output to grid).
Note that when you paste the text from the output, to keep spacing lined up properly, you will need to use a non-proportional font (like Courier New, or Lucida Console). These are the default in programs like Notepad, but not in many word processors etc.

Related

How do I create a date from this data?

I've exported data that contains a date field but not all fields are dates.
Here is a sample:
29/03/2017 12:00:00 AM
30/03/2017 12:00:00 AM
31/03/2017 12:00:00 AM
7/4/2017 0:00
10/4/2017 0:00
11/4/2017 0:00
12/4/2017 0:00
I need a formula to create a date in a new cell so I can create pivot tables and analyze data by date.
Using =DATE(MID(E2,7,4),MID(E2,4,2),LEFT(E2,2)) works for the first set of dates (the ones that include AM at the end)
But I get an error on the second set of dates (the ones that do not have an AM)
Using left(cell,2) returns 42 on these cells. What the heck is this 42? When I use left(cell,1) it returns 4. I can't figure out how to create a date out of this field.
Looks like excel recognizes your date in multiple formats. Thats why the error. Please try the below formula,
=MID(LEFT(TEXT(A1,"dd/mm/yyyy"),10),4,2)&"/"&LEFT(LEFT(TEXT(A1,"dd/mm/yyyy"),10),2)&"/"&RIGHT(LEFT(TEXT(A1,"dd/mm/yyyy"),10),4)
This formula converts your string to a dd/mm/yyyy format and then to mm/dd/yyyy format.

How to convert dutch dates into date/time format in MsAccess

I have TSV files that contains columns with Dutch formatted text fields in dd-MM-YYYY format, like 22-OKT-2004 and 05-MRT-2004. I want to import the file to Access.
The date columns should be imported as date/time objects. However, the above mentioned months (MRT, MEI, OKT) are not recognized, probably because they differ from the corresponding english abbreviations. How should I use the import wizard to still import these objects?
(Strangely, if I import the columns as text columns, and convert them later via table design, it does work. But I don't like this option because some of the TSV files are so big that converting after importing (instead of during the import) gives an error: Microsoft office can't change the data type; there is not enough disk space or memory.)
I was able to recreate your issue using the "French (Canada)" regional setting in Windows. For a tab-delimited file
ID DonorID Amount DateString
1 1 8 01-janv-2013
2 1 2 01-févr-2013
3 1 3 01-mars-2013
4 1 4 01-avr-2013
5 1 5 01-mai-2013
7 2 5 01-juin-2013
8 2 6 01-juil-2013
13 2 4 01-août-2013
14 2 7 01-sept-2013
15 2 9 01-oct-2013
16 2 6 01-nov-2013
17 2 7 01-déc-2013
if I tried to import the date column directly to a Date/Time field in Access I would get conversion errors for every month except "oct" and "nov" (where the French abbreviation matches the English one). However, I could import the date field as Text, and then if I run the query
SELECT ID, DonorID, Amount, CDate([DateString]) AS DateValue
FROM importedData;
the [DateValue] column shows the correct dates. So, you could conceivably import the dates as Text and then
use the above query directly, or
change it into a make-table query (SELECT ... INTO NewTableName FROM ...) to re-write the dates as actual date values.

How do I achieve a pivot report in Crystal Reports for Visual Studio?

I am a rank noob at any reporting but horizontal lines with sub-totals. I have a dataset that comprises the times spent by cars in a carpark. I must produce a table showing a column for each hour and a row for each day, showing the number of entries at a particular time on a particular day.
I would find this easier in the RDLC designer, with it's columnar groupings, but I can't even find such a thing in Crystal.
Help.
Assuming the following fields: {table.car_id} and {table.census_time}
Choose Insert | Crosstab...; add cross-tab to report-header section
Right click the cross-tab and chose 'cross tab expert':
Add {table.census_time] to the column-field list; group by hour
Add {table.census_time} to the row-field list; group by day
Add {table.car_id} to the summary-field list; count
** edit **
You don't need to create a special formula to extract the hour from the date/time field; the cross-tab will do that for you.
Select the 'Cross-Tab' tab, add the {table.census_time} field, then click the 'Group Options...' button.
Choose 'for each hour.' from the picklist.
You need to create a table with a record for each hour:
Table DayHour
Period StartHour EndHour
00:00 - 01:00 0 1
01:00 - 02:00 1 2
02:00 - 03:00 2 3
03:00 - 04:00 3 4
...
23:00 - 00:00 23 24
Then left join your data with this table
SELECT h.Period, DATEPART(dd,d.EntryTime) as Day, 1 as Value
FROM DayHour h LEFT JOIN <YourData> d ON h.StartHour <=DATEPART(hh,d.EntryTime) and DATEPART(hh,d.EntryTime)<h.EndHour
This will return all the record with the time period and will return even a record if no vehicles entered the parking during a specific time frame. Drag and drop the Period column to the column section of the cross tab and Day column to the Rows sections. Drag Value column to the summary section.

DB2 9.7 Distinct rows returned

I have been researching through the posted material on the site and can't figure if it is GROUP BY or PARTITION BY and I can't seem to get the answer I want.
What I have is an enormous amount of TAG data and what I need is to retrieve a history of locations that it was seen at i.e. show me the last time it was seen at a particular location.
I have added a small subset of the data.
What I would want to see is by date Order being newest first so I would expect to see ROW 01 as the first in location ...004 and then next entry would be ROW 209 as it it the latest entry for ...50 and then ROW 216 ..004 again so everytime there is a change.
Can this be done with a SQL Statement
Thanks for any help you can offer
ROW TAG DATE
001 004 2012-10-19 10:20
002 004 2012-10-19 10:10
003 004 2012-10-19 10:00
209 050 2012-10-19 08:50
210 050 2012-10-19 08:40
211 050 2012-10-19 08:30
216 004 2012-10-19 07:30
217 004 2012-10-19 02:20
Here is how it can be done:
with last_records_tagged as (
select
row,
tag,
date,
case when lead(tag) over (partition by tag order by date asc) is null
then 1
else 0
end as last
from your_table
)
select * from last_records_tagged where last = 1
(I ordered ascending by date because it makes more sense to me).
Basically, any time the next record, ordered by date, has a different tag, you know you are at the last record of that group.

Word Mail-Merge not showing elapsed seconds correctly

I'm doing a mail-merge in Word 2010 with an Excel 2010 file as the data source. Everything is working fine except a field where 'elapsed time' is being calculated.
In Excel, the field appears correctly: 193 seconds is showing as 00:03:13 -- for three minutes, thirteen seconds.
When I insert the field in Word it shows up as a time of day: 12:03:05 AM
Is there a MERGEFIELD code that formats this correctly in Word? I haven't been able to find one yet. If there is a work-around, I'd be interested in hearing that as well.
Add a date format switch to your field as follows:
{ MERGEFIELD mydate \#mm:ss }