Word Table to CSV file Macro - vba

I'm new to Macros and teaching myself as I go. My goal is to populate a file with data from a Word table using CSV formatting. The table spans over 300 pages with no exact number of rows per page. A user prompt for selecting a file at the beginning has been created. What would be recommended process wise? I know a For loop would be needed for the row count.
Dim fd As Office.FileDialog
Dim strFile As String
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.Filters.Clear
.Filters.Add "Word Files", "*.docx?", 1
.Title = "Choose a Word file"
.AllowMultiSelect = False
.InitialFileName = "C:\VBA Folder"
If .Show = True Then
strFile = .SelectedItems(1)
End If
End With
Intended Output:
Identifer, Artifact Type, Name, Red, Amber, White, Cust, IO_Type, M_680A, M_680_Phase, M_750, Left_Input, Right_Input, Input_Type, Source, Debounce, LOPI, TOPI, ESDI, EFI, BFI, On_gnd, In_air, FDRIO, Auto_test, Frome, Level, VerifiedBy, Status
XXX , Module, X346, , , , , None,6, , , , , , , , , , , , , , , , , SAS, , ,
XXX , Module, S003, , , , , None,6, , , , , , , , , , , , , , , , , SAS, ,
XXX , Module, X301, , , , , None,6, , , , , , , , , , , , , , , , , SAS, , ,
XXX , Module, X025, , , , , None,6, , , , , , , , , , , , , , , , , SAS, , ,
XXX , Module, X511, , , , , None,6, , , , , , , , , , , , , , , , , SAS, , ,
XXX , Module, X347, , , , , None,6, , , , , , , , , , , , , , , , , SAS, , ,
XXX , Module, X514, , , , , None,6, , , , , , , , , , , , , , , , , SAS, , ,
XXX , Module, S050, , , , , None,6, ,7, , , , , , , , , , , , , , , SAS, , ,
XXX , Module, X021, , , , , None,6, ,7, , , , , , , , , , , , , , , SAS, , ,
XXX , Module, X517, , , , , None,6, ,7, , , , , , , , , , , , , , , SAS, , ,
XXX , Module, X599, , , , , None,6, ,7, , , , , , , , , , , , , , , SAS, , ,
XXX , Module, X022, , , , , None,6, ,7, , , , , , , , , , , , , , , SAS, , ,
XXX , Module, X521, , , , , None,6, ,7, , , , , , , , , , , , , , , ADD, , ,
XXX , Module, X523, , , , , None,6, ,7, , , , , , , , , , , , , , , ADD, , ,
XXX , Module, X019, , , , , None,6, ,7, , , , , , , , , , , , , , , SAS, , ,
XXX , Module, X518, , , , , None,6, ,7, , , , , , , , , , , , , , , SAS, , ,
XXX , Module, M141, , , W, , None,6, ,7, , , ,,0, L, T, , , , , , ,, SAS, ,,

At close to its simplest, you can convert a table to CSV format with:
Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range
With Selection
If .Information(wdWithInTable) = True Then
Set Rng = .Tables(1).Range
With .Tables(1)
.ConvertToText Separator:=","
End With
Rng.Select
End If
End With
Application.ScreenUpdating = True
End Sub
Simply place the selection point anywhere within the table. The converted range will be selected when the macro finishes.
To save the converted range to a new file, you could use code like:
Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range, DocSrc As Document, DocTgt As Document
With Selection
Set DocSrc = .Range.Document
If .Information(wdWithInTable) = True Then
Set Rng = .Tables(1).Range
With .Tables(1)
.ConvertToText Separator:=","
End With
Set DocTgt = Documents.Add
With DocTgt.Range
.FormattedText = Rng.FormattedText
.Characters.Last.Previous.Text = vbNullString
End With
DocSrc.Undo
With Application.Dialogs(wdDialogFileSaveAs)
.Format = wdFormatText
.Name = "C:\Users\" & Environ("Username") & "\Documents\MyFile"
.AddToMru = False
.Show
End With
End If
End With
Application.ScreenUpdating = True
End Sub
This will export the converted content to a new file and display the SaveAs dialogue box, with the user's 'Documents' folder selected. You can coose another folder, if you prefer. Then simply substitute your preferred filename, including the .csv extension, for the 'MyFile.txt' offered by the dialogue box.
If you want to exclude particular columns, you could insert code to delete them at 'With .Tables(1)'. For example, to omit the third column:
With .Tables(1)
.Columns(3).Delete
.ConvertToText Separator:=","
End With

