Quantcast
Channel: Transact-SQL forum
Viewing all 12890 articles
Browse latest View live

Match part of column value with other column

$
0
0

Hi All,

I have a table  Customer which has values like below

CustomerName

Sam Billisto(sbillisto)

 Som Hanks (shanks)

jennifer shin(jshin)

I have another table CustomerKey which has following columns and values

CustomerloginCustKey

sbillisto1233

shanks3344

jshin8897

Now i want to join these two tables so that i can get CustKey for each customer (Basically i want to join values which is in bracket of customer column to CustomerLogin column)

Can someone please advice. Thanks


Merge statement syntax question

$
0
0
In a merge statement, when source and target not matched you insert, but when source and target matched and current_flag='Y'. You update the end_date and current_flag='N' but where are we updating the actual value(for the source and target match)? Thanks in advance.

svk

Min and max

$
0
0

hi

select id,min(actual) Min_Actual, max(actual) max_actual,
count(id)  from main
group by id


in min and max, i need to exclude actual = 0,  If use WHERE Acutal <> 0, my count(ID) will be wrong,  I want to count by id even actual is zero

Is it possible in one query

thanks

v

single upsert statement

$
0
0

Hi All,

I want to update/insert data in a table based on the value of a primary column

Ex:

Create Table Table1
(
AccountID INT,
LastName Varchar(10),
FirstName Varchar(10),
Email Varchar(50),
Address1 Varchar(10),
Address2 Varchar(10),
City Varchar(20),
State Varchar(20),
Country Varchar(20)
)

Create Table Table2
(
AccountID INT,
LastName Varchar(10),
FirstName Varchar(10),
Email Varchar(50),
Address1 Varchar(10),
Address2 Varchar(10),
City Varchar(20),
State Varchar(20),
Country Varchar(20)
)


Table1 will get new data or updates from another process. Now i need a SQL to update/insert Table2 if the AccountID is existing then i have update all the remaining fields if accountID is not found then i have to insert new record in Table2.

Is there simple way of doing this?

Thanks a  lot for your time.

Eshwar.


Please don't forget to Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful. It will helpful to other users.

delete year(getdate() -2) and month as 12

$
0
0

my source table column is ReportingPeriod Nvarchar(6) delete query has to be for datas before 2 years.

this is not working delete from table where ReportingPeriod < year(getdate() -2) and month as 12

i need to get 201212 as result

the criteria is current year -2 and month is 12

in 2014 expected result is 201212

in 2015 expected result is 201312


ShanmugaRaj

Rounding - Always Round Up

$
0
0

Hi All,

 

Is there a way in T-SQL using the round command to always round up regadless of the value.

 

For example:ROUND(normal_hours * pay_rate * 52, - 3) AS Expr3

 

normal_hours = 36

pay rate = 23.64

weeks in year = 52

 

(36 * 23.64) * 52 = 44,254.08

 

It rounds to 44,000. I want 45,000.  Is this possible. Am I overlooking the obvious?

 

 

Thanks in Advance and Happy Holidays!!!!!!!!

Adam

Join statement not working

$
0
0

I have 2 Tables:

Users
------------
UserName
FirstName
LastName

Tickets
---------------------
TicketID
TicketTitle
TicketDescription
UserName

I am trying to tget the FirstName from the Users Table based on matching UserNames for a selected ticket.

I have tried:

SELECT Users.FirstName
FROM USERS
	LEFT JOIN Tickets
	ON Users.UserName = Tickets.UserName
	WHERE Tickets.TicketID = 4

The syntax is correct but it's not returning the FirstName but is not populated.

What am I screwing up?

I'm being stupid, again. Sum & Count, together

$
0
0

Hi-

I thought that this should be easy and I was able to do this...nope.

i need to sum a value for each distinct count.  Having read a few post, i think i need an over() function.

select distinct
    [Training Title],
    [Training Type],
    count(CASE WHEN [user id] IS NULL THEN 0 ELSE 1 END) as 'Number',
    sum(cast([training hours] as float)) as 'Total Hours',
    count([training object id]) as 'Offerings'

-- this is duping, think i need an count([training object id]) over(PARTITION BY [TRAINING TITLE] )
from q2_2014
where [training type] = 'session'
group by
    [Training Title],
    [Training Type]



Odd problem/bug with view in SQL Server 2012

$
0
0

I am using SQL Server 2012, do not have the exact build # handy though.

I created a view:

create view myView as
  a.*, b.storeName, b.storeLocation
from
  Sales a inner join
  Stores b on a.StoresID = b.StoresID

I am certain that the view worked as expected for months.  But had an issue with some related code today, and upon inspection the "b.storename" value in the view had nothing to do with the storename -- it was like a different, random column was being displayed in the view in the "storename" column (for discussion purposes, say that the "store manager" values where being displayed in the view under the "storename" column).  I then rebuilt the view using the "alter" command, and presto, the "storename" column was back to normal and displaying store names.

My assumption is that putting in "*" in the view creation statement is a bad idea, and if you drop and add columns to the underlying base tables can lead to unexpected results.

Any other theories out there, or has anyone identified a specific related bug to what is being described here?

Thanks, any comments appreciated.

substring

$
0
0

Hi everyone,

I have a data like below

number

057740500

0410104

087219602

0020190

0077853

I need to take out last 2 digits of right side if the 2 digits are 00,01,02,03,04,05,06,07,08,09 only and take out all the zeros

in the front (left hand side) of the number

Can any one help.

calling a table from another server

$
0
0

Hi everyone,

