SQL question

G

Guy Cohen

Hi all
I use ASP.NET with MS SQL Server Express 2005.

How do I:
1. Select a date in a specific format:
e.g. select FORMAT(recorddate,'DD/MM/YY') from mytable
2. Select only last 4 characters from a string.
e.g. select right(creditcardnumber,4 ) from mytable

Both return error........

TIA
Guy
 
G

Guy Cohen

OK I found the problem ....
The gridview was expecting the column name (select x from) but the
calculation right(x,4) changed the column name in the result......

Guy
 
R

Riki

Guy said:
Hi all
I use ASP.NET with MS SQL Server Express 2005.

How do I:
1. Select a date in a specific format:
e.g. select FORMAT(recorddate,'DD/MM/YY') from mytable
2. Select only last 4 characters from a string.
e.g. select right(creditcardnumber,4 ) from mytable

Both return error........

TIA
Guy

Look up the following functions in the Online help:

DATEPART
SUBSTRING

For the date, you can issue this command before executing the SELECT:

SET DATEFORMAT dmy
 
M

Mark Rae

1. Select a date in a specific format:
e.g. select FORMAT(recorddate,'DD/MM/YY') from mytable

No doubt there will be people here who will disagree with me here, but in my
experience it's much more efficient to let each layer / tier of your app do
what it's best at.

In this specific case, let your database layer fetch the data, and then use
your presentation layer for formatting that data and displaying it the way
you want. Presumably you're storing your date in SQL Server as either a
datetime or smalldatetime field? Therefore, there's no advantage in
converting it to a varchar just so you can return it to your front end as a
string. If you are binding the data e.g. to a GridView, you can format it
directly in the <asp:GridView> control tag by using the DataFormatString and
HtmlEncode properties of the <asp:BoundField> control tag e.g.

<asp:GridView ID="MyGridView" runat="server">
<asp:BoundField DataField="MyDateField" HeaderText="Date"
DataFormatString="{0:dd/MM/yy}" HtmlEncode="false" />
</asp:GridView>

If you're simply populating e.g. a TextBox, you can do something like:

MyTextBox.Text = <DataSource>["MyDateField"].ToString("DD/MM/YY");

Just as an aside, you'd be well advised to avoid ambiguous date formats such
as DD/MM/YY - e.g. what would you understand by the date 02/03/04...?
If you use something like "dd MMM yyyy", that would be totally unambiguous
across all cultures.

2. Select only last 4 characters from a string.
e.g. select right(creditcardnumber,4 ) from mytable

That's a little different. Assuming you only ever need to know the last four
digits of the credit card number, then getting the database layer to do this
for you is IMO good practice because it reduces the amount of data sent from
server to client - that's always a "good thing" to do.

Therefore, you need to use SQL Server's SUBSTRING method - look it up in SQL
Server Books On-Line.
 
C

Cowboy \(Gregory A. Beamer\)

In SQL, you have some control, but .NET may changes things for you
regardless, so you have to determine where it is best to put things.

FORMAT:

In .NET code it is String.Format()
In SQL
SELECT CONVERT(varchar(20),GetDate(), 103)

Last four characters. If you are using VB.NET, Right(stringName, length)
works. In C#, you have to use .Substring(start, finish)


In SQL:

DECLARE @creditcard varchar(16)

SET @creditcard = '4111111111111111'

SELECT SUBSTRING(@creditcard, (LEN(@creditcard)-4), 4)




--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*************************************************
Think outside of the box!
*************************************************
 
P

Paul

Hmm, "SELECT RIGHT(CreditCardNumber, 4) FROM myTable" - I don't suppose you
could let us know the IP address of your live server that will host your SQL
database because you've just told everyone that you're not storing credit
card numbers encrypted. Just a warning, regardless of the size of your
business of the purpose of your application, this is in voliation of most
credit card security recommendations. At least the number should be stored
encrypted to a specified minimum level - also you shouldn't really store the
(encrypted) details beyond the duration of the transaction (at which point
you only need to store the returned transaction reference from your payment
provider. The penalties applied by VISA/Mastercard, etc if a security
voliation on your companies part results in credit card details being stolen
are huge - probably enough to put most companies out of business.

- Paul.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top