[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Need some help with CREATE A VIEW with CAST

Posted on 2011-03-09
10
Medium Priority
?
268 Views
Last Modified: 2012-05-11
Hello
I would like to create a view =ViewLieferscheine1.
In the SQL Table the A.VersandPostleitzahl is a int.

I would like to CAST the int to a nvarchar.
CAST(A.VersandPostleitzahl AS CHAR)AS A.VersandPostleitzahl......that is wrong and will not do it
 
How can I do this. Please help.
500 points with a solution.
Thank you.
Best regards,
Thomas

sql.Format("CREATE VIEW ViewLieferscheine1 AS SELECT\
	  A.LieferscheinNr, A.VersandName1, A.VersandName2,\
	  A.VersandStrassePostfach,CAST(A.VersandPostleitzahl AS CHAR)AS A.VersandPostleitzahl,\
	  A.VersandOrt,A.VersandLand,A.Lieferscheindatum,\
	  A.DebitorenNr,A.Lieferungsbedingungenkurzbezeichnung AS Lieferbedingung,\
	  A.FakturiertAktiv AS Fakturiert,SUM(B.Einheitbez13wert) AS Gewicht\
	  FROM Lieferscheine AS A inner join Lieferscheinpositionen AS B\
	  ON A.LieferscheinNr=B.LieferscheinNr WHERE A.LieferscheinNr<%d\
	  GROUP BY A.LieferscheinNr,A.VersandName1,A.VersandName2,\
	  A.VersandStrassePostfach, A.VersandPostleitzahl,\
   	  A.VersandOrt,A.VersandLand, A.Lieferscheindatum, A.DebitorenNr,\
	  A.Lieferungsbedingungenkurzbezeichnung, A.FakturiertAktiv",2000000);

Open in new window

0
Comment
Question by:tsp2002
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
10 Comments
 
LVL 19

Accepted Solution

by:
Bhavesh Shah earned 2000 total points
ID: 35082403
Hi,


CAST(A.VersandPostleitzahl AS CHAR)AS A.VersandPostleitzahl

Remove A.

CAST(A.VersandPostleitzahl AS CHAR)AS VersandPostleitzahl



- Bhavesh
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 35082412
Hi,

You no need to give Table Alias while giving column alias.
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 35082424
Hi again,

Secondly your where condition seems not complete

WHERE A.LieferscheinNr<%d

This is not valid syntax.
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

Author Comment

by:tsp2002
ID: 35082788
Hello,
okay that works....thank you.

 A.LieferscheinNr<%d
how should I change this...also with a cast ?
The view should only have LieferscheinNr with a value <2000000
LieferscheinNr is a nvarchar in my SQL table.
Please let me know.
Thank you.


0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 35082870
Hi,

You can directly pass the value.
you no need to cast

sql.Format("CREATE VIEW ViewLieferscheine1 AS SELECT\
          A.LieferscheinNr, A.VersandName1, A.VersandName2,\
          A.VersandStrassePostfach,CAST(A.VersandPostleitzahl AS VARCHAR(10))AS A.VersandPostleitzahl,\
          A.VersandOrt,A.VersandLand,A.Lieferscheindatum,\
          A.DebitorenNr,A.Lieferungsbedingungenkurzbezeichnung AS Lieferbedingung,\
          A.FakturiertAktiv AS Fakturiert,SUM(B.Einheitbez13wert) AS Gewicht\
          FROM Lieferscheine AS A inner join Lieferscheinpositionen AS B\
          ON A.LieferscheinNr=B.LieferscheinNr WHERE A.LieferscheinNr<2000000\
          GROUP BY A.LieferscheinNr,A.VersandName1,A.VersandName2,\
          A.VersandStrassePostfach, A.VersandPostleitzahl,\
          A.VersandOrt,A.VersandLand, A.Lieferscheindatum, A.DebitorenNr,\
          A.Lieferungsbedingungenkurzbezeichnung, A.FakturiertAktiv",2000000);




secondly...try to use VarChar instead of Char with specifying size.
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 35082900
In above query,

I used VarChar(10) as example...

Please use actual size....

Your final view will be like .....
CREATE VIEW ViewLieferscheine1 AS 

SELECT
       A.LieferscheinNr, A.VersandName1, A.VersandName2,
       A.VersandStrassePostfach,CAST(A.VersandPostleitzahl AS VARCHAR(10))AS A.VersandPostleitzahl,
       A.VersandOrt,A.VersandLand,A.Lieferscheindatum,
       A.DebitorenNr,A.Lieferungsbedingungenkurzbezeichnung AS Lieferbedingung,
       A.FakturiertAktiv AS Fakturiert,SUM(B.Einheitbez13wert) AS Gewicht
FROM 
		Lieferscheine AS A 
inner join Lieferscheinpositionen AS B ON A.LieferscheinNr=B.LieferscheinNr 

WHERE A.LieferscheinNr<2000000

GROUP BY A.LieferscheinNr,A.VersandName1,A.VersandName2,
          A.VersandStrassePostfach, A.VersandPostleitzahl,
          A.VersandOrt,A.VersandLand, A.Lieferscheindatum, A.DebitorenNr,
          A.Lieferungsbedingungenkurzbezeichnung, A.FakturiertAktiv

Open in new window

0
 

Author Closing Comment

by:tsp2002
ID: 35082950
okay...thanks for your help.
That was fast and easy.
Have a great day.
Best regards,
Thomas
0
 

Author Comment

by:tsp2002
ID: 35083027

CREATE VIEW ViewLieferscheine1 AS 

SELECT
       A.LieferscheinNr, A.VersandName1, A.VersandName2,
       A.VersandStrassePostfach,CAST(A.VersandPostleitzahl AS VARCHAR(10))AS A.VersandPostleitzahl,
       A.VersandOrt,A.VersandLand,A.Lieferscheindatum,
       A.DebitorenNr,A.Lieferungsbedingungenkurzbezeichnung AS Lieferbedingung,
       A.FakturiertAktiv AS Fakturiert,SUM(B.Einheitbez13wert) AS Gewicht
 
you forgot to remove the A.
 AS VARCHAR(10))AS A.VersandPostleitzahl




CREATE VIEW ViewLieferscheine1 AS 

SELECT
       A.LieferscheinNr, A.VersandName1, A.VersandName2,
       A.VersandStrassePostfach,CAST(A.VersandPostleitzahl AS VARCHAR(10))AS VersandPostleitzahl,
       A.VersandOrt,A.VersandLand,A.Lieferscheindatum,
       A.DebitorenNr,A.Lieferungsbedingungenkurzbezeichnung AS Lieferbedingung,
       A.FakturiertAktiv AS Fakturiert,SUM(B.Einheitbez13wert) AS Gewicht

 
That was my error in the first place...thank you best regards to India from Germany

Open in new window

0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 35083049
Hi,

You used varchar(10).

Please recheck the size with your table.
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 35083056
happy to assist you.
You to have a great time ahead.
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question