I am trying to retrieve the data from a table which is on the different server. how do I do that? I am doing in the following way ., but I am getting an error like "Invalid object name xxxxxx"

select * from [another_server_name].[database_name].[table_name]

Thanks

Vishu

Left Outer Join on same table

$
0
0

Hi All,

We are with sql server 2012. I am asked to take over the maintenance of some stored procs. I am not that familiar with complex stored procedures. It was written by one of the team members. I see that they used left outer join on same table. Can you one of you explain and show me the links with examples about when do we do this.

The query is something like this

Table A

ID

TxnNumber

MasterNumber

Principal

Interest

date

recordtype

Txntype

code

1

241485

3000

2

20140805

81

B

PPP

2

241486

241485

1500

1

20140805

80

B

PPB

select A.ID, A.TxnNumber, A.MasterNumber....

From TableA A LEFT OUTER JOIN TableA AA ON AA.TxnNumber = A.TxnNumber and AA.recordtype = 81

why do we do left outer join on same table?

Thanks

Select first field from column in group value

$
0
0

Hello,

I need to return the first row of data grouped by the first field (Visit ID) in multi-column table:

VisitID       AdmitDate            Unit       Room          OrderCode

001041      2014-08-01         2E          202            SWCC
001041      2014-08-01         2E          202            NULL
006811      2014-08-01         2E          204            SWCC
008815      2014-08-01         2E          206            NULL
004895      2014-08-01         2E          207            SWFA
004895      2014-08-01         2E          207            SWCC
004895      2014-08-01         2E          207            NULL

To return:
001041      2014-08-01         2E          202            SWCC
006811      2014-08-01         2E          204            SWCC
008815      2014-08-01         2E          206            NULL
004895      2014-08-01         2E          207            SWFA

I currently have a group by clause with all field names; and have tried max on the OrderCode in the select that doesn't work, and FIRST isn't recognized in SSL server.  Do I need a subquery (if so, how as I'm newer to writing SQL) or what is another solution?  Thank you.

t-sql query help

$
0
0

Hi i have table having data like this


SQL Query CASE statement using two fields

$
0
0

Hi,

I have two fields. One is called rescategory1, the other is called rescategory2.

I'm not sure if its a CASE statement I need or some sort of WHERE clause but I want to create a query that does the following or something similar:

CASE rescategory2
WHEN rescategory1 = '44' AND rescategory2 = '1' THEN 'Backup'
WHEN rescategory1 = '44' AND rescategory2 = '2' THEN 'Hardware'
END AS [Resolution Sub Category]

Basically, I'm looking to give rescategory2 a value based on that of rescategory1 and rescategory2 combined.

How do I write this?

Cheers

Paul


Trim up to a certain character

$
0
0

 

hi everyone, i hope someone can help me with this.

 

i have a string that I need to trim up to a pipe character ("|"). the problem is, the length of the characters before this pipe character varies greatly. How can I trim the string (for example, abcdef346 | rfgt...) and get only abcdef346 as the result?

 

any help would be greatly appreciated.

Issue with Like operator

$
0
0

Hello Everyone,

I have values of column "Job_Name" in the below way.

sderg_AS_ASDFSDFS_DF__D_FG__Dsdfsdf 

I have a query to print all the characters that occur before first "_" i.e, "sderg" in this case.

select "Job_Name", substr("Job_Name",1,locate('_',"Job_Name")-1),from  table_name

I am unable to write a query to print all the characters that occur after last "_" i.e, "Dsdfsdf" in this case.

Thanks in advance.


Thanks and Regards, Readers please vote for my posts if the questions i asked are helpful.

Diffrence of last 2 rows

$
0
0

Hi i have a table which includes meter reading of different meters. When i give the meter id i need to take the last 2 meter reading values . My table structure and datas are

CREATE TABLE MeterReading ( ReadingID int, MeterID int, DataCounter int, Value numeric(16,4) )

INSERT INTO MeterReading 
VALUES(1, 1, 1, 102.45)
INSERT INTO MeterReading 
VALUES(2, 2, 1, 110.45)
INSERT INTO MeterReading 
VALUES(3, 1, 2, 110.58)
INSERT INTO MeterReading 
VALUES(4, 2, 2, 115.89)
INSERT INTO MeterReading 
VALUES(5, 1, 4, 120.00)
INSERT INTO MeterReading 
VALUES(6, 2, 4, 116.23)

Here i need the difference of latest (based on DataCounter) difference of Values corresponding MeterID

select top 2 * from MeterReading  
where MeterID = 1
order by DataCounter desc
I give the select query. so that i need to take the diffrence of these selected rows' Values. Always the latest value is larger



How can get TOP 10 percent rows against each value of a column in Table?

$
0
0

i have below example. I have to select top 50 percent data rows for each value of col1. I am using SQL Server 2008.

Col1        Col2

A             1

A             3

A             5

A             100

B              2
B              3

B              10

B              11

Expected Output:

Col1        Col2

A             1

A             3

B              2
B              3

I can do this by running query for each value of col1 but i want to do it in single query instead of loop or cursor.

I would appreciate if some one suggest any optimal solution for this problem. Thanks


Zubair Khalid

Text Date in sort order of date

$
0
0

Hi All;

Below is my expression in SQL 

(CONVERT(CHAR(4), e.createdon, 100) + CONVERT(CHAR(4), e.createdon, 120) as createdon

its gives output Oct 2013

I need to sort this expression in date order

Is this possible?

Any help much appreciated

Thanks


Pradnya07

Viewing all 12890 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>