Related

Optimize multiple query separated by condition

I have a stored procedures with three different queries, that querys return values depending of received variable.
They basically return same data, just change WHERE clauses:
Query:
IF #DesignType = 'BDCD'
BEGIN
SELECT
[DT].[DesignTypeGuid]
, [DT].[Name]
, [DT].[Abbreviation]
, CONVERT(BIT , IIF([DT].[DesignTypeGuid] = #ContractedDocument , 1 , 0)) AS [EnforceBaseOnPrevious]
, [DT].[Order]
FROM [DesignType] AS [DT]
WHERE [DT].[IsActive] = 1
AND ([DT].[DesignTypeGuid] != IIF(#DesignTypeGuid = #ContractedDocument , #BidDocument , #ContractedDocument))
AND [DT].[DesignTypeGuid] != #ChangeOrder
AND [DT].[DesignTypeGuid] != #AddedPart
ORDER BY
[DT].[Order]
END
ELSE
BEGIN
IF #DesignType = 'CO'
BEGIN
SELECT
[DT].[DesignTypeGuid]
, [DT].[Name]
, [DT].[Abbreviation]
, 0 AS [EnforceBaseOnPrevious]
, [DT].[Order]
FROM [DesignType] AS [DT]
WHERE [DT].[IsActive] = 1
AND [DT].[DesignTypeGuid] = #ChangeOrder
ORDER BY
[DT].[Order]
END
ELSE
BEGIN
IF #DesignType = 'AP'
BEGIN
SELECT
[DT].[DesignTypeGuid]
, [DT].[Name]
, [DT].[Abbreviation]
, 0 AS [EnforceBaseOnPrevious]
, [DT].[Order]
FROM [DesignType] AS [DT]
WHERE [DT].[IsActive] = 1
AND [DT].[DesignTypeGuid] = #AddedPart
ORDER BY
[DT].[Order]
END
Is there any way to optimize this and do it in a simple SELECT instead do 3 differents depending of DesignType variable? Regards
Not sure about the performance , but the query could definitely be written as
SELECT
[DT].[DesignTypeGuid]
, [DT].[Name]
, [DT].[Abbreviation]
, CASE #DesignType WHEN 'BDCD' THEN CONVERT(BIT , IIF([DT].[DesignTypeGuid] = #ContractedDocument , 1 , 0)) ELSE 0 END AS [EnforceBaseOnPrevious]
, [DT].[Order]
FROM [DesignType] AS [DT]
WHERE [DT].[IsActive] = 1
AND (
(
#DesignType = 'BDCD' AND [DT].[DesignTypeGuid] NOT IN ( IIF(#DesignTypeGuid = #ContractedDocument , #BidDocument , #ContractedDocument),#ChangeOrder,#AddedPart)
)
OR
(
#DesignType = 'CO' AND [DT].[DesignTypeGuid] = #ChangeOrder
)
OR
(
#DesignType = 'AP' AND [DT].[DesignTypeGuid] = #AddedPart
)
)
ORDER BY
[DT].[Order]

Adding in max() to query with many joins kills performance - how to optimise?

Let me preface this by saying I'm working in SSMS2016, and that my SQL is not the greatest.
I have a query with quite a few joins, which runs in slow-but-bearable 20-odd seconds.
I'd like to add a column which is basically the max value of the row_number in the CCR subquery (the 2 lines commented out in the code), however it massively impacts query time (60+ seconds for 1000 records). Is there any way I can get the required column without the huge jump in run time, and any way I can clean up the query in general?
SELECT DISTINCT AWI.*
FROM
(SELECT
4 'SORT'
, CCR.URN
, CCR.SPECIAL_COMMENT
, SL.SCHEDULED_SHIP_DATE
, CASE WHEN SL.LOAD_CLOSED = 'Y' THEN SL.ACTUAL_SHIP_DATE_TIME ELSE NULL END AS 'ACTUAL_SHIP_DATE'
, SL.LOAD_CLOSED
, SH.SHIPPING_LOAD_NUM 'SHIPPING_LOAD'
-- , MAX(CCR.RNK) OVER(PARTITION BY CCR.SHIPPING_LOAD_NUM) 'CTNS_ON_LOAD'
, SH.CARRIER
, SH.CARRIER_SERVICE 'SERVICE'
, IWI.PARENT_INSTR 'WORK_UNIT'
, IWI.CONDITION 'WORK_STATUS'
, IWI.WORK_TYPE
, sl.LOCATION 'DOCK_DOOR'
, CCR.CONTAINER_ID
, SC.LOCATION 'FROM_LOC'
, IWI.TO_LOC
, SH.SHIP_TO
, G.DESCRIPTION 'COUNTRY'
, case when SL.LEADING_STS = '201' then '[201] In Wave'
when SL.LEADING_STS = '300' then '[300] Picking Pending'
when SL.LEADING_STS = '301' then '[301] In Picking'
when SL.LEADING_STS = '400' then '[400] Packing Pending'
when SL.LEADING_STS = '401' then '[401] In Packing'
when SL.LEADING_STS = '500' then '[500] Pack and Hold Pending'
when SL.LEADING_STS = '600' then '[600] Staging Pending'
when SL.LEADING_STS = '650' then '[650] Loading Pending'
when SL.LEADING_STS = '700' then '[700] Ship Confirm Pending'
when SL.LEADING_STS = '800' then '[800] Load Confirm Pending'
when SL.LEADING_STS = '900' then '[900] Closed'
END AS 'LEADING_STS'
, case when SL.TRAILING_STS = '201' then '[201] In Wave'
when SL.TRAILING_STS = '300' then '[300] Picking Pending'
when SL.TRAILING_STS = '301' then '[301] In Picking'
when SL.TRAILING_STS = '400' then '[400] Packing Pending'
when SL.TRAILING_STS = '401' then '[401] In Packing'
when SL.TRAILING_STS = '500' then '[500] Pack and Hold Pending'
when SL.TRAILING_STS = '600' then '[600] Staging Pending'
when SL.TRAILING_STS = '650' then '[650] Loading Pending'
when SL.TRAILING_STS = '700' then '[700] Ship Confirm Pending'
when SL.TRAILING_STS = '800' then '[800] Load Confirm Pending'
when SL.TRAILING_STS = '900' then '[900] Closed'
END AS 'TRAILING_STS'
, CCR.CONTAINER_TYPE
, DIMS.WEIGHT
, DIMS.LENGTH
, DIMS.WIDTH
, DIMS.HEIGHT
FROM
IA_WORK_INSTRUCTION IWI
JOIN
(SELECT INTERNAL_SHIPMENT_NUM, SHIP_TO, CARRIER, CARRIER_SERVICE, SHIPPING_LOAD_NUM, SHIP_TO_COUNTRY FROM SHIPMENT_HEADER) SH
ON SH.INTERNAL_SHIPMENT_NUM = IWI.INTERNAL_NUM
JOIN
(SELECT c1.INTERNAL_WORK_INST_NUM, c1.CONTAINER_ID, c1.URN, c1.INTERNAL_CONTAINER_NUM, c1.SPECIAL_COMMENT, sc1.container_type, H1.SHIPPING_LOAD_NUM
-- , ROW_NUMBER() OVER(PARTITION BY H1.SHIPPING_LOAD_NUM ORDER BY C1.CONTAINER_ID DESC) 'RNK'
FROM CARTON_CALLOFF_REQUEST c1
join shipping_container sc1
on sc1.internal_container_num = c1.internal_container_num
JOIN SHIPMENT_HEADER H1
ON H1.INTERNAL_SHIPMENT_NUM = SC1.INTERNAL_SHIPMENT_NUM
) CCR
ON CCR.INTERNAL_WORK_INST_NUM = IWI.PARENT_INSTR
JOIN
(SELECT DISTINCT IDENTIFIER, DESCRIPTION FROM GENERIC_CONFIG_DETAIL WHERE RECORD_TYPE = 'COUNTRY') G
ON G.IDENTIFIER = SH.SHIP_TO_COUNTRY
JOIN
(SELECT INTERNAL_SHIPMENT_NUM, PARENT, LOCATION FROM SHIPPING_CONTAINER) SC
ON SC.PARENT = CCR.INTERNAL_CONTAINER_NUM AND SC.INTERNAL_SHIPMENT_NUM = SH.INTERNAL_SHIPMENT_NUM
JOIN
(SELECT DISTINCT INTERNAL_CONTAINER_NUM, WEIGHT, LENGTH, WIDTH, HEIGHT FROM SHIPPING_CONTAINER WHERE CONTAINER_ID IS NOT NULL) DIMS
ON DIMS.INTERNAL_CONTAINER_NUM = CCR.INTERNAL_CONTAINER_NUM
JOIN
(SELECT s1.INTERNAL_LOAD_NUM, s1.LEADING_STS, s1.TRAILING_STS, s1.LOAD_CLOSED, s1.ACTUAL_SHIP_DATE_TIME, s1.SCHEDULED_SHIP_DATE, s1.DOCK_DOOR, l1.location FROM SHIPPING_LOAD s1 join location l1 on l1.object_id = s1.dock_door WHERE L1.TEMPLATE_FIELD1 = 'DOOR') SL
ON SL.INTERNAL_LOAD_NUM = SH.SHIPPING_LOAD_NUM
) AWI
WHERE AWI.WORK_TYPE = 'CALL OFF PICK'
You might try replacing the Partition and Over() with Group By.
Please let us know if you face any issues even after adding the Group By.
SELECT DISTINCT AWI.*
FROM
(SELECT
4 'SORT'
, CCR.URN
, CCR.SPECIAL_COMMENT
, SL.SCHEDULED_SHIP_DATE
, CASE WHEN SL.LOAD_CLOSED = 'Y' THEN SL.ACTUAL_SHIP_DATE_TIME ELSE NULL END AS 'ACTUAL_SHIP_DATE'
, SL.LOAD_CLOSED
, SH.SHIPPING_LOAD_NUM 'SHIPPING_LOAD'
, MAX(CCR.RNK)
, SH.CARRIER
, SH.CARRIER_SERVICE 'SERVICE'
, IWI.PARENT_INSTR 'WORK_UNIT'
, IWI.CONDITION 'WORK_STATUS'
, IWI.WORK_TYPE
, sl.LOCATION 'DOCK_DOOR'
, CCR.CONTAINER_ID
, SC.LOCATION 'FROM_LOC'
, IWI.TO_LOC
, SH.SHIP_TO
, G.DESCRIPTION 'COUNTRY'
, case when SL.LEADING_STS = '201' then '[201] In Wave'
when SL.LEADING_STS = '300' then '[300] Picking Pending'
when SL.LEADING_STS = '301' then '[301] In Picking'
when SL.LEADING_STS = '400' then '[400] Packing Pending'
when SL.LEADING_STS = '401' then '[401] In Packing'
when SL.LEADING_STS = '500' then '[500] Pack and Hold Pending'
when SL.LEADING_STS = '600' then '[600] Staging Pending'
when SL.LEADING_STS = '650' then '[650] Loading Pending'
when SL.LEADING_STS = '700' then '[700] Ship Confirm Pending'
when SL.LEADING_STS = '800' then '[800] Load Confirm Pending'
when SL.LEADING_STS = '900' then '[900] Closed'
END AS 'LEADING_STS'
, case when SL.TRAILING_STS = '201' then '[201] In Wave'
when SL.TRAILING_STS = '300' then '[300] Picking Pending'
when SL.TRAILING_STS = '301' then '[301] In Picking'
when SL.TRAILING_STS = '400' then '[400] Packing Pending'
when SL.TRAILING_STS = '401' then '[401] In Packing'
when SL.TRAILING_STS = '500' then '[500] Pack and Hold Pending'
when SL.TRAILING_STS = '600' then '[600] Staging Pending'
when SL.TRAILING_STS = '650' then '[650] Loading Pending'
when SL.TRAILING_STS = '700' then '[700] Ship Confirm Pending'
when SL.TRAILING_STS = '800' then '[800] Load Confirm Pending'
when SL.TRAILING_STS = '900' then '[900] Closed'
END AS 'TRAILING_STS'
, CCR.CONTAINER_TYPE
, DIMS.WEIGHT
, DIMS.LENGTH
, DIMS.WIDTH
, DIMS.HEIGHT
FROM
IA_WORK_INSTRUCTION IWI
JOIN
(SELECT INTERNAL_SHIPMENT_NUM, SHIP_TO, CARRIER, CARRIER_SERVICE, SHIPPING_LOAD_NUM, SHIP_TO_COUNTRY FROM SHIPMENT_HEADER) SH
ON SH.INTERNAL_SHIPMENT_NUM = IWI.INTERNAL_NUM
JOIN
(SELECT c1.INTERNAL_WORK_INST_NUM, c1.CONTAINER_ID, c1.URN, c1.INTERNAL_CONTAINER_NUM, c1.SPECIAL_COMMENT, sc1.container_type, H1.SHIPPING_LOAD_NUM
, ROW_NUMBER() OVER(PARTITION BY H1.SHIPPING_LOAD_NUM ORDER BY C1.CONTAINER_ID DESC) 'RNK'
FROM CARTON_CALLOFF_REQUEST c1
join shipping_container sc1
on sc1.internal_container_num = c1.internal_container_num
JOIN SHIPMENT_HEADER H1
ON H1.INTERNAL_SHIPMENT_NUM = SC1.INTERNAL_SHIPMENT_NUM
) CCR
ON CCR.INTERNAL_WORK_INST_NUM = IWI.PARENT_INSTR
JOIN
(SELECT DISTINCT IDENTIFIER, DESCRIPTION FROM GENERIC_CONFIG_DETAIL WHERE RECORD_TYPE = 'COUNTRY') G
ON G.IDENTIFIER = SH.SHIP_TO_COUNTRY
JOIN
(SELECT INTERNAL_SHIPMENT_NUM, PARENT, LOCATION FROM SHIPPING_CONTAINER) SC
ON SC.PARENT = CCR.INTERNAL_CONTAINER_NUM AND SC.INTERNAL_SHIPMENT_NUM = SH.INTERNAL_SHIPMENT_NUM
JOIN
(SELECT DISTINCT INTERNAL_CONTAINER_NUM, WEIGHT, LENGTH, WIDTH, HEIGHT FROM SHIPPING_CONTAINER WHERE CONTAINER_ID IS NOT NULL) DIMS
ON DIMS.INTERNAL_CONTAINER_NUM = CCR.INTERNAL_CONTAINER_NUM
JOIN
(SELECT s1.INTERNAL_LOAD_NUM, s1.LEADING_STS, s1.TRAILING_STS, s1.LOAD_CLOSED, s1.ACTUAL_SHIP_DATE_TIME, s1.SCHEDULED_SHIP_DATE, s1.DOCK_DOOR, l1.location FROM SHIPPING_LOAD s1 join location l1 on l1.object_id = s1.dock_door WHERE L1.TEMPLATE_FIELD1 = 'DOOR') SL
ON SL.INTERNAL_LOAD_NUM = SH.SHIPPING_LOAD_NUM
GROUP BY
CCR.URN
, CCR.SPECIAL_COMMENT
, SL.SCHEDULED_SHIP_DATE
, CASE WHEN SL.LOAD_CLOSED = 'Y' THEN SL.ACTUAL_SHIP_DATE_TIME ELSE
NULL END
, SL.LOAD_CLOSED
, SH.SHIPPING_LOAD_NUM
, SH.CARRIER
, SH.CARRIER_SERVICE
, IWI.PARENT_INSTR
, IWI.CONDITION
, IWI.WORK_TYPE
, sl.LOCATION
, CCR.CONTAINER_ID
, SC.LOCATION
, IWI.TO_LOC
, SH.SHIP_TO
, G.DESCRIPTION
, SL.LEADING_STS
, TRAILING_STS
, CCR.CONTAINER_TYPE
, DIMS.WEIGHT
, DIMS.LENGTH
, DIMS.WIDTH
, DIMS.HEIGHT
) AWI
WHERE AWI.WORK_TYPE = 'CALL OFF PICK'

Removing duplicates from select query output

Could anyone please suggest what is the error in the below query? Actually I want to remove all duplicates from the select query output and fetch only unique rows. Thanks in advance.
select *
from (
select ROW_NUMBER() over (
partition by Dep
, tariffkode
, LinkTariffType
, poliata
, poliatavia
, podiata
, podiatavia
, PreCarriage
, PortTerminalId
, Product
, RoutingOrder
, PrepaidCollect
, isnull(description, '')
, ScaleCalcCode
, isnull(scalefrom, 0)
, isnull(scaleto, 0)
, CurrencyCode
, Base order by LinkTariffType desc
) Record
, *
from (
select tn.LinkTariffType
, tn.Dep
, tn.POLIata
, tn.POLIatavia
, tn.PODIata
, tn.PODIatavia
, tn.CurrencyCode
, tn.LegalEntityID
, tn.Rate
, tn.Base
, tn.Minimum
, tn.NrDescription
, tn.Description
, tn.DateFrom
, tn.DateUntil
, tn.DateCreate
, tn.DateMod
, tn.ModName
, tn.Tariffkode
, tn.ExpiryDate
, tn.PClass
, tn.Maximum
, tn.RoutingOrder
, tn.TariffCompType
, tn.PrePaidCollect
, tn.Product
, tn.IsDeleted
, (
select distinct Location_IATA
from Company
where Called = 'KARL KING'
and LegalEntityID = 1
and IsDeleted = 0
) as PreCarriage
, tn.PortTerminalID
, tn.ScaleFrom
, tn.ScaleTo
, tn.ScaleCalcCode
, tn.Mandatory
, (
select CompanyID
from PlaceOfReceipt
where warehouse = 'KARL KING'
and LegalEntityID = 1
and OfficeID = 13
and IsDeleted = 0
) as WarehouseID
, tn.TariffRelID
, tn.FreeDescription
, 0
, tn.ShipCode
, tn.AgentID
, tn.ContainerTypeID
, tn.CommodityID
, 0 as TempTable
from TariffNew tn
inner join hhInvoiceLines inv
on tn.Tariffkode = inv.NrInvoiceLine
where (
tn.PreCarriage is not null
and tn.PreCarriage != ''
)
and (
tn.POLIata is not null
and tn.POLIata != ''
)
and inv.OfficeID = 13
and inv.IsDeleted = 0
and inv.LegalEntityID = 1
and tn.LegalEntityID = 1
and tn.Dep = 'E'
and tn.IsDeleted = 0
and tn.DateUntil = '2078-12-31 00:00:00'
and tn.Description = 'kgl'
)
) as b
where b.Record = 1
First, you have to define what you want to call "unique rows".
Once you have the set of columns that determines a row is unique, that is the set of columns you use in the partition by part of row_number()
In the code below, uncomment the column set that defines your "unique rows":
select *
from (
select ROW_NUMBER() over (
partition by
Dep
--, tariffkode
--, LinkTariffType
--, poliata
--, poliatavia
--, podiata
--, podiatavia
--, PreCarriage
--, PortTerminalId
--, Product
--, RoutingOrder
--, PrepaidCollect
--, isnull(description, '')
--, ScaleCalcCode
--, isnull(scalefrom, 0)
--, isnull(scaleto, 0)
--, CurrencyCode
--, Base
order by LinkTariffType desc
) Record
, *
from (
select tn.LinkTariffType
, tn.Dep
, tn.POLIata
, tn.POLIatavia
, tn.PODIata
, tn.PODIatavia
, tn.CurrencyCode
, tn.LegalEntityID
, tn.Rate
, tn.Base
, tn.Minimum
, tn.NrDescription
, tn.Description
, tn.DateFrom
, tn.DateUntil
, tn.DateCreate
, tn.DateMod
, tn.ModName
, tn.Tariffkode
, tn.ExpiryDate
, tn.PClass
, tn.Maximum
, tn.RoutingOrder
, tn.TariffCompType
, tn.PrePaidCollect
, tn.Product
, tn.IsDeleted
, (
select distinct Location_IATA
from Company
where Called = 'KARL KING'
and LegalEntityID = 1
and IsDeleted = 0
) as PreCarriage
, tn.PortTerminalID
, tn.ScaleFrom
, tn.ScaleTo
, tn.ScaleCalcCode
, tn.Mandatory
, (
select CompanyID
from PlaceOfReceipt
where warehouse = 'KARL KING'
and LegalEntityID = 1
and OfficeID = 13
and IsDeleted = 0
) as WarehouseID
, tn.TariffRelID
, tn.FreeDescription
, 0 as UnnamedColumn
, tn.ShipCode
, tn.AgentID
, tn.ContainerTypeID
, tn.CommodityID
, 0 as TempTable
from TariffNew tn
inner join hhInvoiceLines inv on tn.Tariffkode = inv.NrInvoiceLine
where tn.PreCarriage is not null
and tn.PreCarriage != ''
and tn.POLIata is not null
and tn.POLIata != ''
and inv.OfficeID = 13
and inv.IsDeleted = 0
and inv.LegalEntityID = 1
and tn.LegalEntityID = 1
and tn.Dep = 'E'
and tn.IsDeleted = 0
and tn.DateUntil = '2078-12-31 00:00:00'
and tn.Description = 'kgl'
) as s
) as b
where b.Record = 1

Column alias for VALUES ( ) syntax

Here's some SQL:
select T.*
FROM (
values ( '2014-05-30 17:26:32.749' , 'Casual shirt ' ) , ( '2014-05-30 17:26:32.749' , 'Casual shirt-White-Small ' ) , ( '2014-05-30 17:26:32.749' , 'Casual shirt-Blue-Medium ' ) , ( '2014-05-30 17:26:32.749' , 'Cords ' ) , ( '2014-05-30 17:26:32.749' , 'Bodysuit ' ) , ( '2014-05-30 17:26:32.749' , 'Cords-Black-29W x 28L ' ) , ( '2014-05-30 17:26:32.749' , 'Bodysuit-Black-Petit ' ) , ( '2014-05-30 17:26:32.749' , 'Cords-Brown-29W x 28L ' ) , ( '2014-05-30 17:26:32.749' , 'Classic pleated dress pant ' ) , ( '2014-05-30 17:26:32.749' , 'Classic pleated dress pant-Black-29W x 32L ' ) , ( '2014-05-30 17:26:32.749' , 'Dress shirt ' ) , ( '2014-05-30 17:26:32.749' , 'Dress shirt-White-15 ' ) , ( '2014-05-30 17:26:32.749' , 'Dress shirt-White-16 ' )
) T
;
It returns columns "1" and "2". How to change it so it returns named columns? Is it possible without using sysdummy1 ?
You can add the column names to T:
select T.*
FROM (values ( '2014-05-30 17:26:32.749' , 'Casual shirt ' ) ,
( '2014-05-30 17:26:32.749' , 'Casual shirt-White-Small ' ) ,
( '2014-05-30 17:26:32.749' , 'Casual shirt-Blue-Medium ' ) ,
( '2014-05-30 17:26:32.749' , 'Cords ' ) ,
( '2014-05-30 17:26:32.749' , 'Bodysuit ' ) ,
. . .
) T(col1, col2);

Is there an online service to count open and closing brackets?

Is anyone familiar with an online service where I can paste some code, and it tells me
If all my brackets are in place - Not missing/adding a close bracket?
According the language syntax - that the code is right?
I agree that most editors color scheme/and or give you an error. This specific one does not :-) It's called: Notepad ^^ - Which I use a lot, but still. If such a service exists - it can help no matter what IDE you use. If you have a piece of code you want to check - and don't feel like opening the IDE
Example (There is a missing bracket on the '3' part):
IF( OR( ISBLANK(BillingCountry), UPPER(BillingCountry) = 'UNITED STATES' , UPPER(BillingCountry) = 'USA', UPPER(BillingCountry) = 'US', UPPER(BillingCountry) = 'U.S.', UPPER(BillingCountry) = 'U.S.A.' ),
IF ( OR( BillingState = 'CA' , BillingState = 'WA' , BillingState = 'OR' , BillingState = 'ID' , BillingState = 'NV' , BillingState = 'MT' , BillingState = 'WY' , BillingState = 'CO' , BillingState = 'UT' , BillingState = 'AZ' , BillingState = 'NM'),
"1 - West"
,
IF ( OR( BillingState = 'IL' , BillingState = 'ND' , BillingState = 'SD' , BillingState = 'MI' , BillingState = 'NE' , BillingState = 'IA' , BillingState = 'KS' , BillingState = 'OK' , BillingState = 'TX' , BillingState = 'IL' , BillingState = 'MN' , BillingState = 'OH' , BillingState = 'WI' , BillingState = 'IN' , BillingState = 'CANADA'),
"2 - MIDWEST"
,
IF( OR( BillingState = 'NY' , BillingState = 'ME' , BillingState = 'NH' , BillingState = 'MA' , BillingState = 'RI' , BillingState = 'CT' , BillingState = 'NJ' , BillingState = 'DE' , BillingState = 'D.C.' , BillingState = 'VA' , BillingState = 'WVA' , BillingState = 'NC' , BillingState = 'SC' , BillingState = 'GA' , BillingState = 'FL' , BillingState = 'VT' , BillingState = 'PA' , BillingState = 'MD' , BillingState = 'KY' , BillingState = 'TN' , BillingState = 'AR' , BillingState = 'AL' , BillingState = 'MS' , BillingState = 'LS'),
"3 - East"
,
"Unknown" ) )
,
IF( LEN(BillingCountry) > 0, "4 - International", "Unknown")
)
This might help ideone.com. But you could just install Notepad++
This website might help to satisfy the 1st point : http://www.balancebraces.com/
This website has options to check Braces: { },Parentheses: ( ), Brackets: [ ], and Tags: < > and color them for easy identification.