<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: sql code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 11 May 2008 20:35:15 GMT</pubDate>
    <description>DZone Snippets: sql code</description>
    <item>
      <title>how to create a new user in MySQL</title>
      <link>http://snippets.dzone.com/posts/show/5277</link>
      <description>// first, mysql -uroot mysql&lt;br /&gt;// then:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;  mysql% GRANT ALL PRIVILEGES ON *.* TO 'jm3_spoon'@'localhost' IDENTIFIED BY 'stirthatshit' WITH GRANT OPTION;&lt;br /&gt;  mysql% create database jm3_agitator;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 24 Mar 2008 04:06:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5277</guid>
      <author>jm3 (john manoogian III)</author>
    </item>
    <item>
      <title>Drop an unnamed constraint from SqlServer 2000</title>
      <link>http://snippets.dzone.com/posts/show/5221</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;declare @name nvarchar(32), &lt;br /&gt;    @sql nvarchar(1000)&lt;br /&gt;&lt;br /&gt;-- find constraint name&lt;br /&gt;select @name = O.name &lt;br /&gt;from sysobjects AS O&lt;br /&gt;left join sysobjects AS T&lt;br /&gt;    on O.parent_obj = T.id&lt;br /&gt;where isnull(objectproperty(O.id,'IsMSShipped'),1) = 0&lt;br /&gt;    and O.name not like '%dtproper%'&lt;br /&gt;    and O.name not like 'dt[_]%'&lt;br /&gt;    and T.name = 'MyTable'&lt;br /&gt;    and O.name like 'DF__MyTable__MyColu%'&lt;br /&gt;&lt;br /&gt;-- delete if found&lt;br /&gt;if not @name is null&lt;br /&gt;begin&lt;br /&gt;    select @sql = 'ALTER TABLE [MyTable] DROP CONSTRAINT [' + @name + ']'&lt;br /&gt;    execute sp_executesql @sql&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;-- do your ALTER TABLE here&lt;br /&gt;&lt;br /&gt;-- replace the constraint&lt;br /&gt;select @sql = 'ALTER TABLE [MyTable] ADD CONSTRAINT [' + @name + '] DEFAULT (0) FOR [MyColumn]'&lt;br /&gt;execute sp_executesql @sql&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 12 Mar 2008 22:00:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5221</guid>
      <author>frost137 (Douglas Wyatt)</author>
    </item>
    <item>
      <title>Rails Array#add_condition Method</title>
      <link>http://snippets.dzone.com/posts/show/5147</link>
      <description>Lets you add a condition to a set of ActiveRecord conditions easily like this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  conditions = ['active = ? and type = ?', true, 2]&lt;br /&gt;  conditions.add_condition ['person_id = ?', 345]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Array&lt;br /&gt;  def add_condition(condition, conjunction='and')&lt;br /&gt;    if condition.is_a? Array&lt;br /&gt;      if self.empty?&lt;br /&gt;        (self &lt;&lt; condition).flatten!&lt;br /&gt;      else&lt;br /&gt;        self[0] += " #{conjunction} " + condition.shift&lt;br /&gt;        (self &lt;&lt; condition).flatten!&lt;br /&gt;      end&lt;br /&gt;    elsif condition.is_a? String&lt;br /&gt;      self[0] += " #{conjunction} " + condition&lt;br /&gt;    else&lt;br /&gt;      raise "don't know how to handle this condition type"&lt;br /&gt;    end&lt;br /&gt;    self&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 18 Feb 2008 02:54:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5147</guid>
      <author>timmorgan (Tim Morgan)</author>
    </item>
    <item>
      <title>Regular Expressions with MySQL</title>
      <link>http://snippets.dzone.com/posts/show/5011</link>
      <description>&lt;code&gt;&lt;br /&gt;SELECT * FROM texts WHERE content REGEXP '[^a-z]Hello[^a-z]' ;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;If you really want to force a REGEXP comparison to be case sensitive, use the BINARY keyword to make one of the strings a binary string.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;SELECT * FROM texts WHERE content REGEXP BINARY '[^a-zA-Z]Hello[^a-zA-Z]' ;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Warning: some characters do not work. Example :&lt;br /&gt;&lt;code&gt;&lt;br /&gt;SELECT * FROM texts WHERE content REGEXP '[^\w]Hello[^\w]' ;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.ab-d.fr/"&gt;ab-d.fr source code&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Sat, 19 Jan 2008 22:05:22 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5011</guid>
      <author>ki4ngel (Benoit Asselin)</author>
    </item>
    <item>
      <title>GPS distance and initial bearing between points (MySQL)</title>
      <link>http://snippets.dzone.com/posts/show/4991</link>
      <description>Assume you have a table of locations with Latitude and Longitude for each one. In my case the table is "station" and the primary key is "LocID".&lt;br /&gt;&lt;br /&gt;First we create a view to help with the 3D geometry (6378 = Earth's radius in km):&lt;br /&gt;&lt;code&gt;&lt;br /&gt;CREATE VIEW gpsGlb AS&lt;br /&gt;    SELECT &lt;br /&gt;        LocID&lt;br /&gt;        ,6378 * COS(RADIANS(Latitude)) * COS(RADIANS(Longitude)) AS x&lt;br /&gt;        ,6378 * COS(RADIANS(Latitude)) * SIN(RADIANS(Longitude)) AS y&lt;br /&gt;        ,6378 * SIN(RADIANS(Latitude)) AS z&lt;br /&gt;    FROM station;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Here I query for distances to all my locations that are NOT LocID = 405 (rounded miles in my case):&lt;br /&gt;&lt;code&gt;&lt;br /&gt;SELECT &lt;br /&gt;    LocID&lt;br /&gt;    ,ROUND((2 * 6378 * ASIN(d / 2 / 6378)) * 0.621371192) AS dist_mi&lt;br /&gt;FROM&lt;br /&gt;    (SELECT&lt;br /&gt;        SQRT(dx * dx + dy * dy + dz * dz) AS d&lt;br /&gt;        ,LocID&lt;br /&gt;     FROM&lt;br /&gt;        (SELECT&lt;br /&gt;            p1.x - p2.x AS dx&lt;br /&gt;            ,p1.y - p2.y AS dy&lt;br /&gt;            ,p1.z - p2.z AS dz&lt;br /&gt;            ,p2.LocID&lt;br /&gt;        FROM gpsGlb p1&lt;br /&gt;        JOIN gpsGlb p2 ON (p1.LocID = 405 AND p2.LocID != 405)&lt;br /&gt;       ) t1&lt;br /&gt;    ) t2&lt;br /&gt;ORDER BY dist_mi&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Here I get the initial bearing to the locations. The "boxed" calculation will come in handy later.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;SELECT&lt;br /&gt;    LocID&lt;br /&gt;    ,(360 + DEGREES(ATAN2(y, x))) % 360 AS initBearing_deg&lt;br /&gt;    ,ROUND(((360 + DEGREES(ATAN2(y, x))) % 360) / 22.5) * 22.5 &lt;br /&gt;     AS initBearingBoxed_deg&lt;br /&gt;FROM&lt;br /&gt;    (SELECT&lt;br /&gt;        SIN(RADIANS(s2.Longitude - s1.Longitude)) * COS(RADIANS(s2.Latitude)) &lt;br /&gt;        AS y&lt;br /&gt;        ,COS(RADIANS(s1.Latitude)) * SIN(RADIANS(s2.Latitude))&lt;br /&gt;            - SIN(RADIANS(s1.Latitude)) * COS(RADIANS(s2.Latitude))&lt;br /&gt;               * COS(RADIANS(s2.Longitude - s1.Longitude)) &lt;br /&gt;        AS x&lt;br /&gt;        ,s2.LocID&lt;br /&gt;    FROM station s1&lt;br /&gt;    JOIN station s2 ON (s1.LocID = 405 AND s2.LocID != 405)&lt;br /&gt;    ) q1&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Here's the combined query plus boxed degrees converted to 'NNE', etc. I've also added a limit for the distance in the qq1 subquery.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;SELECT&lt;br /&gt;    qq2.LocID&lt;br /&gt;    ,dist_mi&lt;br /&gt;    ,CASE initBearingBoxed_deg&lt;br /&gt;        WHEN 22.5 THEN 'NNE'   WHEN 45 THEN 'NE'&lt;br /&gt;        WHEN 67.5 THEN 'ENE'   WHEN 90 THEN 'E'&lt;br /&gt;        WHEN 112.5 THEN 'ESE'  WHEN 135 THEN 'SE'&lt;br /&gt;        WHEN 157.5 THEN 'SSE'  WHEN 180 THEN 'S'&lt;br /&gt;        WHEN 202.5 THEN 'SSW'  WHEN 225 THEN 'SW'&lt;br /&gt;        WHEN 247.5 THEN 'WSW'  WHEN 270 THEN 'W'&lt;br /&gt;        WHEN 292.5 THEN 'WNW'  WHEN 315 THEN 'NW'&lt;br /&gt;        WHEN 337.5 THEN 'NNW'  ELSE 'N'&lt;br /&gt;     END AS bearing&lt;br /&gt;FROM (&lt;br /&gt;    SELECT &lt;br /&gt;        LocID&lt;br /&gt;        ,ROUND((2 * 6378 * ASIN(d / 2 / 6378)) * 0.621371192) AS dist_mi&lt;br /&gt;    FROM&lt;br /&gt;        (SELECT&lt;br /&gt;            SQRT(dx * dx + dy * dy + dz * dz) AS d&lt;br /&gt;            ,LocID&lt;br /&gt;         FROM&lt;br /&gt;            (SELECT&lt;br /&gt;                p1.x - p2.x AS dx&lt;br /&gt;                ,p1.y - p2.y AS dy&lt;br /&gt;                ,p1.z - p2.z AS dz&lt;br /&gt;                ,p2.LocID&lt;br /&gt;            FROM gpsGlb p1&lt;br /&gt;            JOIN gpsGlb p2 ON (p1.LocID = 405 AND p2.LocID != 405)&lt;br /&gt;           ) t1&lt;br /&gt;        ) t2&lt;br /&gt;    ) qq1&lt;br /&gt;JOIN (&lt;br /&gt;    SELECT&lt;br /&gt;        LocID&lt;br /&gt;        ,(360 + DEGREES(ATAN2(y, x))) % 360 AS initBearing_deg&lt;br /&gt;        ,(360 + ROUND((DEGREES(ATAN2(y, x))) / 22.5) * 22.5) % 360 &lt;br /&gt;         AS initBearingBoxed_deg&lt;br /&gt;    FROM&lt;br /&gt;        (SELECT&lt;br /&gt;            SIN(RADIANS(s2.Longitude - s1.Longitude)) * COS(RADIANS(s2.Latitude)) &lt;br /&gt;             AS y&lt;br /&gt;            ,COS(RADIANS(s1.Latitude)) * SIN(RADIANS(s2.Latitude))&lt;br /&gt;                - SIN(RADIANS(s1.Latitude)) * COS(RADIANS(s2.Latitude))&lt;br /&gt;                   * COS(RADIANS(s2.Longitude - s1.Longitude)) &lt;br /&gt;             AS x&lt;br /&gt;            ,s2.LocID&lt;br /&gt;        FROM station s1&lt;br /&gt;        JOIN station s2 ON (s1.LocID = 405 AND s2.LocID != 405)&lt;br /&gt;        ) q1&lt;br /&gt;    ) qq2 ON (qq1.LocID = qq2.LocID&lt;br /&gt;              AND qq1.dist_mi &lt;= 60)&lt;br /&gt;ORDER BY dist_mi&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 16 Jan 2008 21:14:20 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4991</guid>
      <author>mrclay (Steve Clay)</author>
    </item>
    <item>
      <title>Find Exact Directory name of Variable Directory Name</title>
      <link>http://snippets.dzone.com/posts/show/4964</link>
      <description>This sql server procedure help you, if you have remmember some string of your directory and path name then you will get the exact directory name of that path.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;create proc usp_srchDir&lt;br /&gt;(&lt;br /&gt;	@pathName nvarchar(100),&lt;br /&gt;	@strDirName nvarchar(50),&lt;br /&gt;	@ExactName nvarchar(20) OUTPUT&lt;br /&gt;)&lt;br /&gt;AS&lt;br /&gt;BEGIN&lt;br /&gt;	SET NOCOUNT ON&lt;br /&gt;	DECLARE @strCMD nvarchar(500)&lt;br /&gt;	SET @strCMD='dir  "'+ @pathName +'" /ad/o | find /I "'+@strDirName+'"'&lt;br /&gt;	create table tblFindDIR(SearchDIRName nvarchar(200))&lt;br /&gt;	insert into tblFindDIR(SearchDIRName)	&lt;br /&gt;	exec master.dbo.xp_cmdshell @strCMD&lt;br /&gt;	delete from tblFindDIR where isnull(SearchDIRName,'')=''&lt;br /&gt;	select @ExactName=ltrim(rtrim(right(SearchDIRName,20))) from tblFindDIR where SearchDIRName like '%'+@strDirName+'%' &lt;br /&gt;	drop table tblFindDIR&lt;br /&gt;	SET NOCOUNT OFF&lt;br /&gt;END&lt;br /&gt;&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 09 Jan 2008 08:29:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4964</guid>
      <author>JhaRakesh (Rakesh Jha)</author>
    </item>
    <item>
      <title>Some problems with charset in UTF-8 ?</title>
      <link>http://snippets.dzone.com/posts/show/4814</link>
      <description>So you can use this request MySQL before all others, for fix your problems :&lt;br /&gt;&lt;code&gt;&lt;br /&gt;...&lt;br /&gt;mysql_query( "SET NAMES 'utf8' " );&lt;br /&gt;...&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.ab-d.fr/"&gt;Source: ab-d.fr&lt;br /&gt;Languages: PHP and MySQL&lt;/a&gt;</description>
      <pubDate>Fri, 23 Nov 2007 22:07:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4814</guid>
      <author>ki4ngel (Benoit Asselin)</author>
    </item>
    <item>
      <title>Use get_magic_quotes_gpc();</title>
      <link>http://snippets.dzone.com/posts/show/4795</link>
      <description>&lt;code&gt;&lt;br /&gt;function f_magic_quotes($text) {&lt;br /&gt;	if ( !get_magic_quotes_gpc() ) {&lt;br /&gt;		return addslashes($text);&lt;br /&gt;	} else {&lt;br /&gt;		return $text;&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function f_clean_quotes($text) {&lt;br /&gt;	if ( !get_magic_quotes_gpc() ) {&lt;br /&gt;		return $text;&lt;br /&gt;	} else {&lt;br /&gt;		return stripslashes($text);&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.ab-d.fr/"&gt;Source: ab-d&lt;/a&gt;&lt;br /&gt;</description>
      <pubDate>Sun, 18 Nov 2007 20:57:07 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4795</guid>
      <author>ki4ngel (Benoit Asselin)</author>
    </item>
    <item>
      <title>How to produce daily database table dumps in CSV format</title>
      <link>http://snippets.dzone.com/posts/show/4790</link>
      <description>The following code demonstrates how to produce CSV files with dynamic&lt;br /&gt;file name pattern based on a current day. The produced files have the following naming format:&lt;br /&gt;TABLE_NAME_MM_DD_YYYY.csv&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;!DOCTYPE etl SYSTEM "http://scriptella.javaforge.com/dtd/etl.dtd"&gt;&lt;br /&gt;&lt;etl&gt;&lt;br /&gt;    &lt;properties&gt; &lt;!-- Configure table name --&gt;&lt;br /&gt;        table_name=test&lt;br /&gt;    &lt;/properties&gt;&lt;br /&gt;    &lt;connection id="in" driver="auto" url="jdbc:oracle:thin:@localhost:1521:ORCL" &lt;br /&gt;      classpath="ojdbc14.jar" user="scott" password="tiger"/&gt;&lt;br /&gt;    &lt;connection id="out" driver="csv" url="${table_name}_${etl.date.now('MM_dd_yyyy')}.csv" /&gt;&lt;br /&gt;    &lt;query connection-id="in"&gt; &lt;!-- Query table rows --&gt;&lt;br /&gt;        SELECT * FROM ${table_name}&lt;br /&gt;        &lt;script connection-id="out"&gt; &lt;!-- Export each row into a CSV --&gt;&lt;br /&gt;            $ID, $Name, $Surname &lt;!-- Use column names from selected table --&gt;&lt;br /&gt;        &lt;/script&gt;&lt;br /&gt;    &lt;/query&gt;&lt;br /&gt;&lt;/etl&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Use &lt;a href="http://scriptella.javaforge.com"&gt;Scriptella ETL&lt;/a&gt; to run the example. &lt;br /&gt;&lt;br /&gt;See &lt;a href="http://snippets.dzone.com/posts/show/4862"&gt;How to execute an ETL file&lt;/a&gt; from command line, Ant or directly from Java .</description>
      <pubDate>Fri, 16 Nov 2007 21:08:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4790</guid>
      <author>ejboy (Fyodor Kupolov)</author>
    </item>
    <item>
      <title>disable SQL / MySQL in rails logging in development mode</title>
      <link>http://snippets.dzone.com/posts/show/4727</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;ActiveRecord::Base.logger = Logger.new("#{RAILS_ROOT}/log/#{RAILS_ENV}_database.log")&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;goes at end of config/env</description>
      <pubDate>Fri, 02 Nov 2007 00:09:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4727</guid>
      <author>jm3 (john manoogian III)</author>
    </item>
  </channel>
</rss>
