samedi 31 octobre 2015

Strange characters in MSSQL table

This is my first time working in a Microsoft environment, all of my experience being in Unix, so forgive me if my question is a common one (I looked but couldn't find anything quite like this).

I have a Microsoft SQL Server 2005 database, and many of the tables have text like the following:

¼ƒ¹ƒƒ…““ŽP ƒƒ…““ŽP ƒ

The above text seems to translate to NSX (I copied the text into a different column in the database, and checked in the application that uses this database, and it is "NXS"). Obviously, the application seems to know how to translate this text. I'm wondering, is this an encoding issue? Is it fixable such that I can view the text? Or, possibly, is this some method of encrypting the text?

I'm just hoping someone has encountered this before, knows what's up and how to fix it.

vendredi 30 octobre 2015

SQL Server Query returning all rows in a table instead of MAX()

Im trying to return the highest/last hour meter reading for each piece of equipment in an inventory, however, the query below lists all of the instances even using MAX.

select      sil.[Posting Date], 
            mre.[Service Item No_], 
            sil.[Job Code], 
            max(mre.[Reading]) as 'Hour Reading'     

from        [$meter reading entry]mre left outer join
            [$service invoice line]sil on mre.[Service Item No_] = sil.[Service Item No_]

where       sil.[Job Code] = 200


group by    mre.[Service Item No_], sil.[Job Code], mre.[Reading], sil.[Posting Date]

jeudi 29 octobre 2015

insert based on value in first row

I have a fixed file that I am importing into a single column with data similar to what you see below:

ABC$        WC        11683                                    
11608000163118430001002010056788000000007680031722800315723      
11683000486080280000002010043213000000007120012669100126691      
ABC$        WC                         000000020000000148000     
ABC$        WC        11683                                    
1168101057561604000050200001234000000027020023194001231940      
54322010240519720000502000011682000000035640006721001067210      
1167701030336257000050200008765000000023610029066101151149      
11680010471244820000502000011680000000027515026398201263982

I want to split and insert this data into another table but I want to do so as long as the '11683' is equal to a column value in a different table + 1. I will then increment that value (not seen here).

I tried the following:

declare @blob as varchar(5) 
declare @Num as varchar(5)

set @blob = substring(sdg_winn_blob.blob, 23,5)
set @Num = (Cnum.num + 1)

IF @blob = @Num
INSERT INTO SDG_CWF
    (
        GAME,SERIAL,WINNER,TYPE
    )
SELECT convert(numeric, substring(blob,28, 5)),convert(numeric, substring(blob, 8, 9)),
        (Case when (substring(blob, 6,2)='10') then '3' 
              when (substring(blob, 6,2)='11') then '4' 
              else substring(blob, 7, 1)
         End),
        (Case when (substring(blob, 52,2)='10') then '3' 
              when (substring(blob, 52,2)='11') then '4' 
              else substring(blob, 53, 1)
         End)

FROM sdg_winn_blob
WHERE blob not like 'ABC$%'
else
print 'The Job Failed'

The insert works fine until I try to check to see if the number at position (23, 5) is the same as the number in the Cnum table. I get the error:

Msg 4104, Level 16, State 1, Line 4
The multi-part identifier "sdg_winn_blob.blob" could not be bound.
Msg 4104, Level 16, State 1, Line 5
The multi-part identifier "Cnum.num" could not be bound.

How to Combine the data which is other rows in Same SQL Table [duplicate]

This question already has an answer here:

SQL - joining records from same table

I have data in a table and I need to simply the big table into smaller one version of table by reducing duplicated rows the concat data into rows.

Here is my base table:

BW  SPEC    BAND    PORT    MODE    VC  TC  DOMAIN  BLOCK
---------------------------------------------------------
-   SNDR    1      see_b    umts    VC1 TC1  SVB      rx1
-   SNDR    2      see_b    umts    VC1 TC1  SVB      rx1
-   SNDR    3      see_b    umts    VC1 TC1  SVB      rx1
-   SNDR_2  4      see_b    umts    VC1 TC1  SVB      rx1
-   SNDR    5      see_b    umts    VC1 TC1  SVB      rx1
-   SNDR    6      see_b    umts    VC1 TC1  SVB      rx1
-   SNDR    8      see_b    umts    VC1 TC1  SVB      rx1
12  SNDR_2  9      see_b    umts    VC1 TC1  SVB      rx1
12  SNDR_2  29     see_b    umts    VC1 TC1  SVB      rx1

I want to combine data which present in BAND:

Based on BW Spec PORT MODE VC TC DOMAIN BLOCK for example

enter image description here

Thanks in advance

enter image description here

how to get update records which is newly inserted only in sql?

i want to move data from old sql db to new sql db which are having same tables. but new table is having 100(assume) records and old db is having 1000 records. i inserted all records in new db but i got diffetent id's because this id is having identity in new db.

now my concern is how can i update new identity value for the foreigh key columns. the foreign key column is having duplicate in new db. i have to update foreign key column only for newly inserted records.

mercredi 28 octobre 2015

User Defined function SQL 2008

I'm trying to create a calculated column that returns an INT value, I ve created a function and need to pass the ndx number to the function and having issues with returning multiple values within the sub query. how do I pass the ndx number to the function, I'm assuming that the calculated column looks at values from the same row!?

Msg 512, Level 16, State 1, Line 1

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

CREATE FUNCTION dbo.Nat_Weight(@me38_cycle_data_ndx INT)
RETURNS INT
AS 
BEGIN
DECLARE @nat_weight INT =0;
DECLARE @mattype1 INT;
DECLARE @mattype2 INT;
DECLARE @mattype3 INT;
--DECLARE @me38_cycle_data_ndx INT;
-- get material type, need only hoppers 1-3, hopper 4,5,6 material type will never = 2
SET @mattype1 = (SELECT typehopper_01 FROM mm_Cycle_Data);
SET @mattype2 = (SELECT typehopper_02 FROM mm_Cycle_Data );
SET @mattype3 = (SELECT typehopper_03 FROM mm_Cycle_Data );
-- if material type=2 then add to @nat_weight ,  
IF @mattype1 = 2
    set @nat_weight = (SELECT cyclehopper_01 FROM mm_Cycle_Data WHERE me38_cycle_data_ndx=@me38_cycle_data_ndx );
IF @mattype2 = 2
    set @nat_weight =@nat_weight+ (SELECT cyclehopper_02 FROM mm_Cycle_Data WHERE me38_cycle_data_ndx=@me38_cycle_data_ndx );
IF @mattype3 = 2
    set @nat_weight =@nat_weight+ (SELECT cyclehopper_03 FROM mm_Cycle_Data WHERE me38_cycle_data_ndx=@me38_cycle_data_ndx )
RETURN @nat_weight
END 

mardi 27 octobre 2015

LIKE clause in PIVOT?

I want to have a LIKE kind of clause in a PIVOT. Right now, I have this:

PIVOT (
    MAX(TAX_AMOUNT) FOR TAX_CODE IN ([TVH], [TVH-ON], [TVH-NB], [TPS], [TVQ])
) PVT

But that is problematic, if new TVH TAX_CODE's are being added (TVH-SK, TVH-QC, etc.), this pivot will stop working.

I tried [%TVH%] but that's a syntax error.

Any ideas ?