Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP Export to .csv 2

Status
Not open for further replies.

craigward

Programmer
Nov 13, 2007
230
GB
Hi all,

I have this code which uses an SQL query to extract fields from a table into a .csv file and it works. The issue is there are two date fields in the query and they are be converted to US date format when they should be UK. The date format on the 2008 server is set to united kingdom and the machine has been restarted.

This code was running on a 2003 server and it worked fine on there. The two date fields are last_hs and next_hs. Does anyone have any ideas how i can code them to print out in UK format?


Code:
<%@ Language=VBScript %>
<!--#include file="../../includes/connection.asp"-->
<%

  sub Write_CSV_From_Recordset( RS )

    if RS.EOF then

      '
      ' There is no data to be written
      '
      exit sub

    end if

    dim RX
    set RX = new RegExp
        RX.Pattern = "\r|\n|,|"""

    dim i
    dim Field
    dim Separator

    '
    ' Writing the header row (header row contains field names)
    '

    Separator = ""
    for i = 0 to RS.Fields.Count - 1
      Field = RS.Fields( i ).Name
      if RX.Test( Field ) then
        '
        ' According to recommendations:
        ' - Fields that contain CR/LF, Comma or Double-quote should be enclosed in double-quotes
        ' - Double-quote itself must be escaped by preceeding with another double-quote
        '
        Field = """" & Replace( Field, """", """""" ) & """"
      end if
      Response.Write Separator & Field
      Separator = ","
    next
    Response.Write vbNewLine

    '
    ' Writing the data rows
    '

    do until RS.EOF
      Separator = ""
      for i = 0 to RS.Fields.Count - 1
        '
        ' Note the concatenation with empty string below
        ' This assures that NULL values are converted to empty string
        '
        Field = RS.Fields( i ).Value & ""
        if RX.Test( Field ) then
          Field = """" & Replace( Field, """", """""" ) & """"
        end if
        Response.Write Separator & Field
        Separator = ","
      next
      Response.Write vbNewLine
      RS.MoveNext
    loop

  end sub

  '
  ' EXAMPLE USAGE
  '
  ' - Open a RECORDSET object (forward-only, read-only recommended)
  ' - Send appropriate response headers
  ' - Call the function
  '

  dim RS1
 
  
  set RS1 = Server.CreateObject( "ADODB.RECORDSET" )
  
   
      qry = ";with cteDelegates(edrs_no, company, postalcode, last_hs, next_hs, risk_level, sct1, sct2, training_provider, rown) as(Select wce_course_delegate_link.edrs_no, wce_contact.company, postalcode, wce_contact.last_hs, wce_contact.next_hs, wce_contact.risk_level, wce_course_delegate_link.sct1, wce_course_delegate_link.sct2, wce_course_delegate_link.training_provider, ROW_NUMBER() OVER (PARTITION BY wce_course_delegate_link.edrs_no ORDER BY wce_course_delegate_link.edrs_no, wce_course_delegate_link.sct1, wce_course_delegate_link.training_provider) from wce_course_delegate_link left join wce_contact on wce_course_delegate_link.edrs_no =wce_contact.edrs where wce_course_delegate_link.end_date is null and wce_contact.record_type= 'company') Select edrs_no, company, postalcode, last_hs, next_hs, risk_level, sct1, sct2, training_provider from cteDelegates where rown = 1  order by edrs_no"
	Set RS1 = connStr.Execute(qry)
  Response.ContentType = "text/csv"

  Response.AddHeader "Content-Disposition", "attachment;filename=export.csv"

  Write_CSV_From_Recordset RS1
%>
 

Change the format in your CTE select:
Code:
Select edrs_no, company, postalcode, last_hs=CONVERT(varchar, last_hs, 103), next_hs=CONVERT(varchar, next_hs, 103) , risk_level, sct1, sct2, training_provider from cteDelegates where rown = 1  order by edrs_no


Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
That did the triack!! Many thanks for the help :)
 
I think your easiest solution would be to convert to the date format you want within the query.

Code:
      qry = ";with cteDelegates(edrs_no, company, postalcode, last_hs, next_hs, risk_level, sct1, sct2, training_provider, rown) as(Select wce_course_delegate_link.edrs_no, wce_contact.company, postalcode, wce_contact.last_hs, wce_contact.next_hs, wce_contact.risk_level, wce_course_delegate_link.sct1, wce_course_delegate_link.sct2, wce_course_delegate_link.training_provider, ROW_NUMBER() OVER (PARTITION BY wce_course_delegate_link.edrs_no ORDER BY wce_course_delegate_link.edrs_no, wce_course_delegate_link.sct1, wce_course_delegate_link.training_provider) from wce_course_delegate_link left join wce_contact on wce_course_delegate_link.edrs_no =wce_contact.edrs where wce_course_delegate_link.end_date is null and wce_contact.record_type= 'company') Select edrs_no, company, postalcode, [!]Convert(VarChar(10), [/!]last_hs[!], 103) As last_hs[/!], next_hs, risk_level, sct1, sct2, training_provider from cteDelegates where rown = 1  order by edrs_no"

The 103 that you see in the code corresponds with the British date format dd/mm/yyyy (
The date format on the 2008 server is set to united kingdom and the machine has been restarted.

I'm not really sure what you mean by this. Regardless, from SQL Server's perspective, the server's date format doesn't really matter. Each login for sql server has a language setting. It's this language setting that determines date format. The language setting for the sql server only applies to new logins. Existing logins would not be changed.

I encourage you to read this blog that I wrote a couple years ago. It explains the date formats pretty well (IMO).
I suggest that you download and run SQLCop. This is a free application that I wrote over a year ago. SQLCop can tell you which logins on your server has a language setting that is not match the server's default language. SQLCop is free to download and use. After running SQLCop and logging in to your database, expand Configuration, and then Login Language. You can find SQLCop from the link in my signature.

Personally, I think you should change the query to convert to the British date format, but also look at the login languages to make sure they are set properly because this can affect other queries that your application runs.

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thanks for the detailed response i will look at your suggestions
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top